author | ant |
Fri, 31 Jan 2014 18:49:58 +0400 | |
changeset 23280 | df31f522531f |
parent 22574 | 7f8ce0c8c20a |
child 25201 | 4adc75e0c4e5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing; |
|
26 |
||
27 |
import java.awt.AWTKeyStroke; |
|
28 |
import java.awt.event.KeyEvent; |
|
29 |
||
30 |
/** |
|
31 |
* A KeyStroke represents a key action on the keyboard, or equivalent input |
|
32 |
* device. KeyStrokes can correspond to only a press or release of a particular |
|
33 |
* key, just as KEY_PRESSED and KEY_RELEASED KeyEvents do; alternately, they |
|
34 |
* can correspond to typing a specific Java character, just as KEY_TYPED |
|
35 |
* KeyEvents do. In all cases, KeyStrokes can specify modifiers (alt, shift, |
|
36 |
* control, meta, altGraph, or a combination thereof) which must be present during the |
|
37 |
* action for an exact match. |
|
38 |
* <p> |
|
39 |
* KeyStrokes are used to define high-level (semantic) action events. Instead |
|
40 |
* of trapping every keystroke and throwing away the ones you are not |
|
41 |
* interested in, those keystrokes you care about automatically initiate |
|
42 |
* actions on the Components with which they are registered. |
|
43 |
* <p> |
|
44 |
* KeyStrokes are immutable, and are intended to be unique. Client code cannot |
|
45 |
* create a KeyStroke; a variant of <code>getKeyStroke</code> must be used |
|
46 |
* instead. These factory methods allow the KeyStroke implementation to cache |
|
47 |
* and share instances efficiently. |
|
48 |
* <p> |
|
49 |
* <strong>Warning:</strong> |
|
50 |
* Serialized objects of this class will not be compatible with |
|
51 |
* future Swing releases. The current serialization support is |
|
52 |
* appropriate for short term storage or RMI between applications running |
|
53 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 54 |
* of all JavaBeans™ |
2 | 55 |
* has been added to the <code>java.beans</code> package. |
56 |
* Please see {@link java.beans.XMLEncoder}. |
|
57 |
* |
|
58 |
* @see javax.swing.text.Keymap |
|
59 |
* @see #getKeyStroke |
|
60 |
* |
|
61 |
* @author Arnaud Weber |
|
62 |
* @author David Mendenhall |
|
63 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
64 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 65 |
public class KeyStroke extends AWTKeyStroke { |
66 |
||
67 |
/** |
|
68 |
* Serial Version ID. |
|
69 |
*/ |
|
70 |
private static final long serialVersionUID = -9060180771037902530L; |
|
71 |
||
72 |
private KeyStroke() { |
|
73 |
} |
|
74 |
private KeyStroke(char keyChar, int keyCode, int modifiers, |
|
75 |
boolean onKeyRelease) { |
|
76 |
super(keyChar, keyCode, modifiers, onKeyRelease); |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Returns a shared instance of a <code>KeyStroke</code> |
|
81 |
* that represents a <code>KEY_TYPED</code> event for the |
|
82 |
* specified character. |
|
83 |
* |
|
84 |
* @param keyChar the character value for a keyboard key |
|
85 |
* @return a KeyStroke object for that key |
|
86 |
*/ |
|
87 |
public static KeyStroke getKeyStroke(char keyChar) { |
|
88 |
synchronized (AWTKeyStroke.class) { |
|
89 |
registerSubclass(KeyStroke.class); |
|
90 |
return (KeyStroke)getAWTKeyStroke(keyChar); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Returns an instance of a KeyStroke, specifying whether the key is |
|
96 |
* considered to be activated when it is pressed or released. Unlike all |
|
97 |
* other factory methods in this class, the instances returned by this |
|
98 |
* method are not necessarily cached or shared. |
|
99 |
* |
|
100 |
* @param keyChar the character value for a keyboard key |
|
101 |
* @param onKeyRelease <code>true</code> if this KeyStroke corresponds to a |
|
102 |
* key release; <code>false</code> otherwise. |
|
103 |
* @return a KeyStroke object for that key |
|
104 |
* @deprecated use getKeyStroke(char) |
|
105 |
*/ |
|
106 |
@Deprecated |
|
107 |
public static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease) { |
|
108 |
return new KeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, onKeyRelease); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Returns a shared instance of a {@code KeyStroke} |
|
113 |
* that represents a {@code KEY_TYPED} event for the |
|
114 |
* specified Character object and a |
|
115 |
* set of modifiers. Note that the first parameter is of type Character |
|
116 |
* rather than char. This is to avoid inadvertent clashes with calls to |
|
117 |
* <code>getKeyStroke(int keyCode, int modifiers)</code>. |
|
118 |
* |
|
119 |
* The modifiers consist of any combination of following:<ul> |
|
120 |
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK |
|
121 |
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK |
|
122 |
* <li>java.awt.event.InputEvent.META_DOWN_MASK |
|
123 |
* <li>java.awt.event.InputEvent.ALT_DOWN_MASK |
|
124 |
* <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK |
|
125 |
* </ul> |
|
126 |
* The old modifiers listed below also can be used, but they are |
|
127 |
* mapped to _DOWN_ modifiers. <ul> |
|
128 |
* <li>java.awt.event.InputEvent.SHIFT_MASK |
|
129 |
* <li>java.awt.event.InputEvent.CTRL_MASK |
|
130 |
* <li>java.awt.event.InputEvent.META_MASK |
|
131 |
* <li>java.awt.event.InputEvent.ALT_MASK |
|
132 |
* <li>java.awt.event.InputEvent.ALT_GRAPH_MASK |
|
133 |
* </ul> |
|
134 |
* also can be used, but they are mapped to _DOWN_ modifiers. |
|
135 |
* |
|
136 |
* Since these numbers are all different powers of two, any combination of |
|
137 |
* them is an integer in which each bit represents a different modifier |
|
138 |
* key. Use 0 to specify no modifiers. |
|
139 |
* |
|
140 |
* @param keyChar the Character object for a keyboard character |
|
141 |
* @param modifiers a bitwise-ored combination of any modifiers |
|
142 |
* @return an KeyStroke object for that key |
|
143 |
* @throws IllegalArgumentException if keyChar is null |
|
144 |
* |
|
145 |
* @see java.awt.event.InputEvent |
|
146 |
* @since 1.3 |
|
147 |
*/ |
|
148 |
public static KeyStroke getKeyStroke(Character keyChar, int modifiers) { |
|
149 |
synchronized (AWTKeyStroke.class) { |
|
150 |
registerSubclass(KeyStroke.class); |
|
151 |
return (KeyStroke)getAWTKeyStroke(keyChar, modifiers); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Returns a shared instance of a KeyStroke, given a numeric key code and a |
|
157 |
* set of modifiers, specifying whether the key is activated when it is |
|
158 |
* pressed or released. |
|
159 |
* <p> |
|
160 |
* The "virtual key" constants defined in java.awt.event.KeyEvent can be |
|
161 |
* used to specify the key code. For example:<ul> |
|
162 |
* <li>java.awt.event.KeyEvent.VK_ENTER |
|
163 |
* <li>java.awt.event.KeyEvent.VK_TAB |
|
164 |
* <li>java.awt.event.KeyEvent.VK_SPACE |
|
165 |
* </ul> |
|
2473
3f4bbd3be2f1
6680988: KeyEvent is still missing VK values for many keyboards
yan
parents:
2
diff
changeset
|
166 |
* Alternatively, the key code may be obtained by calling |
3f4bbd3be2f1
6680988: KeyEvent is still missing VK values for many keyboards
yan
parents:
2
diff
changeset
|
167 |
* <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code>. |
3f4bbd3be2f1
6680988: KeyEvent is still missing VK values for many keyboards
yan
parents:
2
diff
changeset
|
168 |
* |
2 | 169 |
* The modifiers consist of any combination of:<ul> |
170 |
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK |
|
171 |
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK |
|
172 |
* <li>java.awt.event.InputEvent.META_DOWN_MASK |
|
173 |
* <li>java.awt.event.InputEvent.ALT_DOWN_MASK |
|
174 |
* <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK |
|
175 |
* </ul> |
|
176 |
* The old modifiers <ul> |
|
177 |
* <li>java.awt.event.InputEvent.SHIFT_MASK |
|
178 |
* <li>java.awt.event.InputEvent.CTRL_MASK |
|
179 |
* <li>java.awt.event.InputEvent.META_MASK |
|
180 |
* <li>java.awt.event.InputEvent.ALT_MASK |
|
181 |
* <li>java.awt.event.InputEvent.ALT_GRAPH_MASK |
|
182 |
* </ul> |
|
183 |
* also can be used, but they are mapped to _DOWN_ modifiers. |
|
184 |
* |
|
185 |
* Since these numbers are all different powers of two, any combination of |
|
186 |
* them is an integer in which each bit represents a different modifier |
|
187 |
* key. Use 0 to specify no modifiers. |
|
188 |
* |
|
189 |
* @param keyCode an int specifying the numeric code for a keyboard key |
|
190 |
* @param modifiers a bitwise-ored combination of any modifiers |
|
191 |
* @param onKeyRelease <code>true</code> if the KeyStroke should represent |
|
192 |
* a key release; <code>false</code> otherwise. |
|
193 |
* @return a KeyStroke object for that key |
|
194 |
* |
|
195 |
* @see java.awt.event.KeyEvent |
|
196 |
* @see java.awt.event.InputEvent |
|
197 |
*/ |
|
198 |
public static KeyStroke getKeyStroke(int keyCode, int modifiers, |
|
199 |
boolean onKeyRelease) { |
|
200 |
synchronized (AWTKeyStroke.class) { |
|
201 |
registerSubclass(KeyStroke.class); |
|
202 |
return (KeyStroke)getAWTKeyStroke(keyCode, modifiers, |
|
203 |
onKeyRelease); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Returns a shared instance of a KeyStroke, given a numeric key code and a |
|
209 |
* set of modifiers. The returned KeyStroke will correspond to a key press. |
|
210 |
* <p> |
|
211 |
* The "virtual key" constants defined in java.awt.event.KeyEvent can be |
|
212 |
* used to specify the key code. For example:<ul> |
|
213 |
* <li>java.awt.event.KeyEvent.VK_ENTER |
|
214 |
* <li>java.awt.event.KeyEvent.VK_TAB |
|
215 |
* <li>java.awt.event.KeyEvent.VK_SPACE |
|
216 |
* </ul> |
|
2473
3f4bbd3be2f1
6680988: KeyEvent is still missing VK values for many keyboards
yan
parents:
2
diff
changeset
|
217 |
* Alternatively, the key code may be obtained by calling |
3f4bbd3be2f1
6680988: KeyEvent is still missing VK values for many keyboards
yan
parents:
2
diff
changeset
|
218 |
* <code>java.awt.event.KeyEvent.getExtendedKeyCodeForChar</code>. |
3f4bbd3be2f1
6680988: KeyEvent is still missing VK values for many keyboards
yan
parents:
2
diff
changeset
|
219 |
* |
2 | 220 |
* The modifiers consist of any combination of:<ul> |
221 |
* <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK |
|
222 |
* <li>java.awt.event.InputEvent.CTRL_DOWN_MASK |
|
223 |
* <li>java.awt.event.InputEvent.META_DOWN_MASK |
|
224 |
* <li>java.awt.event.InputEvent.ALT_DOWN_MASK |
|
225 |
* <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK |
|
226 |
* </ul> |
|
227 |
* The old modifiers <ul> |
|
228 |
* <li>java.awt.event.InputEvent.SHIFT_MASK |
|
229 |
* <li>java.awt.event.InputEvent.CTRL_MASK |
|
230 |
* <li>java.awt.event.InputEvent.META_MASK |
|
231 |
* <li>java.awt.event.InputEvent.ALT_MASK |
|
232 |
* <li>java.awt.event.InputEvent.ALT_GRAPH_MASK |
|
233 |
* </ul> |
|
234 |
* also can be used, but they are mapped to _DOWN_ modifiers. |
|
235 |
* |
|
236 |
* Since these numbers are all different powers of two, any combination of |
|
237 |
* them is an integer in which each bit represents a different modifier |
|
238 |
* key. Use 0 to specify no modifiers. |
|
239 |
* |
|
240 |
* @param keyCode an int specifying the numeric code for a keyboard key |
|
241 |
* @param modifiers a bitwise-ored combination of any modifiers |
|
242 |
* @return a KeyStroke object for that key |
|
243 |
* |
|
244 |
* @see java.awt.event.KeyEvent |
|
245 |
* @see java.awt.event.InputEvent |
|
246 |
*/ |
|
247 |
public static KeyStroke getKeyStroke(int keyCode, int modifiers) { |
|
248 |
synchronized (AWTKeyStroke.class) { |
|
249 |
registerSubclass(KeyStroke.class); |
|
250 |
return (KeyStroke)getAWTKeyStroke(keyCode, modifiers); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns a KeyStroke which represents the stroke which generated a given |
|
256 |
* KeyEvent. |
|
257 |
* <p> |
|
258 |
* This method obtains the keyChar from a KeyTyped event, and the keyCode |
|
259 |
* from a KeyPressed or KeyReleased event. The KeyEvent modifiers are |
|
260 |
* obtained for all three types of KeyEvent. |
|
261 |
* |
|
262 |
* @param anEvent the KeyEvent from which to obtain the KeyStroke |
|
263 |
* @throws NullPointerException if <code>anEvent</code> is null |
|
264 |
* @return the KeyStroke that precipitated the event |
|
265 |
*/ |
|
266 |
public static KeyStroke getKeyStrokeForEvent(KeyEvent anEvent) { |
|
267 |
synchronized (AWTKeyStroke.class) { |
|
268 |
registerSubclass(KeyStroke.class); |
|
269 |
return (KeyStroke)getAWTKeyStrokeForEvent(anEvent); |
|
270 |
} |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Parses a string and returns a <code>KeyStroke</code>. |
|
275 |
* The string must have the following syntax: |
|
276 |
* <pre> |
|
277 |
* <modifiers>* (<typedID> | <pressedReleasedID>) |
|
278 |
* |
|
279 |
* modifiers := shift | control | ctrl | meta | alt | altGraph |
|
280 |
* typedID := typed <typedKey> |
|
281 |
* typedKey := string of length 1 giving Unicode character. |
|
282 |
* pressedReleasedID := (pressed | released) key |
|
283 |
* key := KeyEvent key code name, i.e. the name following "VK_". |
|
284 |
* </pre> |
|
285 |
* If typed, pressed or released is not specified, pressed is assumed. Here |
|
286 |
* are some examples: |
|
287 |
* <pre> |
|
20157
cafca01a8e28
8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents:
5506
diff
changeset
|
288 |
* "INSERT" => getKeyStroke(KeyEvent.VK_INSERT, 0); |
cafca01a8e28
8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents:
5506
diff
changeset
|
289 |
* "control DELETE" => getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK); |
cafca01a8e28
8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents:
5506
diff
changeset
|
290 |
* "alt shift X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK); |
cafca01a8e28
8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents:
5506
diff
changeset
|
291 |
* "alt shift released X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true); |
cafca01a8e28
8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents:
5506
diff
changeset
|
292 |
* "typed a" => getKeyStroke('a'); |
2 | 293 |
* </pre> |
294 |
* |
|
295 |
* In order to maintain backward-compatibility, specifying a null String, |
|
296 |
* or a String which is formatted incorrectly, returns null. |
|
297 |
* |
|
298 |
* @param s a String formatted as described above |
|
299 |
* @return a KeyStroke object for that String, or null if the specified |
|
300 |
* String is null, or is formatted incorrectly |
|
301 |
* |
|
302 |
* @see java.awt.event.KeyEvent |
|
303 |
*/ |
|
304 |
public static KeyStroke getKeyStroke(String s) { |
|
305 |
if (s == null || s.length() == 0) { |
|
306 |
return null; |
|
307 |
} |
|
308 |
synchronized (AWTKeyStroke.class) { |
|
309 |
registerSubclass(KeyStroke.class); |
|
310 |
try { |
|
311 |
return (KeyStroke)getAWTKeyStroke(s); |
|
312 |
} catch (IllegalArgumentException e) { |
|
313 |
return null; |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
} |