On 09/19/2012 03:55 PM, Russell Edwards wrote:
Hello,

Apologies for asking a beginner's question.

I am writing a plugin which has its own MapMode. I need to catch mouse
wheel and key press events. How can I do this?

I have tried

... extends MapMode implements MouseWheelListener, KeyListener

... enterMode() {
...
         Main.map.mapView.addMouseWheelListener(this);
         Main.map.mapView.addKeyListener(this);
  ...
}

but the listeners never seem to get called. Moreover the map still zooms
in and out when the wheel is moved.

Thanks in advance for your help and patience,

I put your code in SelectAction and MouseWheelListener worked directly.
Not sure if you can easily suppress zoom, though. What is your mapmode about?

Other plugins have a similar similar setup, e.g. alignways has a mapmode with MouseListener. In order to catch keystrokes, you should try AWTEventListener.

Paul




Index: src/org/openstreetmap/josm/actions/mapmode/SelectAction.java
===================================================================
--- src/org/openstreetmap/josm/actions/mapmode/SelectAction.java	(revision 5513)
+++ src/org/openstreetmap/josm/actions/mapmode/SelectAction.java	(working copy)
@@ -14,6 +14,8 @@
 import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
+import java.awt.event.MouseWheelEvent;
+import java.awt.event.MouseWheelListener;
 import java.awt.geom.Point2D;
 import java.util.Collection;
 import java.util.Collections;
@@ -63,7 +65,12 @@
  *
  * @author imi
  */
-public class SelectAction extends MapMode implements AWTEventListener, SelectionEnded {
+public class SelectAction extends MapMode implements AWTEventListener, SelectionEnded, MouseWheelListener {
+
+    @Override
+    public void mouseWheelMoved(MouseWheelEvent e) {
+        System.err.println("mouse wheel!");
+    }
     // "select" means the selection rectangle and "move" means either dragging
     // or select if no mouse movement occurs (i.e. just clicking)
     enum Mode { move, rotate, scale, select }
@@ -178,11 +185,14 @@
         super.enterMode();
         mv.addMouseListener(this);
         mv.addMouseMotionListener(this);
+        mv.addMouseWheelListener(this);
         mv.setVirtualNodesEnabled(Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0);
         drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
         cycleManager.init();
         virtualManager.init();
         // This is required to update the cursors when ctrl/shift/alt is pressed
+
+        // Keyevents:
         try {
             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
         } catch (SecurityException ex) {}
@@ -207,6 +217,7 @@
      * This is called whenever the keyboard modifier status changes
      */
     public void eventDispatched(AWTEvent e) {
+        System.err.println("AWT: "+e);
         if(oldEvent == null)
             return;
         // We don't have a mouse event, so we pass the old mouse event but the
_______________________________________________
josm-dev mailing list
josm-dev@openstreetmap.org
http://lists.openstreetmap.org/listinfo/josm-dev

Reply via email to