How to get the "mousedown" event on OpenLayers.Map?

I am using OpenLayers 2.13. I want to detect the events mousedown , mousemove , mouseup when the mouse is above OpenLayers.Map , so I wrote the following code.

 var map = new OpenLayers.Map("map",{controls:[ new OpenLayers.Control.Navigation(), new OpenLayers.Control.ArgParser(), new OpenLayers.Control.Attribution() ]}); var events = map.events; events.register("mousedown",map,function(e){ console.log("mousedown"); }); events.register("mousemove",map,function(e){ console.log("mousemove"); }); events.register("mouseup",map,function(e){ console.log("mouseup"); }); 

As a result, mousemove and mouseup detected, but not mousedown s.

It says here that mousemove and mouseup supported, but mousedown not. Are there any hacks that I can apply to detect mousedown events without modifying the OpenLayers script?

+4
source share
3 answers

Add the 4th argument as true .

 var events = map.events; events.register("mousedown",map,function(e){ console.log("mousedown"); return true; },true); // This argument is new 

There are several event listeners that are already listening for the mousedown event. One of them will have an event when it is detected [dragging the card], so the mousedown event will never reach the last listener.

Without the 4th argument, events.register() will add a listener to the end of the event listener chain. With the 4th argument, he will add it to the first.

+8
source

There is also registerPriority (), which adds to the top of the list:

http://dev.openlayers.org/docs/files/OpenLayers/Events-js.html#OpenLayers.Events.registerPriority

+2
source

A simple way to do this:

 map.events.listeners.mousedown.unshift({ func: function(){ // code } }); 
-1
source

Source: https://habr.com/ru/post/1488837/


All Articles