[OpenLayers-Users] Capturing mouse events

Mike Leahy mgleahy at alumni.uwaterloo.ca
Fri Jul 23 06:24:38 EDT 2010


Thanks Piero,

The first link is definitely a good example to rely on for most typical click 
events (with the left/default mouse button).  But it still lacks the ability 
to catch a right click.  I tried to mimic this in the html file I attached to 
my first message, but the 'rightclick' method that is noted in the 
documentation doesn't actually seem to do anything...while similar 
mouseup/mousedown events are also treated as single-clicks.

It's not a big deal I guess...it would just be nice to handle all these events 
through the same set of objects if possible, rather than working around it.  
But in any case, here's what I've got so far, using a bit of jQuery:

$(map.div).bind('contextmenu',function(e){
	e.preventDefault();
});

$(map.div).mouseup(function(e){
	if (OpenLayers.Event.isRightClick(e)){
		console.log("Right button click up");
		e.xy = map.events.getMousePosition(e);
		e.geoxy = map.getLonLatFromPixel(e.xy);
		// Do more stuff...
	}
});

That seems to be enough to get me going for now - unless anyone can make any 
further recommendations.

Thanks for the help.

Mike

On Thursday, July 23, 2010 03:38:19 Piero Campa wrote:
> 
> Well, regarding the mouse 'clicks' you should take a look at the source of
> this example:
> http://openlayers.org/dev/examples/click-handler.html
> It's something about defining a class OpenLayers.Control.Click inheriting
> from OpenLayers.Control, then defining the click Handlers.
> 
> Whether you'd like instead to manage mouse moves you can just register the
> event, e.g.
> myMap.events.register('mousemove', this, MouseMove);
> where myMap is your OpenLayers.Map and MouseMove is your own function that
> is called at every mouse movement.
> 
> Take a look here for the supported event types (of a map):
> http://dev.openlayers.org/apidocs/files/OpenLayers/Map-js.html#OpenLayers.M
> ap.EVENT_TYPES
> 
> I'm not OL expert so I don't give 100% certainty that these are the best
> ways to go... they worked for me!
> Piero
> --
> View this message in context:
> http://osgeo-org.1803224.n2.nabble.com/Capturing-mouse-events-tp5327630p53
> 28664.html Sent from the OpenLayers Users mailing list archive at
> Nabble.com.
> 
> On Thursday, July 22, 2010 23:13:24 Mike Leahy wrote:
> > Ok...a bit more on this.
> > 
> > I am able to capture right clicks by disabling the contextmenu action,
> > then checking for the right-click event on mouseup.
> > 
> > map.div.oncontextmenu = function(){return false;};
> > map.div.onmouseup = function(e){
> > 
> > 	if (OpenLayers.Event.isRightClick(e)){
> > 	
> > 		alert("Right button click"); // Do stuff here...
> > 	
> > 	}
> > 
> > };
> > 
> > From here, though, I'd want to get the event variable 'e' to have the
> > various properties that are normally prepared through OpenLayers events
> > (e.g., to have the 'xy' property).  Can anyone recommend a better way to
> > do this?  Is there a problem with taking over the mouseup event on the
> > map div?  If so, what other approach might work here (e.g., would using
> > jQuery to bind events work better)?
> > 
> > Regards,
> > Mike
> > 
> > On Thursday, July 22, 2010 21:48:54 Mike Leahy wrote:
> > > That does work for right-click, although it's not as tidy.  What
> > > confuses me is that it would seem that based on the documentation that
> > > there are specific functions meant for handling these events in the
> > > click handler class:
> > > http://dev.openlayers.org/docs/files/OpenLayers/Handler/Click-js.html
> > > 
> > > Am I misinterpreting that?
> > > 
> > > On Thursday, July 22, 2010 21:07:33 Phil Scadden wrote:
> > > > Not sure how OpenLayers handles it, but fir html, how  about
> > > > onContextMenu event? For mouse handling, I am pretty sure you can
> > > > only reliably pick right button in onMousedown event
> > > > --
> > > > Phil Scadden, Senior Scientist GNS Science Ltd 764 Cumberland St,
> > > > Private Bag 1930, Dunedin, New Zealand Ph +64 3 4799663, fax +64 3
> > > > 477 5232
> > > > 
> > > > Notice: This email and any attachments are confidential. If received
> > > > in error please destroy and immediately notify us. Do not copy or
> > > > disclose the contents.
> > > > 
> > > > On Thursday, July 22, 2010 18:58:39 Mike Leahy wrote:
> > > > > Hello list,
> > > > > 
> > > > > I'm taking a look at OpenLayers again, and I'm just trying to
> > > > > figure out how to effectively capture various mouse events.  Based
> > > > > on the examples provided, it's fairly straightforward to get click
> > > > > and double-click events.  But I don't seem to be able to figure
> > > > > out how to handle the right click event, or
> > > > > mouseup/mousedown events.
> > > > > 
> > > > > I've tried cobbling together a sample...and although I seem to be
> > > > > able to make something happen with mouseup and mousedown, these end
> > > > > up appearing as 'click' events.  As for a right-click event, I
> > > > > can't seem to capture that at all.  I'm sure this can be done, but
> > > > > I just don't understand the OpenLayers classes well enough to
> > > > > implement it.
> > > > > 
> > > > >  Can someone look at the attached html file and identify where I'm
> > > > > 
> > > > > going wrong?  If you drop it into the examples of the dev version
> > > > > of OpenLayers, it should work (at least in FireFox), and it'll
> > > > > write messages into a Firebug console.
> > > > > 
> > > > > Mike



More information about the Users mailing list