1 /*
2 * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
115 * <li>The key that generates {@code VK_Q} when using a U.S. keyboard layout also
116 * generates a unique code for Russian or Hebrew layout. There is no a
117 * {@code VK_} constant for these and many other codes in various layouts. These codes
118 * may be obtained by using {@code getExtendedKeyCode} and are used whenever
119 * a {@code VK_} constant is used.
120 * <li>Not all characters have a keycode associated with them. For example,
121 * there is no keycode for the question mark because there is no keyboard
122 * for which it appears on the primary layer.
123 * <li>In order to support the platform-independent handling of action keys,
124 * the Java platform uses a few additional virtual key constants for functions
125 * that would otherwise have to be recognized by interpreting virtual key codes
126 * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
127 * is returned instead of VK_CONVERT with the ALT modifier.
128 * <li>As specified in <a href="../doc-files/FocusSpec.html">Focus Specification</a>
129 * key events are dispatched to the focus owner by default.
130 * </ul>
131 *
132 * <p>
133 * WARNING: Aside from those keys that are defined by the Java language
134 * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
135 * constants. Sun reserves the right to change these values as needed
136 * to accommodate a wider range of keyboards in the future.
137 * <p>
138 * An unspecified behavior will be caused if the {@code id} parameter
139 * of any particular {@code KeyEvent} instance is not
140 * in the range from {@code KEY_FIRST} to {@code KEY_LAST}.
141 *
142 * @author Carl Quinn
143 * @author Amy Fowler
144 * @author Norbert Lindenberg
145 *
146 * @see KeyAdapter
147 * @see KeyListener
148 * @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html">Tutorial: Writing a Key Listener</a>
149 *
150 * @since 1.1
151 */
152 public class KeyEvent extends InputEvent {
153
154 /**
155 * Stores the state of native event dispatching system
177 * Often, however, characters are produced by series of key presses, and
178 * the mapping from key pressed events to key typed events may be
179 * many-to-one or many-to-many.
180 */
181 public static final int KEY_TYPED = KEY_FIRST;
182
183 /**
184 * The "key pressed" event. This event is generated when a key
185 * is pushed down.
186 */
187 public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
188
189 /**
190 * The "key released" event. This event is generated when a key
191 * is let up.
192 */
193 public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
194
195 /* Virtual key codes. */
196
197 public static final int VK_ENTER = '\n';
198 public static final int VK_BACK_SPACE = '\b';
199 public static final int VK_TAB = '\t';
200 public static final int VK_CANCEL = 0x03;
201 public static final int VK_CLEAR = 0x0C;
202 public static final int VK_SHIFT = 0x10;
203 public static final int VK_CONTROL = 0x11;
204 public static final int VK_ALT = 0x12;
205 public static final int VK_PAUSE = 0x13;
206 public static final int VK_CAPS_LOCK = 0x14;
207 public static final int VK_ESCAPE = 0x1B;
208 public static final int VK_SPACE = 0x20;
209 public static final int VK_PAGE_UP = 0x21;
210 public static final int VK_PAGE_DOWN = 0x22;
211 public static final int VK_END = 0x23;
212 public static final int VK_HOME = 0x24;
213
214 /**
215 * Constant for the non-numpad <b>left</b> arrow key.
216 * @see #VK_KP_LEFT
217 */
218 public static final int VK_LEFT = 0x25;
219
220 /**
221 * Constant for the non-numpad <b>up</b> arrow key.
222 * @see #VK_KP_UP
223 */
224 public static final int VK_UP = 0x26;
225
226 /**
227 * Constant for the non-numpad <b>right</b> arrow key.
228 * @see #VK_KP_RIGHT
229 */
230 public static final int VK_RIGHT = 0x27;
231
240 */
241 public static final int VK_COMMA = 0x2C;
242
243 /**
244 * Constant for the minus key, "-"
245 * @since 1.2
246 */
247 public static final int VK_MINUS = 0x2D;
248
249 /**
250 * Constant for the period key, "."
251 */
252 public static final int VK_PERIOD = 0x2E;
253
254 /**
255 * Constant for the forward slash key, "/"
256 */
257 public static final int VK_SLASH = 0x2F;
258
259 /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
260 public static final int VK_0 = 0x30;
261 public static final int VK_1 = 0x31;
262 public static final int VK_2 = 0x32;
263 public static final int VK_3 = 0x33;
264 public static final int VK_4 = 0x34;
265 public static final int VK_5 = 0x35;
266 public static final int VK_6 = 0x36;
267 public static final int VK_7 = 0x37;
268 public static final int VK_8 = 0x38;
269 public static final int VK_9 = 0x39;
270
271 /**
272 * Constant for the semicolon key, ";"
273 */
274 public static final int VK_SEMICOLON = 0x3B;
275
276 /**
277 * Constant for the equals key, "="
278 */
279 public static final int VK_EQUALS = 0x3D;
280
281 /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
282 public static final int VK_A = 0x41;
283 public static final int VK_B = 0x42;
284 public static final int VK_C = 0x43;
285 public static final int VK_D = 0x44;
286 public static final int VK_E = 0x45;
287 public static final int VK_F = 0x46;
288 public static final int VK_G = 0x47;
289 public static final int VK_H = 0x48;
290 public static final int VK_I = 0x49;
291 public static final int VK_J = 0x4A;
292 public static final int VK_K = 0x4B;
293 public static final int VK_L = 0x4C;
294 public static final int VK_M = 0x4D;
295 public static final int VK_N = 0x4E;
296 public static final int VK_O = 0x4F;
297 public static final int VK_P = 0x50;
298 public static final int VK_Q = 0x51;
299 public static final int VK_R = 0x52;
300 public static final int VK_S = 0x53;
301 public static final int VK_T = 0x54;
302 public static final int VK_U = 0x55;
303 public static final int VK_V = 0x56;
304 public static final int VK_W = 0x57;
305 public static final int VK_X = 0x58;
306 public static final int VK_Y = 0x59;
307 public static final int VK_Z = 0x5A;
308
309 /**
310 * Constant for the open bracket key, "["
311 */
312 public static final int VK_OPEN_BRACKET = 0x5B;
313
314 /**
315 * Constant for the back slash key, "\"
316 */
317 public static final int VK_BACK_SLASH = 0x5C;
318
319 /**
320 * Constant for the close bracket key, "]"
321 */
322 public static final int VK_CLOSE_BRACKET = 0x5D;
323
324 public static final int VK_NUMPAD0 = 0x60;
325 public static final int VK_NUMPAD1 = 0x61;
326 public static final int VK_NUMPAD2 = 0x62;
327 public static final int VK_NUMPAD3 = 0x63;
328 public static final int VK_NUMPAD4 = 0x64;
329 public static final int VK_NUMPAD5 = 0x65;
330 public static final int VK_NUMPAD6 = 0x66;
331 public static final int VK_NUMPAD7 = 0x67;
332 public static final int VK_NUMPAD8 = 0x68;
333 public static final int VK_NUMPAD9 = 0x69;
334 public static final int VK_MULTIPLY = 0x6A;
335 public static final int VK_ADD = 0x6B;
336
337 /**
338 * This constant is obsolete, and is included only for backwards
339 * compatibility.
340 * @see #VK_SEPARATOR
341 */
342 public static final int VK_SEPARATER = 0x6C;
343
344 /**
345 * Constant for the Numpad Separator key.
346 * @since 1.4
347 */
348 public static final int VK_SEPARATOR = VK_SEPARATER;
349
350 public static final int VK_SUBTRACT = 0x6D;
351 public static final int VK_DECIMAL = 0x6E;
352 public static final int VK_DIVIDE = 0x6F;
353 public static final int VK_DELETE = 0x7F; /* ASCII DEL */
354 public static final int VK_NUM_LOCK = 0x90;
355 public static final int VK_SCROLL_LOCK = 0x91;
356
357 /** Constant for the F1 function key. */
358 public static final int VK_F1 = 0x70;
359
360 /** Constant for the F2 function key. */
361 public static final int VK_F2 = 0x71;
362
363 /** Constant for the F3 function key. */
364 public static final int VK_F3 = 0x72;
365
366 /** Constant for the F4 function key. */
367 public static final int VK_F4 = 0x73;
368
369 /** Constant for the F5 function key. */
370 public static final int VK_F5 = 0x74;
371
372 /** Constant for the F6 function key. */
373 public static final int VK_F6 = 0x75;
374
446 public static final int VK_F21 = 0xF008;
447
448 /**
449 * Constant for the F22 function key.
450 * @since 1.2
451 */
452 public static final int VK_F22 = 0xF009;
453
454 /**
455 * Constant for the F23 function key.
456 * @since 1.2
457 */
458 public static final int VK_F23 = 0xF00A;
459
460 /**
461 * Constant for the F24 function key.
462 * @since 1.2
463 */
464 public static final int VK_F24 = 0xF00B;
465
466 public static final int VK_PRINTSCREEN = 0x9A;
467 public static final int VK_INSERT = 0x9B;
468 public static final int VK_HELP = 0x9C;
469 public static final int VK_META = 0x9D;
470
471 public static final int VK_BACK_QUOTE = 0xC0;
472 public static final int VK_QUOTE = 0xDE;
473
474 /**
475 * Constant for the numeric keypad <b>up</b> arrow key.
476 * @see #VK_UP
477 * @since 1.2
478 */
479 public static final int VK_KP_UP = 0xE0;
480
481 /**
482 * Constant for the numeric keypad <b>down</b> arrow key.
483 * @see #VK_DOWN
484 * @since 1.2
485 */
486 public static final int VK_KP_DOWN = 0xE1;
487
488 /**
489 * Constant for the numeric keypad <b>left</b> arrow key.
490 * @see #VK_LEFT
491 * @since 1.2
621 */
622 public static final int VK_UNDERSCORE = 0x020B;
623
624 /**
625 * Constant for the Microsoft Windows "Windows" key.
626 * It is used for both the left and right version of the key.
627 * @see #getKeyLocation()
628 * @since 1.5
629 */
630 public static final int VK_WINDOWS = 0x020C;
631
632 /**
633 * Constant for the Microsoft Windows Context Menu key.
634 * @since 1.5
635 */
636 public static final int VK_CONTEXT_MENU = 0x020D;
637
638 /* for input method support on Asian Keyboards */
639
640 /* not clear what this means - listed in Microsoft Windows API */
641 public static final int VK_FINAL = 0x0018;
642
643 /** Constant for the Convert function key. */
644 /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
645 public static final int VK_CONVERT = 0x001C;
646
647 /** Constant for the Don't Convert function key. */
648 /* Japanese PC 106 keyboard: muhenkan */
649 public static final int VK_NONCONVERT = 0x001D;
650
651 /** Constant for the Accept or Commit function key. */
652 /* Japanese Solaris keyboard: kakutei */
653 public static final int VK_ACCEPT = 0x001E;
654
655 /* not clear what this means - listed in Microsoft Windows API */
656 public static final int VK_MODECHANGE = 0x001F;
657
658 /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
659 might still be used on other platforms */
660 public static final int VK_KANA = 0x0015;
661
662 /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
663 might still be used for other platforms */
664 public static final int VK_KANJI = 0x0019;
665
666 /**
667 * Constant for the Alphanumeric function key.
668 * @since 1.2
669 */
670 /* Japanese PC 106 keyboard: eisuu */
671 public static final int VK_ALPHANUMERIC = 0x00F0;
672
673 /**
674 * Constant for the Katakana function key.
675 * @since 1.2
676 */
677 /* Japanese PC 106 keyboard: katakana */
678 public static final int VK_KATAKANA = 0x00F1;
679
680 /**
681 * Constant for the Hiragana function key.
682 * @since 1.2
683 */
1068 * @throws IllegalArgumentException if <code>id</code> is
1069 * <code>KEY_TYPED</code> and <code>keyChar</code> is
1070 * <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
1071 * <code>KEY_TYPED</code> and <code>keyCode</code> is not
1072 * <code>VK_UNDEFINED</code>
1073 * @throws IllegalArgumentException if <code>source</code> is null
1074 * @see #getSource()
1075 * @see #getID()
1076 * @see #getWhen()
1077 * @see #getModifiers()
1078 * @see #getKeyCode()
1079 * @see #getKeyChar()
1080 */
1081 public KeyEvent(Component source, int id, long when, int modifiers,
1082 int keyCode, char keyChar) {
1083 this(source, id, when, modifiers, keyCode, keyChar,
1084 KEY_LOCATION_UNKNOWN);
1085 }
1086
1087 /**
1088 * @deprecated as of JDK1.1
1089 */
1090 @Deprecated
1091 public KeyEvent(Component source, int id, long when, int modifiers,
1092 int keyCode) {
1093 this(source, id, when, modifiers, keyCode, (char)keyCode);
1094 }
1095
1096 /**
1097 * Returns the integer keyCode associated with the key in this event.
1098 *
1099 * @return the integer code for an actual key on the keyboard.
1100 * (For <code>KEY_TYPED</code> events, the keyCode is
1101 * <code>VK_UNDEFINED</code>.)
1102 */
1103 public int getKeyCode() {
1104 return keyCode;
1105 }
1106
1107 /**
1108 * Set the keyCode value to indicate a physical key.
1167 /**
1168 * Returns the location of the key that originated this key event.
1169 *
1170 * Some keys occur more than once on a keyboard, e.g. the left and
1171 * right shift keys. Additionally, some keys occur on the numeric
1172 * keypad. This provides a way of distinguishing such keys.
1173 *
1174 * @return the location of the key that was pressed or released.
1175 * Always returns <code>KEY_LOCATION_UNKNOWN</code> for
1176 * <code>KEY_TYPED</code> events.
1177 * @since 1.4
1178 */
1179 public int getKeyLocation() {
1180 return keyLocation;
1181 }
1182
1183 /**
1184 * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
1185 * These strings can be localized by changing the awt.properties file.
1186 *
1187 * @return a string containing a text description for a physical key,
1188 * identified by its keyCode
1189 */
1190 public static String getKeyText(int keyCode) {
1191 if (keyCode >= VK_0 && keyCode <= VK_9 ||
1192 keyCode >= VK_A && keyCode <= VK_Z) {
1193 return String.valueOf((char)keyCode);
1194 }
1195
1196 switch(keyCode) {
1197 case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
1198 case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
1199 case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
1200 case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
1201 case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
1202 case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
1203 case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
1204 case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
1205 case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
1206 case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
1359
1360 if ((keyCode & 0x01000000) != 0) {
1361 return String.valueOf((char)(keyCode ^ 0x01000000 ));
1362 }
1363 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1364 return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1365 }
1366
1367 /**
1368 * Returns a <code>String</code> describing the modifier key(s),
1369 * such as "Shift", or "Ctrl+Shift". These strings can be
1370 * localized by changing the <code>awt.properties</code> file.
1371 * <p>
1372 * Note that <code>InputEvent.ALT_MASK</code> and
1373 * <code>InputEvent.BUTTON2_MASK</code> have the same value,
1374 * so the string "Alt" is returned for both modifiers. Likewise,
1375 * <code>InputEvent.META_MASK</code> and
1376 * <code>InputEvent.BUTTON3_MASK</code> have the same value,
1377 * so the string "Meta" is returned for both modifiers.
1378 *
1379 * @return string a text description of the combination of modifier
1380 * keys that were held down during the event
1381 * @see InputEvent#getModifiersExText(int)
1382 */
1383 public static String getKeyModifiersText(int modifiers) {
1384 StringBuilder buf = new StringBuilder();
1385 if ((modifiers & InputEvent.META_MASK) != 0) {
1386 buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1387 buf.append("+");
1388 }
1389 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1390 buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1391 buf.append("+");
1392 }
1393 if ((modifiers & InputEvent.ALT_MASK) != 0) {
1394 buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1395 buf.append("+");
1396 }
1397 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1398 buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1595 default:
1596 str.append("KEY_LOCATION_UNKNOWN");
1597 break;
1598 }
1599 str.append(",rawCode=").append(rawCode);
1600 str.append(",primaryLevelUnicode=").append(primaryLevelUnicode);
1601 str.append(",scancode=").append(scancode);
1602 str.append(",extendedKeyCode=0x").append(Long.toHexString(extendedKeyCode));
1603
1604 return str.toString();
1605 }
1606 /**
1607 * Returns an extended key code for the event.
1608 * The extended key code is a unique id assigned to a key on the keyboard
1609 * just like {@code keyCode}. However, unlike {@code keyCode}, this value depends on the
1610 * current keyboard layout. For instance, pressing the left topmost letter key
1611 * in a common English layout produces the same value as {@code keyCode}, {@code VK_Q}.
1612 * Pressing the same key in a regular Russian layout gives another code, unique for the
1613 * letter "Cyrillic I short".
1614 *
1615 * @since 1.7
1616 *
1617 */
1618 public int getExtendedKeyCode() {
1619 return (int)extendedKeyCode;
1620 }
1621 /**
1622 * Returns an extended key code for a unicode character.
1623 *
1624 * @return for a unicode character with a corresponding {@code VK_} constant -- this
1625 * {@code VK_} constant; for a character appearing on the primary
1626 * level of a known keyboard layout -- a unique integer.
1627 * If a character does not appear on the primary level of a known keyboard,
1628 * {@code VK_UNDEFINED} is returned.
1629 *
1630 * @since 1.7
1631 *
1632 */
1633 public static int getExtendedKeyCodeForChar(int c) {
1634 // Return a keycode (if any) associated with a character.
1635 return sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(c);
1636 }
1637
1638 /**
1639 * Sets new modifiers by the old ones. The key modifiers
1640 * override overlaping mouse modifiers.
1641 */
1642 private void setNewModifiers() {
1643 if ((modifiers & SHIFT_MASK) != 0) {
1644 modifiers |= SHIFT_DOWN_MASK;
1645 }
1646 if ((modifiers & ALT_MASK) != 0) {
1647 modifiers |= ALT_DOWN_MASK;
1648 }
1649 if ((modifiers & CTRL_MASK) != 0) {
1650 modifiers |= CTRL_DOWN_MASK;
1651 }
|
1 /*
2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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
115 * <li>The key that generates {@code VK_Q} when using a U.S. keyboard layout also
116 * generates a unique code for Russian or Hebrew layout. There is no a
117 * {@code VK_} constant for these and many other codes in various layouts. These codes
118 * may be obtained by using {@code getExtendedKeyCode} and are used whenever
119 * a {@code VK_} constant is used.
120 * <li>Not all characters have a keycode associated with them. For example,
121 * there is no keycode for the question mark because there is no keyboard
122 * for which it appears on the primary layer.
123 * <li>In order to support the platform-independent handling of action keys,
124 * the Java platform uses a few additional virtual key constants for functions
125 * that would otherwise have to be recognized by interpreting virtual key codes
126 * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
127 * is returned instead of VK_CONVERT with the ALT modifier.
128 * <li>As specified in <a href="../doc-files/FocusSpec.html">Focus Specification</a>
129 * key events are dispatched to the focus owner by default.
130 * </ul>
131 *
132 * <p>
133 * WARNING: Aside from those keys that are defined by the Java language
134 * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
135 * constants. The platform steward reserves the right to change these values as needed
136 * to accommodate a wider range of keyboards in the future.
137 * <p>
138 * An unspecified behavior will be caused if the {@code id} parameter
139 * of any particular {@code KeyEvent} instance is not
140 * in the range from {@code KEY_FIRST} to {@code KEY_LAST}.
141 *
142 * @author Carl Quinn
143 * @author Amy Fowler
144 * @author Norbert Lindenberg
145 *
146 * @see KeyAdapter
147 * @see KeyListener
148 * @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html">Tutorial: Writing a Key Listener</a>
149 *
150 * @since 1.1
151 */
152 public class KeyEvent extends InputEvent {
153
154 /**
155 * Stores the state of native event dispatching system
177 * Often, however, characters are produced by series of key presses, and
178 * the mapping from key pressed events to key typed events may be
179 * many-to-one or many-to-many.
180 */
181 public static final int KEY_TYPED = KEY_FIRST;
182
183 /**
184 * The "key pressed" event. This event is generated when a key
185 * is pushed down.
186 */
187 public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
188
189 /**
190 * The "key released" event. This event is generated when a key
191 * is let up.
192 */
193 public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
194
195 /* Virtual key codes. */
196
197 /** Constant for the ENTER virtual key. */
198 public static final int VK_ENTER = '\n';
199
200 /** Constant for the BACK_SPACE virtual key. */
201 public static final int VK_BACK_SPACE = '\b';
202
203 /** Constant for the TAB virtual key. */
204 public static final int VK_TAB = '\t';
205
206 /** Constant for the CANCEL virtual key. */
207 public static final int VK_CANCEL = 0x03;
208
209 /** Constant for the CLEAR virtual key. */
210 public static final int VK_CLEAR = 0x0C;
211
212 /** Constant for the SHIFT virtual key. */
213 public static final int VK_SHIFT = 0x10;
214
215 /** Constant for the CONTROL virtual key. */
216 public static final int VK_CONTROL = 0x11;
217
218 /** Constant for the ALT virtual key. */
219 public static final int VK_ALT = 0x12;
220
221 /** Constant for the PAUSE virtual key. */
222 public static final int VK_PAUSE = 0x13;
223
224 /** Constant for the CAPS_LOCK virtual key. */
225 public static final int VK_CAPS_LOCK = 0x14;
226
227 /** Constant for the ESCAPE virtual key. */
228 public static final int VK_ESCAPE = 0x1B;
229
230 /** Constant for the SPACE virtual key. */
231 public static final int VK_SPACE = 0x20;
232
233 /** Constant for the PAGE_UP virtual key. */
234 public static final int VK_PAGE_UP = 0x21;
235
236 /** Constant for the PAGE_DOWN virtual key. */
237 public static final int VK_PAGE_DOWN = 0x22;
238
239 /** Constant for the END virtual key. */
240 public static final int VK_END = 0x23;
241
242 /** Constant for the HOME virtual key. */
243 public static final int VK_HOME = 0x24;
244
245 /**
246 * Constant for the non-numpad <b>left</b> arrow key.
247 * @see #VK_KP_LEFT
248 */
249 public static final int VK_LEFT = 0x25;
250
251 /**
252 * Constant for the non-numpad <b>up</b> arrow key.
253 * @see #VK_KP_UP
254 */
255 public static final int VK_UP = 0x26;
256
257 /**
258 * Constant for the non-numpad <b>right</b> arrow key.
259 * @see #VK_KP_RIGHT
260 */
261 public static final int VK_RIGHT = 0x27;
262
271 */
272 public static final int VK_COMMA = 0x2C;
273
274 /**
275 * Constant for the minus key, "-"
276 * @since 1.2
277 */
278 public static final int VK_MINUS = 0x2D;
279
280 /**
281 * Constant for the period key, "."
282 */
283 public static final int VK_PERIOD = 0x2E;
284
285 /**
286 * Constant for the forward slash key, "/"
287 */
288 public static final int VK_SLASH = 0x2F;
289
290 /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
291
292 /** Constant for the "0" key. */
293 public static final int VK_0 = 0x30;
294
295 /** Constant for the "1" key. */
296 public static final int VK_1 = 0x31;
297
298 /** Constant for the "2" key. */
299 public static final int VK_2 = 0x32;
300
301 /** Constant for the "3" key. */
302 public static final int VK_3 = 0x33;
303
304 /** Constant for the "4" key. */
305 public static final int VK_4 = 0x34;
306
307 /** Constant for the "5" key. */
308 public static final int VK_5 = 0x35;
309
310 /** Constant for the "6" key. */
311 public static final int VK_6 = 0x36;
312
313 /** Constant for the "7" key. */
314 public static final int VK_7 = 0x37;
315
316 /** Constant for the "8" key. */
317 public static final int VK_8 = 0x38;
318
319 /** Constant for the "9" key. */
320 public static final int VK_9 = 0x39;
321
322 /**
323 * Constant for the semicolon key, ";"
324 */
325 public static final int VK_SEMICOLON = 0x3B;
326
327 /**
328 * Constant for the equals key, "="
329 */
330 public static final int VK_EQUALS = 0x3D;
331
332 /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
333
334 /** Constant for the "A" key. */
335 public static final int VK_A = 0x41;
336
337 /** Constant for the "B" key. */
338 public static final int VK_B = 0x42;
339
340 /** Constant for the "C" key. */
341 public static final int VK_C = 0x43;
342
343 /** Constant for the "D" key. */
344 public static final int VK_D = 0x44;
345
346 /** Constant for the "E" key. */
347 public static final int VK_E = 0x45;
348
349 /** Constant for the "F" key. */
350 public static final int VK_F = 0x46;
351
352 /** Constant for the "G" key. */
353 public static final int VK_G = 0x47;
354
355 /** Constant for the "H" key. */
356 public static final int VK_H = 0x48;
357
358 /** Constant for the "I" key. */
359 public static final int VK_I = 0x49;
360
361 /** Constant for the "J" key. */
362 public static final int VK_J = 0x4A;
363
364 /** Constant for the "K" key. */
365 public static final int VK_K = 0x4B;
366
367 /** Constant for the "L" key. */
368 public static final int VK_L = 0x4C;
369
370 /** Constant for the "M" key. */
371 public static final int VK_M = 0x4D;
372
373 /** Constant for the "N" key. */
374 public static final int VK_N = 0x4E;
375
376 /** Constant for the "O" key. */
377 public static final int VK_O = 0x4F;
378
379 /** Constant for the "P" key. */
380 public static final int VK_P = 0x50;
381
382 /** Constant for the "Q" key. */
383 public static final int VK_Q = 0x51;
384
385 /** Constant for the "R" key. */
386 public static final int VK_R = 0x52;
387
388 /** Constant for the "S" key. */
389 public static final int VK_S = 0x53;
390
391 /** Constant for the "T" key. */
392 public static final int VK_T = 0x54;
393
394 /** Constant for the "U" key. */
395 public static final int VK_U = 0x55;
396
397 /** Constant for the "V" key. */
398 public static final int VK_V = 0x56;
399
400 /** Constant for the "W" key. */
401 public static final int VK_W = 0x57;
402
403 /** Constant for the "X" key. */
404 public static final int VK_X = 0x58;
405
406 /** Constant for the "Y" key. */
407 public static final int VK_Y = 0x59;
408
409 /** Constant for the "Z" key. */
410 public static final int VK_Z = 0x5A;
411
412 /**
413 * Constant for the open bracket key, "["
414 */
415 public static final int VK_OPEN_BRACKET = 0x5B;
416
417 /**
418 * Constant for the back slash key, "\"
419 */
420 public static final int VK_BACK_SLASH = 0x5C;
421
422 /**
423 * Constant for the close bracket key, "]"
424 */
425 public static final int VK_CLOSE_BRACKET = 0x5D;
426
427 /** Constant for the number pad "0" key. */
428 public static final int VK_NUMPAD0 = 0x60;
429
430 /** Constant for the number pad "1" key. */
431 public static final int VK_NUMPAD1 = 0x61;
432
433 /** Constant for the number pad "2" key. */
434 public static final int VK_NUMPAD2 = 0x62;
435
436 /** Constant for the number pad "3" key. */
437 public static final int VK_NUMPAD3 = 0x63;
438
439 /** Constant for the number pad "4" key. */
440 public static final int VK_NUMPAD4 = 0x64;
441
442 /** Constant for the number pad "5" key. */
443 public static final int VK_NUMPAD5 = 0x65;
444
445 /** Constant for the number pad "6" key. */
446 public static final int VK_NUMPAD6 = 0x66;
447
448 /** Constant for the number pad "7" key. */
449 public static final int VK_NUMPAD7 = 0x67;
450
451 /** Constant for the number pad "8" key. */
452 public static final int VK_NUMPAD8 = 0x68;
453
454 /** Constant for the number pad "9" key. */
455 public static final int VK_NUMPAD9 = 0x69;
456
457 /** Constant for the number pad multiply key. */
458 public static final int VK_MULTIPLY = 0x6A;
459
460 /** Constant for the number pad add key. */
461 public static final int VK_ADD = 0x6B;
462
463 /**
464 * This constant is obsolete, and is included only for backwards
465 * compatibility.
466 * @see #VK_SEPARATOR
467 */
468 public static final int VK_SEPARATER = 0x6C;
469
470 /**
471 * Constant for the Numpad Separator key.
472 * @since 1.4
473 */
474 public static final int VK_SEPARATOR = VK_SEPARATER;
475
476 /** Constant for the number pad subtract key. */
477 public static final int VK_SUBTRACT = 0x6D;
478
479 /** Constant for the number pad decimal point key. */
480 public static final int VK_DECIMAL = 0x6E;
481
482 /** Constant for the number pad divide key. */
483 public static final int VK_DIVIDE = 0x6F;
484
485 /** Constant for the delete key. */
486 public static final int VK_DELETE = 0x7F; /* ASCII DEL */
487
488 /** Constant for the NUM_LOCK key. */
489 public static final int VK_NUM_LOCK = 0x90;
490
491 /** Constant for the SCROLL_LOCK key. */
492 public static final int VK_SCROLL_LOCK = 0x91;
493
494 /** Constant for the F1 function key. */
495 public static final int VK_F1 = 0x70;
496
497 /** Constant for the F2 function key. */
498 public static final int VK_F2 = 0x71;
499
500 /** Constant for the F3 function key. */
501 public static final int VK_F3 = 0x72;
502
503 /** Constant for the F4 function key. */
504 public static final int VK_F4 = 0x73;
505
506 /** Constant for the F5 function key. */
507 public static final int VK_F5 = 0x74;
508
509 /** Constant for the F6 function key. */
510 public static final int VK_F6 = 0x75;
511
583 public static final int VK_F21 = 0xF008;
584
585 /**
586 * Constant for the F22 function key.
587 * @since 1.2
588 */
589 public static final int VK_F22 = 0xF009;
590
591 /**
592 * Constant for the F23 function key.
593 * @since 1.2
594 */
595 public static final int VK_F23 = 0xF00A;
596
597 /**
598 * Constant for the F24 function key.
599 * @since 1.2
600 */
601 public static final int VK_F24 = 0xF00B;
602
603 /** Constant for the PRINTSCREEN key. */
604 public static final int VK_PRINTSCREEN = 0x9A;
605
606 /** Constant for the INSERT key. */
607 public static final int VK_INSERT = 0x9B;
608
609 /** Constant for the HELP key. */
610 public static final int VK_HELP = 0x9C;
611
612 /** Constant for the META key. */
613 public static final int VK_META = 0x9D;
614
615 /** Constant for the BACK_QUOTE key. */
616 public static final int VK_BACK_QUOTE = 0xC0;
617
618 /** Constant for the QUOTE key. */
619 public static final int VK_QUOTE = 0xDE;
620
621 /**
622 * Constant for the numeric keypad <b>up</b> arrow key.
623 * @see #VK_UP
624 * @since 1.2
625 */
626 public static final int VK_KP_UP = 0xE0;
627
628 /**
629 * Constant for the numeric keypad <b>down</b> arrow key.
630 * @see #VK_DOWN
631 * @since 1.2
632 */
633 public static final int VK_KP_DOWN = 0xE1;
634
635 /**
636 * Constant for the numeric keypad <b>left</b> arrow key.
637 * @see #VK_LEFT
638 * @since 1.2
768 */
769 public static final int VK_UNDERSCORE = 0x020B;
770
771 /**
772 * Constant for the Microsoft Windows "Windows" key.
773 * It is used for both the left and right version of the key.
774 * @see #getKeyLocation()
775 * @since 1.5
776 */
777 public static final int VK_WINDOWS = 0x020C;
778
779 /**
780 * Constant for the Microsoft Windows Context Menu key.
781 * @since 1.5
782 */
783 public static final int VK_CONTEXT_MENU = 0x020D;
784
785 /* for input method support on Asian Keyboards */
786
787 /* not clear what this means - listed in Microsoft Windows API */
788 /** Constant for the FINAL key. */
789 public static final int VK_FINAL = 0x0018;
790
791 /** Constant for the Convert function key. */
792 /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
793 public static final int VK_CONVERT = 0x001C;
794
795 /** Constant for the Don't Convert function key. */
796 /* Japanese PC 106 keyboard: muhenkan */
797 public static final int VK_NONCONVERT = 0x001D;
798
799 /** Constant for the Accept or Commit function key. */
800 /* Japanese Solaris keyboard: kakutei */
801 public static final int VK_ACCEPT = 0x001E;
802
803 /* not clear what this means - listed in Microsoft Windows API */
804 /** Constant for the MODECHANGE key. */
805 public static final int VK_MODECHANGE = 0x001F;
806
807 /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
808 might still be used on other platforms */
809 /**
810 * Constant for the KANA lock key.
811 * @see #VK_KANA_LOCK
812 **/
813 public static final int VK_KANA = 0x0015;
814
815 /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
816 might still be used for other platforms */
817 /**
818 * Constant for KANJI.
819 * @see #VK_INPUT_METHOD_ON_OFF
820 */
821 public static final int VK_KANJI = 0x0019;
822
823 /**
824 * Constant for the Alphanumeric function key.
825 * @since 1.2
826 */
827 /* Japanese PC 106 keyboard: eisuu */
828 public static final int VK_ALPHANUMERIC = 0x00F0;
829
830 /**
831 * Constant for the Katakana function key.
832 * @since 1.2
833 */
834 /* Japanese PC 106 keyboard: katakana */
835 public static final int VK_KATAKANA = 0x00F1;
836
837 /**
838 * Constant for the Hiragana function key.
839 * @since 1.2
840 */
1225 * @throws IllegalArgumentException if <code>id</code> is
1226 * <code>KEY_TYPED</code> and <code>keyChar</code> is
1227 * <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
1228 * <code>KEY_TYPED</code> and <code>keyCode</code> is not
1229 * <code>VK_UNDEFINED</code>
1230 * @throws IllegalArgumentException if <code>source</code> is null
1231 * @see #getSource()
1232 * @see #getID()
1233 * @see #getWhen()
1234 * @see #getModifiers()
1235 * @see #getKeyCode()
1236 * @see #getKeyChar()
1237 */
1238 public KeyEvent(Component source, int id, long when, int modifiers,
1239 int keyCode, char keyChar) {
1240 this(source, id, when, modifiers, keyCode, keyChar,
1241 KEY_LOCATION_UNKNOWN);
1242 }
1243
1244 /**
1245 * @deprecated as of JDK1.1; use {@link #KeyEvent(Component, int, long, int, int, char)} instead
1246 * @param source The <code>Component</code> that originated the event
1247 * @param id An integer indicating the type of event.
1248 * For information on allowable values, see
1249 * the class description for {@link KeyEvent}
1250 * @param when A long integer that specifies the time the event
1251 * occurred.
1252 * Passing negative or zero value
1253 * is not recommended
1254 * @param modifiers The modifier keys down during event (shift, ctrl,
1255 * alt, meta).
1256 * Passing negative value
1257 * is not recommended.
1258 * Zero value means that no modifiers were passed.
1259 * Use either an extended _DOWN_MASK or old _MASK modifiers,
1260 * however do not mix models in the one event.
1261 * The extended modifiers are preferred for using
1262 * @param keyCode The integer code for an actual key, or VK_UNDEFINED
1263 * (for a key-typed event)
1264 */
1265 @Deprecated
1266 public KeyEvent(Component source, int id, long when, int modifiers,
1267 int keyCode) {
1268 this(source, id, when, modifiers, keyCode, (char)keyCode);
1269 }
1270
1271 /**
1272 * Returns the integer keyCode associated with the key in this event.
1273 *
1274 * @return the integer code for an actual key on the keyboard.
1275 * (For <code>KEY_TYPED</code> events, the keyCode is
1276 * <code>VK_UNDEFINED</code>.)
1277 */
1278 public int getKeyCode() {
1279 return keyCode;
1280 }
1281
1282 /**
1283 * Set the keyCode value to indicate a physical key.
1342 /**
1343 * Returns the location of the key that originated this key event.
1344 *
1345 * Some keys occur more than once on a keyboard, e.g. the left and
1346 * right shift keys. Additionally, some keys occur on the numeric
1347 * keypad. This provides a way of distinguishing such keys.
1348 *
1349 * @return the location of the key that was pressed or released.
1350 * Always returns <code>KEY_LOCATION_UNKNOWN</code> for
1351 * <code>KEY_TYPED</code> events.
1352 * @since 1.4
1353 */
1354 public int getKeyLocation() {
1355 return keyLocation;
1356 }
1357
1358 /**
1359 * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
1360 * These strings can be localized by changing the awt.properties file.
1361 *
1362 * @param keyCode the key whose description is to be returned
1363 * @return a string containing a text description for a physical key,
1364 * identified by its keyCode
1365 */
1366 public static String getKeyText(int keyCode) {
1367 if (keyCode >= VK_0 && keyCode <= VK_9 ||
1368 keyCode >= VK_A && keyCode <= VK_Z) {
1369 return String.valueOf((char)keyCode);
1370 }
1371
1372 switch(keyCode) {
1373 case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
1374 case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
1375 case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
1376 case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
1377 case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
1378 case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
1379 case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
1380 case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
1381 case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
1382 case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
1535
1536 if ((keyCode & 0x01000000) != 0) {
1537 return String.valueOf((char)(keyCode ^ 0x01000000 ));
1538 }
1539 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1540 return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1541 }
1542
1543 /**
1544 * Returns a <code>String</code> describing the modifier key(s),
1545 * such as "Shift", or "Ctrl+Shift". These strings can be
1546 * localized by changing the <code>awt.properties</code> file.
1547 * <p>
1548 * Note that <code>InputEvent.ALT_MASK</code> and
1549 * <code>InputEvent.BUTTON2_MASK</code> have the same value,
1550 * so the string "Alt" is returned for both modifiers. Likewise,
1551 * <code>InputEvent.META_MASK</code> and
1552 * <code>InputEvent.BUTTON3_MASK</code> have the same value,
1553 * so the string "Meta" is returned for both modifiers.
1554 *
1555 * @param modifiers the modifier mask to be processed
1556 * @return string a text description of the combination of modifier
1557 * keys that were held down during the event
1558 * @see InputEvent#getModifiersExText(int)
1559 */
1560 public static String getKeyModifiersText(int modifiers) {
1561 StringBuilder buf = new StringBuilder();
1562 if ((modifiers & InputEvent.META_MASK) != 0) {
1563 buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1564 buf.append("+");
1565 }
1566 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1567 buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1568 buf.append("+");
1569 }
1570 if ((modifiers & InputEvent.ALT_MASK) != 0) {
1571 buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1572 buf.append("+");
1573 }
1574 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1575 buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1772 default:
1773 str.append("KEY_LOCATION_UNKNOWN");
1774 break;
1775 }
1776 str.append(",rawCode=").append(rawCode);
1777 str.append(",primaryLevelUnicode=").append(primaryLevelUnicode);
1778 str.append(",scancode=").append(scancode);
1779 str.append(",extendedKeyCode=0x").append(Long.toHexString(extendedKeyCode));
1780
1781 return str.toString();
1782 }
1783 /**
1784 * Returns an extended key code for the event.
1785 * The extended key code is a unique id assigned to a key on the keyboard
1786 * just like {@code keyCode}. However, unlike {@code keyCode}, this value depends on the
1787 * current keyboard layout. For instance, pressing the left topmost letter key
1788 * in a common English layout produces the same value as {@code keyCode}, {@code VK_Q}.
1789 * Pressing the same key in a regular Russian layout gives another code, unique for the
1790 * letter "Cyrillic I short".
1791 *
1792 * @return an extended key code for the event
1793 * @since 1.7
1794 */
1795 public int getExtendedKeyCode() {
1796 return (int)extendedKeyCode;
1797 }
1798 /**
1799 * Returns an extended key code for a unicode character.
1800 *
1801 * @param c the unicode character to be processed
1802 * @return for a unicode character with a corresponding {@code VK_} constant -- this
1803 * {@code VK_} constant; for a character appearing on the primary
1804 * level of a known keyboard layout -- a unique integer.
1805 * If a character does not appear on the primary level of a known keyboard,
1806 * {@code VK_UNDEFINED} is returned.
1807 *
1808 * @since 1.7
1809 */
1810 public static int getExtendedKeyCodeForChar(int c) {
1811 // Return a keycode (if any) associated with a character.
1812 return sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(c);
1813 }
1814
1815 /**
1816 * Sets new modifiers by the old ones. The key modifiers
1817 * override overlaping mouse modifiers.
1818 */
1819 private void setNewModifiers() {
1820 if ((modifiers & SHIFT_MASK) != 0) {
1821 modifiers |= SHIFT_DOWN_MASK;
1822 }
1823 if ((modifiers & ALT_MASK) != 0) {
1824 modifiers |= ALT_DOWN_MASK;
1825 }
1826 if ((modifiers & CTRL_MASK) != 0) {
1827 modifiers |= CTRL_DOWN_MASK;
1828 }
|