Changeset 7217 in josm for trunk


Ignore:
Timestamp:
2014-06-06T10:10:31+02:00 (10 years ago)
Author:
akks
Message:

see #10104: refactor key press/release detection introducing Main.map.keyDetector

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java

    r7005 r7217  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.AWTEvent;
    76import java.awt.Cursor;
    8 import java.awt.Toolkit;
    9 import java.awt.event.AWTEventListener;
    107import java.awt.event.ActionEvent;
    118import java.awt.event.InputEvent;
     
    2926import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3027import org.openstreetmap.josm.gui.util.HighlightHelper;
     28import org.openstreetmap.josm.gui.util.ModifierListener;
    3129import org.openstreetmap.josm.tools.CheckParameterUtil;
    3230import org.openstreetmap.josm.tools.ImageProvider;
     
    4846 * @author imi
    4947 */
    50 public class DeleteAction extends MapMode implements AWTEventListener {
     48public class DeleteAction extends MapMode implements ModifierListener {
    5149    // Cache previous mouse event (needed when only the modifier keys are
    5250    // pressed but the mouse isn't moved)
     
    113111        Main.map.mapView.addMouseMotionListener(this);
    114112        // This is required to update the cursors when ctrl/shift/alt is pressed
    115         try {
    116             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    117         } catch (SecurityException ex) {
    118             Main.warn(ex);
    119         }
     113        Main.map.keyDetector.addModifierListener(this);
    120114    }
    121115
     
    125119        Main.map.mapView.removeMouseListener(this);
    126120        Main.map.mapView.removeMouseMotionListener(this);
    127         try {
    128             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    129         } catch (SecurityException ex) {
    130             Main.warn(ex);
    131         }
     121        Main.map.keyDetector.removeModifierListener(this);
    132122        removeHighlighting();
    133123    }
     
    400390     */
    401391    @Override
    402     public void eventDispatched(AWTEvent e) {
     392    public void modifiersChanged(int modifiers) {
    403393        if(oldEvent == null)
    404394            return;
    405395        // We don't have a mouse event, so we pass the old mouse event but the
    406396        // new modifiers.
    407         giveUserFeedback(oldEvent, ((InputEvent) e).getModifiers());
     397        giveUserFeedback(oldEvent, modifiers);
    408398    }
    409399}
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r7005 r7217  
    77import static org.openstreetmap.josm.tools.I18n.trn;
    88
    9 import java.awt.AWTEvent;
    109import java.awt.BasicStroke;
    1110import java.awt.Color;
    12 import java.awt.Component;
    1311import java.awt.Cursor;
    1412import java.awt.Graphics2D;
    15 import java.awt.KeyboardFocusManager;
    1613import java.awt.Point;
    1714import java.awt.Stroke;
    18 import java.awt.Toolkit;
    19 import java.awt.event.AWTEventListener;
    2015import java.awt.event.ActionEvent;
    21 import java.awt.event.ActionListener;
    22 import java.awt.event.InputEvent;
    2316import java.awt.event.KeyEvent;
    2417import java.awt.event.MouseEvent;
     
    3629import java.util.Map;
    3730import java.util.Set;
    38 import java.util.TreeSet;
    3931
    4032import javax.swing.AbstractAction;
    4133import javax.swing.JCheckBoxMenuItem;
    42 import javax.swing.JFrame;
    4334import javax.swing.JMenuItem;
    4435import javax.swing.JOptionPane;
    4536import javax.swing.JPopupMenu;
    46 import javax.swing.SwingUtilities;
    47 import javax.swing.Timer;
    4837
    4938import org.openstreetmap.josm.Main;
     
    7160import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    7261import org.openstreetmap.josm.gui.util.GuiHelper;
     62import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
     63import org.openstreetmap.josm.gui.util.ModifierListener;
    7364import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    7465import org.openstreetmap.josm.tools.Geometry;
     
    8172 * Mapmode to add nodes, create and extend ways.
    8273 */
    83 public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, AWTEventListener {
     74public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, KeyPressReleaseListener, ModifierListener {
    8475    private final Cursor cursorJoinNode;
    8576    private final Cursor cursorJoinWay;
     
    112103    private final SnapHelper snapHelper = new SnapHelper();
    113104
    114     private Shortcut backspaceShortcut;
    115     private BackSpaceAction backspaceAction;
     105    private final Shortcut backspaceShortcut;
     106    private final BackSpaceAction backspaceAction;
    116107    private final Shortcut snappingShortcut;
     108    private boolean ignoreNextKeyRelease;
    117109
    118110    private final SnapChangeAction snapChangeAction;
     
    227219        snapCheckboxMenuItem.getAction().setEnabled(true);
    228220
    229         timer = new Timer(0, new ActionListener() {
    230             @Override
    231             public void actionPerformed(ActionEvent ae) {
    232                 timer.stop();
    233                 if (set.remove(releaseEvent.getKeyCode())) {
    234                     doKeyReleaseEvent(releaseEvent);
    235                 }
    236             }
    237 
    238         });
    239221        Main.map.statusLine.getAnglePanel().addMouseListener(snapHelper.anglePopupListener);
    240222        Main.registerActionShortcut(backspaceAction, backspaceShortcut);
     
    245227        DataSet.addSelectionListener(this);
    246228
    247         try {
    248             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    249         } catch (SecurityException ex) {
    250             Main.warn(ex);
    251         }
     229        Main.map.keyDetector.addKeyListener(this);
     230        Main.map.keyDetector.addModifierListener(this);
     231        ignoreNextKeyRelease = true;
    252232        // would like to but haven't got mouse position yet:
    253233        // computeHelperLine(false, false, false);
     
    269249
    270250        removeHighlighting();
    271         try {
    272             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    273         } catch (SecurityException ex) {
    274             Main.warn(ex);
    275         }
     251        Main.map.keyDetector.removeKeyListener(this);
     252        Main.map.keyDetector.removeModifierListener(this);
    276253
    277254        // when exiting we let everybody know about the currently selected
     
    288265     */
    289266    @Override
    290     public void eventDispatched(AWTEvent event) {
     267    public void modifiersChanged(int modifiers) {
    291268        if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable())
    292269            return;
    293         if (event instanceof KeyEvent) {
    294             KeyEvent e = (KeyEvent) event;
    295             if (snappingShortcut.isEvent(e) || (useRepeatedShortcut && getShortcut().isEvent(e))) {
    296                 Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    297                 if (SwingUtilities.getWindowAncestor(focused) instanceof JFrame) {
    298                     processKeyEvent(e);
    299                 }
    300             }
    301         } //  toggle angle snapping
    302         updateKeyModifiers((InputEvent) event);
     270        updateKeyModifiers(modifiers);
    303271        computeHelperLine();
    304272        addHighlighting();
    305273    }
    306274
    307     // events for crossplatform key holding processing
    308     private final Set<Integer> set = new TreeSet<>();
    309     private KeyEvent releaseEvent;
    310     private Timer timer;
    311     void processKeyEvent(KeyEvent e) {
     275    @Override
     276    public void doKeyPressed(KeyEvent e) {
    312277        if (!snappingShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
    313278            return;
    314 
    315         if (e.getID() == KeyEvent.KEY_PRESSED) {
    316             if (timer.isRunning()) {
    317                 timer.stop();
    318             } else if (set.add((e.getKeyCode()))) {
    319                 doKeyPressEvent(e);
    320             }
    321         } else if (e.getID() == KeyEvent.KEY_RELEASED) {
    322             if (timer.isRunning()) {
    323                 timer.stop();
    324                 if (set.remove(e.getKeyCode())) {
    325                     doKeyReleaseEvent(e);
    326                 }
    327             } else {
    328                 releaseEvent = e;
    329                 timer.restart();
    330             }
    331         }
    332     }
    333 
    334     private void doKeyPressEvent(KeyEvent e) {
    335279        snapHelper.setFixedMode();
    336280        computeHelperLine();
    337281        redrawIfRequired();
    338282    }
    339     private void doKeyReleaseEvent(KeyEvent e) {
     283
     284    @Override
     285    public void doKeyReleased(KeyEvent e) {
     286        if (!snappingShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
     287            return;
     288        if (ignoreNextKeyRelease) {
     289            ignoreNextKeyRelease = false;
     290            return;
     291        }
    340292        snapHelper.unFixOrTurnOff();
    341293        computeHelperLine();
     
    393345     * If in nodeway mode, insert the node into the way.
    394346     */
    395     @Override public void mouseReleased(MouseEvent e) {
     347    @Override
     348    public void mouseReleased(MouseEvent e) {
    396349        if (e.getButton() == MouseEvent.BUTTON3) {
    397350            Point curMousePos = e.getPoint();
     
    419372
    420373        // we copy ctrl/alt/shift from the event just in case our global
    421         // AWTEvent didn't make it through the security manager. Unclear
     374        // keyDetector didn't make it through the security manager. Unclear
    422375        // if that can ever happen but better be safe.
    423376        updateKeyModifiers(e);
     
    775728
    776729        // we copy ctrl/alt/shift from the event just in case our global
    777         // AWTEvent didn't make it through the security manager. Unclear
     730        // keyDetector didn't make it through the security manager. Unclear
    778731        // if that can ever happen but better be safe.
    779732        updateKeyModifiers(e);
     
    941894     * Repaint on mouse exit so that the helper line goes away.
    942895     */
    943     @Override public void mouseExited(MouseEvent e) {
     896    @Override
     897    public void mouseExited(MouseEvent e) {
    944898        if(!Main.map.mapView.isActiveLayerDrawable())
    945899            return;
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java

    r7216 r7217  
    66import static org.openstreetmap.josm.tools.I18n.tr;
    77
    8 import java.awt.AWTEvent;
    98import java.awt.BasicStroke;
    109import java.awt.Color;
    11 import java.awt.Component;
    1210import java.awt.Cursor;
    1311import java.awt.Graphics2D;
    14 import java.awt.KeyboardFocusManager;
    1512import java.awt.Point;
    1613import java.awt.Rectangle;
    1714import java.awt.Stroke;
    18 import java.awt.Toolkit;
    19 import java.awt.event.AWTEventListener;
    2015import java.awt.event.ActionEvent;
    21 import java.awt.event.ActionListener;
    22 import java.awt.event.InputEvent;
    2316import java.awt.event.KeyEvent;
    2417import java.awt.event.MouseEvent;
     
    3225import java.util.LinkedList;
    3326import java.util.List;
    34 import java.util.Set;
    35 import java.util.TreeSet;
    3627import javax.swing.JCheckBoxMenuItem;
    37 import javax.swing.JFrame;
    3828import javax.swing.JMenuItem;
    39 import javax.swing.SwingUtilities;
    40 import javax.swing.Timer;
    4129
    4230import org.openstreetmap.josm.Main;
     
    6048import org.openstreetmap.josm.gui.layer.MapViewPaintable;
    6149import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     50import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
    6251import org.openstreetmap.josm.gui.util.GuiHelper;
     52import org.openstreetmap.josm.gui.util.ModifierListener;
    6353import org.openstreetmap.josm.tools.Geometry;
    6454import org.openstreetmap.josm.tools.ImageProvider;
     
    6858 * Makes a rectangle from a line, or modifies a rectangle.
    6959 */
    70 public class ExtrudeAction extends MapMode implements MapViewPaintable {
     60public class ExtrudeAction extends MapMode implements MapViewPaintable, KeyPressReleaseListener, ModifierListener {
    7161
    7262    enum Mode { extrude, translate, select, create_new, translate_node }
     
    157147    /** The cursor for the 'alwaysCreateNodes' submode. */
    158148    private final Cursor cursorCreateNodes;
     149
     150    private boolean ignoreNextKeyRelease;
    159151
    160152    private static class ReferenceSegment {
     
    225217                tr("Mode: {0}", tr("Extrude Dual alignment")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
    226218        useRepeatedShortcut = Main.pref.getBoolean("extrude.dualalign.toggleOnRepeatedX", true);
    227         timer = new Timer(0, new ActionListener() {
    228             @Override
    229             public void actionPerformed(ActionEvent ae) {
    230                 timer.stop();
    231                 if (set.remove(releaseEvent.getKeyCode())) {
    232                     doKeyReleaseEvent(releaseEvent);
    233                 }
    234             }
    235         });
    236219    }
    237220
     
    294277        Main.map.mapView.addMouseListener(this);
    295278        Main.map.mapView.addMouseMotionListener(this);
    296         try {
    297             Toolkit.getDefaultToolkit().addAWTEventListener(altKeyListener, AWTEvent.KEY_EVENT_MASK);
    298         } catch (SecurityException ex) {
    299             Main.warn(ex);
    300         }
    301279        initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200);
    302280        initialMoveThreshold = Main.pref.getInteger("extrude.initial-move-threshold", 1);
     
    313291        ignoreSharedNodes = Main.pref.getBoolean("extrude.ignore-shared-nodes", true);
    314292        dualAlignCheckboxMenuItem.getAction().setEnabled(true);
     293        ignoreNextKeyRelease = true;
     294        Main.map.keyDetector.addKeyListener(this);
     295        Main.map.keyDetector.addModifierListener(this);
    315296    }
    316297
     
    321302        Main.map.mapView.removeTemporaryLayer(this);
    322303        dualAlignCheckboxMenuItem.getAction().setEnabled(false);
    323         try {
    324             Toolkit.getDefaultToolkit().removeAWTEventListener(altKeyListener);
    325         } catch (SecurityException ex) {
    326             Main.warn(ex);
    327         }
     304        Main.map.keyDetector.removeKeyListener(this);
     305        Main.map.keyDetector.removeModifierListener(this);
    328306        super.exitMode();
    329307    }
     
    334312
    335313    /**
    336      * This listener is used to indicate different modes via cursor when the Alt/Ctrl/Shift modifier is pressed,
    337      * and for listening to dual alignment shortcuts.
    338      */
    339     private final AWTEventListener altKeyListener = new AWTEventListener() {
    340         @Override
    341         public void eventDispatched(AWTEvent e) {
    342             if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable())
    343                 return;
    344             InputEvent ie = (InputEvent) e;
    345             boolean alt = (ie.getModifiers() & (ActionEvent.ALT_MASK|InputEvent.ALT_GRAPH_MASK)) != 0;
    346             boolean ctrl = (ie.getModifiers() & (ActionEvent.CTRL_MASK)) != 0;
    347             boolean shift = (ie.getModifiers() & (ActionEvent.SHIFT_MASK)) != 0;
    348             if (mode == Mode.select) {
    349                 Main.map.mapView.setNewCursor(ctrl ? cursorTranslate : alt ? cursorCreateNew : shift ? cursorCreateNodes : cursor, this);
    350             }
    351             if (e instanceof KeyEvent) {
    352                 KeyEvent ke = (KeyEvent) e;
    353                 if (dualAlignShortcut.isEvent(ke) || (useRepeatedShortcut && getShortcut().isEvent(ke))) {
    354                     Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    355                     if (SwingUtilities.getWindowAncestor(focused) instanceof JFrame) {
    356                         processKeyEvent(ke);
    357                     }
    358                 }
    359             }
     314     * This method is called to indicate different modes via cursor when the Alt/Ctrl/Shift modifier is pressed,
     315     */
     316    @Override
     317    public void modifiersChanged(int modifiers) {
     318        if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable())
     319            return;
     320        updateKeyModifiers(modifiers);
     321        if (mode == Mode.select) {
     322            Main.map.mapView.setNewCursor(ctrl ? cursorTranslate : alt ? cursorCreateNew : shift ? cursorCreateNodes : cursor, this);
    360323        }
    361324    };
    362325
    363     // events for crossplatform key holding processing
    364     // thanks to http://www.arco.in-berlin.de/keyevent.html
    365     private final Set<Integer> set = new TreeSet<Integer>();
    366     private KeyEvent releaseEvent;
    367     private Timer timer;
    368     private void processKeyEvent(KeyEvent e) {
     326    @Override
     327    public void doKeyPressed(KeyEvent e) {
     328    }
     329
     330    @Override
     331    public void doKeyReleased(KeyEvent e) {
    369332        if (!dualAlignShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
    370             return;
    371 
    372         if (e.getID() == KeyEvent.KEY_PRESSED) {
    373             if (timer.isRunning()) {
    374                 timer.stop();
    375             } else if (set.add((e.getKeyCode()))) {
    376                 doKeyPressEvent(e);
    377             }
    378         } else if (e.getID() == KeyEvent.KEY_RELEASED) {
    379             if (timer.isRunning()) {
    380                 timer.stop();
    381                 if (set.remove(e.getKeyCode())) {
    382                     doKeyReleaseEvent(e);
    383                 }
    384             } else {
    385                 releaseEvent = e;
    386                 timer.restart();
    387             }
    388         }
    389     }
    390 
    391     private void doKeyPressEvent(KeyEvent e) {
    392     }
    393 
    394     private void doKeyReleaseEvent(KeyEvent e) {
    395         toggleDualAlign();
    396     }
    397 
     333             return;
     334        if (ignoreNextKeyRelease) {
     335            ignoreNextKeyRelease = false;
     336        } else {
     337            toggleDualAlign();
     338        }
     339    }
    398340    /**
    399341     * Toggles dual alignment mode.
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ImproveWayAccuracyAction.java

    r7005 r7217  
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
    8 import java.awt.AWTEvent;
    98import java.awt.Color;
    109import java.awt.Cursor;
     
    1211import java.awt.Point;
    1312import java.awt.Stroke;
    14 import java.awt.Toolkit;
    15 import java.awt.event.AWTEventListener;
    16 import java.awt.event.InputEvent;
    1713import java.awt.event.KeyEvent;
    1814import java.awt.event.MouseEvent;
     
    4743import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4844import org.openstreetmap.josm.gui.util.GuiHelper;
     45import org.openstreetmap.josm.gui.util.ModifierListener;
    4946import org.openstreetmap.josm.tools.ImageProvider;
    5047import org.openstreetmap.josm.tools.Pair;
     
    5552 */
    5653public class ImproveWayAccuracyAction extends MapMode implements MapViewPaintable,
    57         SelectionChangedListener, AWTEventListener {
     54        SelectionChangedListener, ModifierListener {
    5855
    5956    enum State {
     
    145142        DataSet.addSelectionListener(this);
    146143
    147         try {
    148             Toolkit.getDefaultToolkit().addAWTEventListener(this,
    149                     AWTEvent.KEY_EVENT_MASK);
    150         } catch (SecurityException ex) {
    151             Main.warn(ex);
    152         }
     144        Main.map.keyDetector.addModifierListener(this);
    153145    }
    154146
     
    162154        DataSet.removeSelectionListener(this);
    163155
    164         try {
    165             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    166         } catch (SecurityException ex) {
    167             Main.warn(ex);
    168         }
    169 
     156        Main.map.keyDetector.addModifierListener(this);
    170157        Main.map.mapView.repaint();
    171158    }
     
    321308    // -------------------------------------------------------------------------
    322309    @Override
    323     public void eventDispatched(AWTEvent event) {
     310    public void modifiersChanged(int modifiers) {
    324311        if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable()) {
    325312            return;
    326313        }
    327         updateKeyModifiers((InputEvent) event);
     314        updateKeyModifiers(modifiers);
    328315        updateCursorDependentObjectsIfNeeded();
    329316        updateCursor();
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ParallelWayAction.java

    r7005 r7217  
    66import static org.openstreetmap.josm.tools.I18n.tr;
    77
    8 import java.awt.AWTEvent;
    98import java.awt.Color;
    109import java.awt.Cursor;
     
    1211import java.awt.Point;
    1312import java.awt.Stroke;
    14 import java.awt.Toolkit;
    15 import java.awt.event.AWTEventListener;
    16 import java.awt.event.InputEvent;
    1713import java.awt.event.KeyEvent;
    1814import java.awt.event.MouseEvent;
     
    4137import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4238import org.openstreetmap.josm.gui.util.GuiHelper;
     39import org.openstreetmap.josm.gui.util.ModifierListener;
    4340import org.openstreetmap.josm.tools.Geometry;
    4441import org.openstreetmap.josm.tools.ImageProvider;
     
    8481 * @author Ole Jørgen Brønner (olejorgenb)
    8582 */
    86 public class ParallelWayAction extends MapMode implements AWTEventListener, MapViewPaintable, PreferenceChangedListener {
     83public class ParallelWayAction extends MapMode implements ModifierListener, MapViewPaintable, PreferenceChangedListener {
    8784
    8885    private enum Mode {
     
    161158
    162159        //// Needed to update the mouse cursor if modifiers are changed when the mouse is motionless
    163         try {
    164             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    165         } catch (SecurityException ex) {
    166             Main.warn(ex);
    167         }
     160        Main.map.keyDetector.addModifierListener(this);
    168161        sourceWays = new LinkedHashSet<>(getCurrentDataSet().getSelectedWays());
    169162        for (Way w : sourceWays) {
     
    181174        Main.map.statusLine.setDist(-1);
    182175        Main.map.statusLine.repaint();
    183         try {
    184             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    185         } catch (SecurityException ex) {
    186             Main.warn(ex);
    187         }
     176        Main.map.keyDetector.removeModifierListener(this);
    188177        removeWayHighlighting(sourceWays);
    189178        pWays = null;
     
    238227
    239228    @Override
    240     public void eventDispatched(AWTEvent e) {
     229    public void modifiersChanged(int modifiers) {
    241230        if (Main.map == null || mv == null || !mv.isActiveLayerDrawable())
    242231            return;
    243232
    244233        // Should only get InputEvents due to the mask in enterMode
    245         if (updateModifiersState((InputEvent) e)) {
     234        if (updateModifiersState(modifiers)) {
    246235            updateStatusLine();
    247236            updateCursor();
     
    249238    }
    250239
    251     private boolean updateModifiersState(InputEvent e) {
     240    private boolean updateModifiersState(int modifiers) {
    252241        boolean oldAlt = alt, oldShift = shift, oldCtrl = ctrl;
    253         updateKeyModifiers(e);
     242        updateKeyModifiers(modifiers);
    254243        return (oldAlt != alt || oldShift != shift || oldCtrl != ctrl);
    255244    }
     
    302291    public void mousePressed(MouseEvent e) {
    303292        requestFocusInMapView();
    304         updateModifiersState(e);
     293        updateModifiersState(e.getModifiers());
    305294        // Other buttons are off limit, but we still get events.
    306295        if (e.getButton() != MouseEvent.BUTTON1)
     
    327316    @Override
    328317    public void mouseReleased(MouseEvent e) {
    329         updateModifiersState(e);
     318        updateModifiersState(e.getModifiers());
    330319        // Other buttons are off limit, but we still get events.
    331320        if (e.getButton() != MouseEvent.BUTTON1)
     
    382371            return;
    383372
    384         boolean modifiersChanged = updateModifiersState(e);
     373        boolean modifiersChanged = updateModifiersState(e.getModifiers());
    385374        updateFlagsChangeableAlways();
    386375
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r7009 r7217  
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
    8 import java.awt.AWTEvent;
    98import java.awt.Cursor;
    109import java.awt.Point;
    1110import java.awt.Rectangle;
    12 import java.awt.Toolkit;
    13 import java.awt.event.AWTEventListener;
    1411import java.awt.event.ActionEvent;
    1512import java.awt.event.ActionListener;
    16 import java.awt.event.InputEvent;
    1713import java.awt.event.KeyEvent;
    1814import java.awt.event.MouseEvent;
     
    5349import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    5450import org.openstreetmap.josm.gui.util.GuiHelper;
     51import org.openstreetmap.josm.gui.util.ModifierListener;
    5552import org.openstreetmap.josm.tools.ImageProvider;
    5653import org.openstreetmap.josm.tools.Pair;
     
    6865 * feature "selection remove" is disabled on this platform.
    6966 */
    70 public class SelectAction extends MapMode implements AWTEventListener, SelectionEnded {
     67public class SelectAction extends MapMode implements ModifierListener, SelectionEnded {
    7168    // "select" means the selection rectangle and "move" means either dragging
    7269    // or select if no mouse movement occurs (i.e. just clicking)
     
    188185        virtualManager.init();
    189186        // This is required to update the cursors when ctrl/shift/alt is pressed
    190         try {
    191             Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
    192         } catch (SecurityException ex) {
    193             Main.warn(ex);
    194         }
     187        Main.map.keyDetector.addModifierListener(this);
    195188    }
    196189
     
    202195        mv.removeMouseMotionListener(this);
    203196        mv.setVirtualNodesEnabled(false);
    204         try {
    205             Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    206         } catch (SecurityException ex) {
    207             Main.warn(ex);
    208         }
     197        Main.map.keyDetector.removeModifierListener(this);
    209198        removeHighlighting();
    210199    }
     
    212201    int previousModifiers;
    213202
    214      /**
    215      * This is called whenever the keyboard modifier status changes
    216      */
    217203    @Override
    218     public void eventDispatched(AWTEvent e) {
    219         if(oldEvent == null)
    220             return;
    221         // We don't have a mouse event, so we pass the old mouse event but the
    222         // new modifiers.
    223         int modif = ((InputEvent) e).getModifiers();
    224         if (previousModifiers == modif)
    225             return;
    226         previousModifiers = modif;
    227         if(giveUserFeedback(oldEvent, ((InputEvent) e).getModifiers())) {
     204    public void modifiersChanged(int modifiers) {
     205        if (!Main.isDisplayingMapView() || oldEvent==null) return;
     206        if(giveUserFeedback(oldEvent, modifiers)) {
    228207            mv.repaint();
    229208        }
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r7075 r7217  
    7373import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;
    7474import org.openstreetmap.josm.gui.layer.Layer;
     75import org.openstreetmap.josm.gui.util.AdvancedKeyPressDetector;
    7576import org.openstreetmap.josm.tools.Destroyable;
    7677import org.openstreetmap.josm.tools.GBC;
     
    9596     */
    9697    public final MapView mapView;
     98
     99    /**
     100     * This object allows to detect key press and release events
     101     */
     102    public final AdvancedKeyPressDetector keyDetector = new AdvancedKeyPressDetector();
    97103
    98104    /**
     
    245251            for (JComponent c: allMapModeButtons) c.setFocusTraversalKeysEnabled(false);
    246252        }
     253
     254        keyDetector.register();
    247255    }
    248256
     
    290298        statusLine.destroy();
    291299        mapView.destroy();
     300        keyDetector.unregister();
    292301    }
    293302
Note: See TracChangeset for help on using the changeset viewer.