1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.AWTEvent;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.KeyboardFocusManager;
|
---|
9 | import java.awt.Toolkit;
|
---|
10 | import java.awt.event.AWTEventListener;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Set;
|
---|
14 | import java.util.TreeSet;
|
---|
15 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
16 |
|
---|
17 | import javax.swing.JFrame;
|
---|
18 | import javax.swing.SwingUtilities;
|
---|
19 | import javax.swing.Timer;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.tools.ListenerList;
|
---|
22 | import org.openstreetmap.josm.tools.Logging;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Helper object that allows cross-platform detection of key press and release events
|
---|
26 | * instance is available globally as {@code Main.map.keyDetector}.
|
---|
27 | * @since 7217
|
---|
28 | */
|
---|
29 | public class AdvancedKeyPressDetector implements AWTEventListener {
|
---|
30 |
|
---|
31 | // events for crossplatform key holding processing
|
---|
32 | // thanks to http://www.arco.in-berlin.de/keyevent.html
|
---|
33 | private final Set<Integer> set = new TreeSet<>();
|
---|
34 | private KeyEvent releaseEvent;
|
---|
35 | private Timer timer;
|
---|
36 |
|
---|
37 | private final List<KeyPressReleaseListener> keyListeners = new CopyOnWriteArrayList<>();
|
---|
38 | private final ListenerList<ModifierExListener> modifierExListeners = ListenerList.create();
|
---|
39 | private int previousModifiersEx;
|
---|
40 |
|
---|
41 | private boolean enabled = true;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Adds an object that wants to receive key press and release events.
|
---|
45 | * @param l listener to add
|
---|
46 | */
|
---|
47 | public void addKeyListener(KeyPressReleaseListener l) {
|
---|
48 | keyListeners.add(l);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Adds an object that wants to receive extended key modifier changed events.
|
---|
53 | * @param l listener to add
|
---|
54 | * @since 12517
|
---|
55 | */
|
---|
56 | public void addModifierExListener(ModifierExListener l) {
|
---|
57 | modifierExListeners.addListener(l);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Removes the listener.
|
---|
62 | * @param l listener to remove
|
---|
63 | */
|
---|
64 | public void removeKeyListener(KeyPressReleaseListener l) {
|
---|
65 | keyListeners.remove(l);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Removes the extended key modifier listener.
|
---|
70 | * @param l listener to remove
|
---|
71 | * @since 12517
|
---|
72 | */
|
---|
73 | public void removeModifierExListener(ModifierExListener l) {
|
---|
74 | modifierExListeners.removeListener(l);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Register this object as AWTEventListener
|
---|
79 | */
|
---|
80 | public void register() {
|
---|
81 | try {
|
---|
82 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
---|
83 | } catch (SecurityException ex) {
|
---|
84 | Logging.warn(ex);
|
---|
85 | }
|
---|
86 | timer = new Timer(0, e -> {
|
---|
87 | timer.stop();
|
---|
88 | if (set.remove(releaseEvent.getKeyCode()) && enabled && isFocusInMainWindow()) {
|
---|
89 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
90 | q.doKeyReleased(releaseEvent);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | });
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Unregister this object as AWTEventListener
|
---|
98 | * lists of listeners are not cleared!
|
---|
99 | */
|
---|
100 | public void unregister() {
|
---|
101 | if (timer != null) {
|
---|
102 | timer.stop();
|
---|
103 | }
|
---|
104 | set.clear();
|
---|
105 | releaseEvent = null;
|
---|
106 | if (!keyListeners.isEmpty()) {
|
---|
107 | Logging.warn(tr("Some of the key listeners forgot to remove themselves: {0}"), keyListeners.toString());
|
---|
108 | }
|
---|
109 | if (modifierExListeners.hasListeners()) {
|
---|
110 | Logging.warn(tr("Some of the key modifier listeners forgot to remove themselves: {0}"), modifierExListeners.toString());
|
---|
111 | }
|
---|
112 | try {
|
---|
113 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
---|
114 | } catch (SecurityException ex) {
|
---|
115 | Logging.warn(ex);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void processKeyEvent(KeyEvent e) {
|
---|
120 | if (Logging.isTraceEnabled()) {
|
---|
121 | Logging.trace("AdvancedKeyPressDetector enabled={0} => processKeyEvent({1}) from {2}",
|
---|
122 | enabled, e, new Exception().getStackTrace()[2]);
|
---|
123 | }
|
---|
124 | if (e.getID() == KeyEvent.KEY_PRESSED) {
|
---|
125 | if (timer.isRunning()) {
|
---|
126 | timer.stop();
|
---|
127 | } else if (set.add(e.getKeyCode()) && enabled && isFocusInMainWindow()) {
|
---|
128 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
129 | Logging.trace("{0} => doKeyPressed({1})", q, e);
|
---|
130 | q.doKeyPressed(e);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | } else if (e.getID() == KeyEvent.KEY_RELEASED) {
|
---|
134 | if (timer.isRunning()) {
|
---|
135 | timer.stop();
|
---|
136 | if (set.remove(e.getKeyCode()) && enabled && isFocusInMainWindow()) {
|
---|
137 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
138 | Logging.trace("{0} => doKeyReleased({1})", q, e);
|
---|
139 | q.doKeyReleased(e);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | } else {
|
---|
143 | releaseEvent = e;
|
---|
144 | timer.restart();
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void eventDispatched(AWTEvent e) {
|
---|
151 | if (!(e instanceof KeyEvent)) {
|
---|
152 | return;
|
---|
153 | }
|
---|
154 | KeyEvent ke = (KeyEvent) e;
|
---|
155 |
|
---|
156 | // check if ctrl, alt, shift extended modifiers are changed
|
---|
157 | int modifEx = ke.getModifiersEx();
|
---|
158 | if (previousModifiersEx != modifEx) {
|
---|
159 | previousModifiersEx = modifEx;
|
---|
160 | modifierExListeners.fireEvent(m -> m.modifiersExChanged(modifEx));
|
---|
161 | }
|
---|
162 |
|
---|
163 | processKeyEvent(ke);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Allows to determine if the key with specific code is pressed now
|
---|
168 | * @param keyCode the key code, for example KeyEvent.VK_ENTER
|
---|
169 | * @return true if the key is pressed now
|
---|
170 | */
|
---|
171 | public boolean isKeyPressed(int keyCode) {
|
---|
172 | return set.contains(keyCode);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Sets the enabled state of the key detector. We need to disable it when text fields that disable
|
---|
177 | * shortcuts gain focus.
|
---|
178 | * @param enabled if {@code true}, enables this key detector. If {@code false}, disables it
|
---|
179 | * @since 7539
|
---|
180 | */
|
---|
181 | public final void setEnabled(boolean enabled) {
|
---|
182 | this.enabled = enabled;
|
---|
183 | if (Logging.isTraceEnabled()) {
|
---|
184 | Logging.trace("AdvancedKeyPressDetector enabled={0} from {1}", enabled, new Exception().getStackTrace()[1]);
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | private static boolean isFocusInMainWindow() {
|
---|
189 | Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
|
---|
190 | return focused != null && SwingUtilities.getWindowAncestor(focused) instanceof JFrame;
|
---|
191 | }
|
---|
192 | }
|
---|