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.Main;
|
---|
22 | import org.openstreetmap.josm.tools.ListenerList;
|
---|
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 | /** @deprecated replaced by {@link #modifierExListeners} */
|
---|
39 | @Deprecated
|
---|
40 | private final List<ModifierListener> modifierListeners = new CopyOnWriteArrayList<>();
|
---|
41 | private final ListenerList<ModifierExListener> modifierExListeners = ListenerList.create();
|
---|
42 | /** @deprecated replaced by {@link #previousModifiersEx} */
|
---|
43 | @Deprecated
|
---|
44 | private int previousModifiers;
|
---|
45 | private int previousModifiersEx;
|
---|
46 |
|
---|
47 | private boolean enabled = true;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Adds an object that wants to receive key press and release events.
|
---|
51 | * @param l listener to add
|
---|
52 | */
|
---|
53 | public void addKeyListener(KeyPressReleaseListener l) {
|
---|
54 | keyListeners.add(l);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Adds an object that wants to receive key modifier changed events.
|
---|
59 | * @param l listener to add
|
---|
60 | * @deprecated use {@link #addModifierExListener} instead
|
---|
61 | */
|
---|
62 | @Deprecated
|
---|
63 | public void addModifierListener(ModifierListener l) {
|
---|
64 | modifierListeners.add(l);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Adds an object that wants to receive extended key modifier changed events.
|
---|
69 | * @param l listener to add
|
---|
70 | * @since 12517
|
---|
71 | */
|
---|
72 | public void addModifierExListener(ModifierExListener l) {
|
---|
73 | modifierExListeners.addListener(l);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Removes the listener.
|
---|
78 | * @param l listener to remove
|
---|
79 | */
|
---|
80 | public void removeKeyListener(KeyPressReleaseListener l) {
|
---|
81 | keyListeners.remove(l);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Removes the key modifier listener.
|
---|
86 | * @param l listener to remove
|
---|
87 | * @deprecated use {@link #removeModifierExListener} instead
|
---|
88 | */
|
---|
89 | @Deprecated
|
---|
90 | public void removeModifierListener(ModifierListener l) {
|
---|
91 | modifierListeners.remove(l);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Removes the extended key modifier listener.
|
---|
96 | * @param l listener to remove
|
---|
97 | * @since 12517
|
---|
98 | */
|
---|
99 | public void removeModifierExListener(ModifierExListener l) {
|
---|
100 | modifierExListeners.removeListener(l);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Register this object as AWTEventListener
|
---|
105 | */
|
---|
106 | public void register() {
|
---|
107 | try {
|
---|
108 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
---|
109 | } catch (SecurityException ex) {
|
---|
110 | Main.warn(ex);
|
---|
111 | }
|
---|
112 | timer = new Timer(0, e -> {
|
---|
113 | timer.stop();
|
---|
114 | if (set.remove(releaseEvent.getKeyCode()) && enabled && isFocusInMainWindow()) {
|
---|
115 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
116 | q.doKeyReleased(releaseEvent);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | });
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Unregister this object as AWTEventListener
|
---|
124 | * lists of listeners are not cleared!
|
---|
125 | */
|
---|
126 | public void unregister() {
|
---|
127 | if (timer != null) {
|
---|
128 | timer.stop();
|
---|
129 | }
|
---|
130 | set.clear();
|
---|
131 | if (!keyListeners.isEmpty()) {
|
---|
132 | Main.warn(tr("Some of the key listeners forgot to remove themselves: {0}"), keyListeners.toString());
|
---|
133 | }
|
---|
134 | if (!modifierListeners.isEmpty()) {
|
---|
135 | Main.warn(tr("Some of the key modifier listeners forgot to remove themselves: {0}"), modifierListeners.toString());
|
---|
136 | }
|
---|
137 | if (modifierExListeners.hasListeners()) {
|
---|
138 | Main.warn(tr("Some of the key modifier listeners forgot to remove themselves: {0}"), modifierExListeners.toString());
|
---|
139 | }
|
---|
140 | try {
|
---|
141 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
---|
142 | } catch (SecurityException ex) {
|
---|
143 | Main.warn(ex);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void processKeyEvent(KeyEvent e) {
|
---|
148 | if (Main.isTraceEnabled()) {
|
---|
149 | Main.trace("AdvancedKeyPressDetector enabled="+enabled+" => processKeyEvent("+e+") from "+new Exception().getStackTrace()[2]);
|
---|
150 | }
|
---|
151 | if (e.getID() == KeyEvent.KEY_PRESSED) {
|
---|
152 | if (timer.isRunning()) {
|
---|
153 | timer.stop();
|
---|
154 | } else if (set.add(e.getKeyCode()) && enabled && isFocusInMainWindow()) {
|
---|
155 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
156 | if (Main.isTraceEnabled()) {
|
---|
157 | Main.trace(q+" => doKeyPressed("+e+')');
|
---|
158 | }
|
---|
159 | q.doKeyPressed(e);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | } else if (e.getID() == KeyEvent.KEY_RELEASED) {
|
---|
163 | if (timer.isRunning()) {
|
---|
164 | timer.stop();
|
---|
165 | if (set.remove(e.getKeyCode()) && enabled && isFocusInMainWindow()) {
|
---|
166 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
167 | if (Main.isTraceEnabled()) {
|
---|
168 | Main.trace(q+" => doKeyReleased("+e+')');
|
---|
169 | }
|
---|
170 | q.doKeyReleased(e);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | } else {
|
---|
174 | releaseEvent = e;
|
---|
175 | timer.restart();
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | @SuppressWarnings("deprecation")
|
---|
182 | public void eventDispatched(AWTEvent e) {
|
---|
183 | if (!(e instanceof KeyEvent)) {
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | KeyEvent ke = (KeyEvent) e;
|
---|
187 |
|
---|
188 | // check if ctrl, alt, shift modifiers are changed
|
---|
189 | int modif = ke.getModifiers();
|
---|
190 | if (previousModifiers != modif) {
|
---|
191 | previousModifiers = modif;
|
---|
192 | for (ModifierListener m: modifierListeners) {
|
---|
193 | m.modifiersChanged(modif);
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | // check if ctrl, alt, shift extended modifiers are changed
|
---|
198 | int modifEx = ke.getModifiersEx();
|
---|
199 | if (previousModifiersEx != modifEx) {
|
---|
200 | previousModifiersEx = modifEx;
|
---|
201 | modifierExListeners.fireEvent(m -> m.modifiersExChanged(modifEx));
|
---|
202 | }
|
---|
203 |
|
---|
204 | processKeyEvent(ke);
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Allows to determine if the key with specific code is pressed now
|
---|
209 | * @param keyCode the key code, for example KeyEvent.VK_ENTER
|
---|
210 | * @return true if the key is pressed now
|
---|
211 | */
|
---|
212 | public boolean isKeyPressed(int keyCode) {
|
---|
213 | return set.contains(keyCode);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Sets the enabled state of the key detector. We need to disable it when text fields that disable
|
---|
218 | * shortcuts gain focus.
|
---|
219 | * @param enabled if {@code true}, enables this key detector. If {@code false}, disables it
|
---|
220 | * @since 7539
|
---|
221 | */
|
---|
222 | public final void setEnabled(boolean enabled) {
|
---|
223 | this.enabled = enabled;
|
---|
224 | if (Main.isTraceEnabled()) {
|
---|
225 | Main.trace("AdvancedKeyPressDetector enabled="+enabled+" from "+new Exception().getStackTrace()[1]);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private static boolean isFocusInMainWindow() {
|
---|
230 | Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
|
---|
231 | return focused != null && SwingUtilities.getWindowAncestor(focused) instanceof JFrame;
|
---|
232 | }
|
---|
233 | }
|
---|