[OpenLayers-Users] Right-click menu

Graham Davis gdavis at refractions.net
Fri Feb 23 11:29:32 EST 2007


Sure,

The javascript event object contains information about buttons clicked.  
The mouseDown function should already be doing a test to see if the 
click is a left-click (and exits the function if it isn't).  I added a 
function to Events.js for checking right-clicks similar to the 
left-click one:

  isRightClick: function(event) {
    return (((event.which) && (event.which == 2)) ||
            ((event.button) && (event.button == 2)));
  },

Then in MouseDefaults.js I modified  mouseDown like this:

    mouseDown: function(evt) {

        // on left click, do map dragging stuff
        if (OpenLayers.Event.isLeftClick(evt)) {
             ... put usual map dragging code here
        }
        // on right-click show map menu
        else if (OpenLayers.Event.isRightClick(evt)) {
             ... do your right-click code here
        }


Graham.


Robert Buzink wrote:

> that's great. I want that :-). Could you tell me how you managed to 
> 'properly
> recongnize a right-click event in the MouseDefeaults.js file on 
> mousedown'?
>
> On 2/21/07, *Graham Davis* <gdavis at refractions.net 
> <mailto:gdavis at refractions.net>> wrote:
>
>     Well, I seem to have found the answer to my own question, so I thought
>     I'd email it to the list just in case anyone else needs to know how.
>
>     Basically there is an oncontextmenu event for DOM objects.  You can
>     assign this to a function and put any code you like in there, and then
>     return false at the end to ensure the event stops and doesn't display
>     the browser's context menu.  So I added this to my map object to
>     disable
>     all browser context menus from appearing on the map like so:
>
>     map.div.oncontextmenu = function noContextMenu(e) {return false;};
>
>     Seems to work well in both IE and FF (haven't test Opera or on a Mac).
>
>     --
>     Graham Davis
>     Refractions Research Inc.
>     gdavis at refractions.net <mailto:gdavis at refractions.net>
>
>
>
>     Graham Davis wrote:
>
>     >I'm trying to implement a right-click menu on my map, similar to what
>     >you see here: http://maps.ask.com/maps.  I've been able to properly
>     >recongnize a right-click event in the MouseDefeaults.js file on
>     mousedown.
>     >
>     >My problem is that the browser's own right-click menu also
>     appears, and
>     >I need to disable or stop this.  I've tried stopping the event from
>     >propagating in the mousedown function after my code (and in the
>     mouseup
>     >function since that is when the browser menu seems to appear) however
>     >this doesn't stop the browser menu from appearing.
>     >
>     >Has anyone been able to implement a right-click menu on their map
>     that
>     >properly disables the browser's right-click menu?
>     >
>     >Also, this might be of importance, but I'm using the vector branch of
>     >code.  I've noticed that when not using a vector layer, the
>     right-click
>     >menu on the map brings up the image menu as you have clicked on
>     an image
>     >in the map.  When there is a vector layer overtop, right-clicking
>     brings
>     >up the standard browser menu where you can view source and
>     whatnot, so
>     >the menus are different.  However, I think any fix for this would
>     work
>     >for both (and I have been testing everything I try both with and
>     without
>     >a vector layer). So far nothing works.
>     >
>     >
>     >
>
>
>     _______________________________________________
>     Users mailing list
>     Users at openlayers.org <mailto:Users at openlayers.org>
>     http://openlayers.org/mailman/listinfo/users
>     <http://openlayers.org/mailman/listinfo/users>
>
>
>
>
> -- 
> Robert Buzink, website ontwerp en ontwikkeling
> Sint-Pietersaalststraat 123
> 9000 Gent
> +32 (0)9 33 00 267
> +32 (0)487 548 414
> post at robertbuzink.nl <mailto:post at robertbuzink.nl>
> robertbuzink.nl/webdesign <http://robertbuzink.nl/webdesign> 



-- 
Graham Davis
Refractions Research Inc.
gdavis at refractions.net




More information about the Users mailing list