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.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.TreeSet;
|
---|
17 |
|
---|
18 | import javax.swing.JFrame;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 | import javax.swing.Timer;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Helper object that allows cross-platform detection of key press and realease events
|
---|
26 | * instance is available globally as 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 ArrayList<KeyPressReleaseListener> keyListeners = new ArrayList<>();
|
---|
38 | private final ArrayList<ModifierListener> modifierListeners = new ArrayList<>();
|
---|
39 | private int previousModifiers;
|
---|
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 synchronized void addKeyListener(KeyPressReleaseListener l) {
|
---|
48 | keyListeners.add(l);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Adds an object that wants to receive key modifier changed events.
|
---|
53 | * @param l listener to add
|
---|
54 | */
|
---|
55 | public synchronized void addModifierListener(ModifierListener l) {
|
---|
56 | modifierListeners.add(l);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Removes the listener.
|
---|
61 | * @param l listener to remove
|
---|
62 | */
|
---|
63 | public synchronized void removeKeyListener(KeyPressReleaseListener l) {
|
---|
64 | keyListeners.remove(l);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Removes the key modifier listener.
|
---|
69 | * @param l listener to remove
|
---|
70 | */
|
---|
71 | public synchronized void removeModifierListener(ModifierListener l) {
|
---|
72 | modifierListeners.remove(l);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Register this object as AWTEventListener
|
---|
77 | */
|
---|
78 | public void register() {
|
---|
79 | try {
|
---|
80 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
---|
81 | } catch (SecurityException ex) {
|
---|
82 | Main.warn(ex);
|
---|
83 | }
|
---|
84 | timer = new Timer(0, new ActionListener() {
|
---|
85 | @Override
|
---|
86 | public void actionPerformed(ActionEvent e) {
|
---|
87 | timer.stop();
|
---|
88 | if (set.remove(releaseEvent.getKeyCode()) && enabled) {
|
---|
89 | synchronized (AdvancedKeyPressDetector.this) {
|
---|
90 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
91 | q.doKeyReleased(releaseEvent);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | });
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Unregister this object as AWTEventListener
|
---|
101 | * lists of listeners are not cleared!
|
---|
102 | */
|
---|
103 | public void unregister() {
|
---|
104 | timer.stop();
|
---|
105 | set.clear();
|
---|
106 | synchronized (this) {
|
---|
107 | if (!keyListeners.isEmpty()) {
|
---|
108 | Main.warn(tr("Some of the key listeners forgot to remove themselves: {0}"), keyListeners.toString());
|
---|
109 | }
|
---|
110 | if (!modifierListeners.isEmpty()) {
|
---|
111 | Main.warn(tr("Some of the key modifier listeners forgot to remove themselves: {0}"), modifierListeners.toString());
|
---|
112 | }
|
---|
113 | }
|
---|
114 | try {
|
---|
115 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
---|
116 | } catch (SecurityException ex) {
|
---|
117 | Main.warn(ex);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void processKeyEvent(KeyEvent e) {
|
---|
122 | if (e.getID() == KeyEvent.KEY_PRESSED) {
|
---|
123 | if (timer.isRunning()) {
|
---|
124 | timer.stop();
|
---|
125 | } else if (set.add((e.getKeyCode())) && enabled) {
|
---|
126 | synchronized (this) {
|
---|
127 | if (isFocusInMainWindow()) {
|
---|
128 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
129 | q.doKeyPressed(e);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | } else if (e.getID() == KeyEvent.KEY_RELEASED) {
|
---|
135 | if (timer.isRunning()) {
|
---|
136 | timer.stop();
|
---|
137 | if (set.remove(e.getKeyCode()) && enabled) {
|
---|
138 | synchronized (this) {
|
---|
139 | if (isFocusInMainWindow()) {
|
---|
140 | for (KeyPressReleaseListener q: keyListeners) {
|
---|
141 | q.doKeyReleased(e);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | } else {
|
---|
147 | releaseEvent = e;
|
---|
148 | timer.restart();
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public void eventDispatched(AWTEvent e) {
|
---|
155 | if (!(e instanceof KeyEvent)) {
|
---|
156 | return;
|
---|
157 | }
|
---|
158 | KeyEvent ke = (KeyEvent) e;
|
---|
159 |
|
---|
160 | // check if ctrl, alt, shift modifiers are changed
|
---|
161 | int modif = ke.getModifiers();
|
---|
162 | if (previousModifiers != modif) {
|
---|
163 | previousModifiers = modif;
|
---|
164 | synchronized (this) {
|
---|
165 | for (ModifierListener m: modifierListeners) {
|
---|
166 | m.modifiersChanged(modif);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | processKeyEvent(ke);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Allows to determine if the key with specific code is pressed now
|
---|
176 | * @param keyCode the key code, for example KeyEvent.VK_ENTER
|
---|
177 | * @return true if the key is pressed now
|
---|
178 | */
|
---|
179 | public boolean isKeyPressed(int keyCode) {
|
---|
180 | return set.contains(keyCode);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Sets the enabled state of the key detector. We need to disable it when text fields that disable
|
---|
185 | * shortcuts gain focus.
|
---|
186 | * @param enabled if {@code true}, enables this key detector. If {@code false}, disables it
|
---|
187 | * @since 7539
|
---|
188 | */
|
---|
189 | public final void setEnabled(boolean enabled) {
|
---|
190 | this.enabled = enabled;
|
---|
191 | }
|
---|
192 |
|
---|
193 | private boolean isFocusInMainWindow() {
|
---|
194 | Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
|
---|
195 | return SwingUtilities.getWindowAncestor(focused) instanceof JFrame;
|
---|
196 | }
|
---|
197 | }
|
---|