1 /*
2 * Copyright 1996-2008 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package java.awt.event;
27
28 import java.awt.Event;
29 import java.awt.Component;
30 import java.awt.GraphicsEnvironment;
31 import java.awt.Toolkit;
32 import java.io.IOException;
33 import java.io.ObjectInputStream;
34
35 /**
36 * An event which indicates that a keystroke occurred in a component.
37 * <p>
38 * This low-level event is generated by a component object (such as a text
39 * field) when a key is pressed, released, or typed.
40 * The event is passed to every <code>KeyListener</code>
41 * or <code>KeyAdapter</code> object which registered to receive such
42 * events using the component's <code>addKeyListener</code> method.
43 * (<code>KeyAdapter</code> objects implement the
44 * <code>KeyListener</code> interface.) Each such listener object
45 * gets this <code>KeyEvent</code> when the event occurs.
46 * <p>
47 * <em>"Key typed" events</em> are higher-level and generally do not depend on
48 * the platform or keyboard layout. They are generated when a Unicode character
49 * is entered, and are the preferred way to find out about character input.
50 * In the simplest case, a key typed event is produced by a single key press
51 * (e.g., 'a'). Often, however, characters are produced by series of key
52 * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to
53 * key typed events may be many-to-one or many-to-many. Key releases are not
54 * usually necessary to generate a key typed event, but there are some cases
55 * where the key typed event is not generated until a key is released (e.g.,
56 * entering ASCII sequences via the Alt-Numpad method in Windows).
57 * No key typed events are generated for keys that don't generate Unicode
58 * characters (e.g., action keys, modifier keys, etc.).
59 * <p>
60 * The getKeyChar method always returns a valid Unicode character or
61 * CHAR_UNDEFINED. Character input is reported by KEY_TYPED events:
62 * KEY_PRESSED and KEY_RELEASED events are not necessarily associated
63 * with character input. Therefore, the result of the getKeyChar method
64 * is guaranteed to be meaningful only for KEY_TYPED events.
65 * <p>
66 * For key pressed and key released events, the getKeyCode method returns
67 * the event's keyCode. For key typed events, the getKeyCode method
68 * always returns VK_UNDEFINED.
69 *
70 * <p>
71 * <em>"Key pressed" and "key released" events</em> are lower-level and depend
72 * on the platform and keyboard layout. They are generated whenever a key is
73 * pressed or released, and are the only way to find out about keys that don't
74 * generate character input (e.g., action keys, modifier keys, etc.). The key
75 * being pressed or released is indicated by the getKeyCode method, which returns
76 * a virtual key code.
77 *
78 * <p>
79 * <em>Virtual key codes</em> are used to report which keyboard key has
80 * been pressed, rather than a character generated by the combination
81 * of one or more keystrokes (such as "A", which comes from shift and "a").
82 *
83 * <p>
84 * For example, pressing the Shift key will cause a KEY_PRESSED event
85 * with a VK_SHIFT keyCode, while pressing the 'a' key will result in
86 * a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event
87 * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
88 * value of 'A' is generated.
89 *
90 * <p>
91 * Pressing and releasing a key on the keyboard results in the generating
92 * the following key events (in order):
93 * <PRE>
94 * {@code KEY_PRESSED}
95 * {@code KEY_TYPED} (is only generated if a valid Unicode character could be generated.)
96 * {@code KEY_RELEASED}
97 * </PRE>
98 *
99 * But in some cases (e.g. auto-repeat or input method is activated) the order
100 * could be different (and platform dependent).
101 *
102 * <p>
103 * Notes:
104 * <ul>
105 * <li>Key combinations which do not result in Unicode characters, such as action
106 * keys like F1 and the HELP key, do not generate KEY_TYPED events.
107 * <li>Not all keyboards or systems are capable of generating all
108 * virtual key codes. No attempt is made in Java to generate these keys
109 * artificially.
110 * <li>Virtual key codes do not identify a physical key: they depend on the
111 * platform and keyboard layout. For example, the key that generates VK_Q
112 * when using a U.S. keyboard layout will generate VK_A when using a French
113 * keyboard layout.
114 * <li>Not all characters have a keycode associated with them. For example,
115 * there is no keycode for the question mark because there is no keyboard
116 * for which it appears on the primary layer.
117 * <li>In order to support the platform-independent handling of action keys,
118 * the Java platform uses a few additional virtual key constants for functions
119 * that would otherwise have to be recognized by interpreting virtual key codes
120 * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
121 * is returned instead of VK_CONVERT with the ALT modifier.
122 * <li>As specified in <a href="../doc-files/FocusSpec.html">Focus Specification</a>
123 * key events are dispatched to the focus owner by default.
124 * </ul>
125 *
126 * <p>
127 * WARNING: Aside from those keys that are defined by the Java language
128 * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
129 * constants. Sun reserves the right to change these values as needed
130 * to accomodate a wider range of keyboards in the future.
131 * <p>
132 * An unspecified behavior will be caused if the {@code id} parameter
133 * of any particular {@code KeyEvent} instance is not
134 * in the range from {@code KEY_FIRST} to {@code KEY_LAST}.
135 *
136 * @author Carl Quinn
137 * @author Amy Fowler
138 * @author Norbert Lindenberg
139 *
140 * @see KeyAdapter
141 * @see KeyListener
142 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/keylistener.html">Tutorial: Writing a Key Listener</a>
143 *
144 * @since 1.1
145 */
146 public class KeyEvent extends InputEvent {
147
148 /**
149 * Stores the state of native event dispatching system
150 * - true, if when the event was created event proxying
151 * mechanism was active
152 * - false, if it was inactive
153 * Used in Component.dispatchEventImpl to correctly dispatch
154 * events when proxy is active
155 */
156 private boolean isProxyActive = false;
157
158 /**
159 * The first number in the range of ids used for key events.
160 */
161 public static final int KEY_FIRST = 400;
162
163 /**
164 * The last number in the range of ids used for key events.
165 */
166 public static final int KEY_LAST = 402;
167
168 /**
169 * The "key typed" event. This event is generated when a character is
170 * entered. In the simplest case, it is produced by a single key press.
171 * Often, however, characters are produced by series of key presses, and
172 * the mapping from key pressed events to key typed events may be
173 * many-to-one or many-to-many.
174 */
175 public static final int KEY_TYPED = KEY_FIRST;
176
177 /**
178 * The "key pressed" event. This event is generated when a key
179 * is pushed down.
180 */
181 public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
182
183 /**
184 * The "key released" event. This event is generated when a key
185 * is let up.
186 */
187 public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
188
189 /* Virtual key codes. */
190
191 public static final int VK_ENTER = '\n';
192 public static final int VK_BACK_SPACE = '\b';
193 public static final int VK_TAB = '\t';
194 public static final int VK_CANCEL = 0x03;
195 public static final int VK_CLEAR = 0x0C;
196 public static final int VK_SHIFT = 0x10;
197 public static final int VK_CONTROL = 0x11;
198 public static final int VK_ALT = 0x12;
199 public static final int VK_PAUSE = 0x13;
200 public static final int VK_CAPS_LOCK = 0x14;
201 public static final int VK_ESCAPE = 0x1B;
202 public static final int VK_SPACE = 0x20;
203 public static final int VK_PAGE_UP = 0x21;
204 public static final int VK_PAGE_DOWN = 0x22;
205 public static final int VK_END = 0x23;
206 public static final int VK_HOME = 0x24;
207
208 /**
209 * Constant for the non-numpad <b>left</b> arrow key.
210 * @see #VK_KP_LEFT
211 */
212 public static final int VK_LEFT = 0x25;
213
214 /**
215 * Constant for the non-numpad <b>up</b> arrow key.
216 * @see #VK_KP_UP
217 */
218 public static final int VK_UP = 0x26;
219
220 /**
221 * Constant for the non-numpad <b>right</b> arrow key.
222 * @see #VK_KP_RIGHT
223 */
224 public static final int VK_RIGHT = 0x27;
225
226 /**
227 * Constant for the non-numpad <b>down</b> arrow key.
228 * @see #VK_KP_DOWN
229 */
230 public static final int VK_DOWN = 0x28;
231
232 /**
233 * Constant for the comma key, ","
234 */
235 public static final int VK_COMMA = 0x2C;
236
237 /**
238 * Constant for the minus key, "-"
239 * @since 1.2
240 */
241 public static final int VK_MINUS = 0x2D;
242
243 /**
244 * Constant for the period key, "."
245 */
246 public static final int VK_PERIOD = 0x2E;
247
248 /**
249 * Constant for the forward slash key, "/"
250 */
251 public static final int VK_SLASH = 0x2F;
252
253 /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
254 public static final int VK_0 = 0x30;
255 public static final int VK_1 = 0x31;
256 public static final int VK_2 = 0x32;
257 public static final int VK_3 = 0x33;
258 public static final int VK_4 = 0x34;
259 public static final int VK_5 = 0x35;
260 public static final int VK_6 = 0x36;
261 public static final int VK_7 = 0x37;
262 public static final int VK_8 = 0x38;
263 public static final int VK_9 = 0x39;
264
265 /**
266 * Constant for the semicolon key, ";"
267 */
268 public static final int VK_SEMICOLON = 0x3B;
269
270 /**
271 * Constant for the equals key, "="
272 */
273 public static final int VK_EQUALS = 0x3D;
274
275 /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
276 public static final int VK_A = 0x41;
277 public static final int VK_B = 0x42;
278 public static final int VK_C = 0x43;
279 public static final int VK_D = 0x44;
280 public static final int VK_E = 0x45;
281 public static final int VK_F = 0x46;
282 public static final int VK_G = 0x47;
283 public static final int VK_H = 0x48;
284 public static final int VK_I = 0x49;
285 public static final int VK_J = 0x4A;
286 public static final int VK_K = 0x4B;
287 public static final int VK_L = 0x4C;
288 public static final int VK_M = 0x4D;
289 public static final int VK_N = 0x4E;
290 public static final int VK_O = 0x4F;
291 public static final int VK_P = 0x50;
292 public static final int VK_Q = 0x51;
293 public static final int VK_R = 0x52;
294 public static final int VK_S = 0x53;
295 public static final int VK_T = 0x54;
296 public static final int VK_U = 0x55;
297 public static final int VK_V = 0x56;
298 public static final int VK_W = 0x57;
299 public static final int VK_X = 0x58;
300 public static final int VK_Y = 0x59;
301 public static final int VK_Z = 0x5A;
302
303 /**
304 * Constant for the open bracket key, "["
305 */
306 public static final int VK_OPEN_BRACKET = 0x5B;
307
308 /**
309 * Constant for the back slash key, "\"
310 */
311 public static final int VK_BACK_SLASH = 0x5C;
312
313 /**
314 * Constant for the close bracket key, "]"
315 */
316 public static final int VK_CLOSE_BRACKET = 0x5D;
317
318 public static final int VK_NUMPAD0 = 0x60;
319 public static final int VK_NUMPAD1 = 0x61;
320 public static final int VK_NUMPAD2 = 0x62;
321 public static final int VK_NUMPAD3 = 0x63;
322 public static final int VK_NUMPAD4 = 0x64;
323 public static final int VK_NUMPAD5 = 0x65;
324 public static final int VK_NUMPAD6 = 0x66;
325 public static final int VK_NUMPAD7 = 0x67;
326 public static final int VK_NUMPAD8 = 0x68;
327 public static final int VK_NUMPAD9 = 0x69;
328 public static final int VK_MULTIPLY = 0x6A;
329 public static final int VK_ADD = 0x6B;
330
331 /**
332 * This constant is obsolete, and is included only for backwards
333 * compatibility.
334 * @see #VK_SEPARATOR
335 */
336 public static final int VK_SEPARATER = 0x6C;
337
338 /**
339 * Constant for the Numpad Separator key.
340 * @since 1.4
341 */
342 public static final int VK_SEPARATOR = VK_SEPARATER;
343
344 public static final int VK_SUBTRACT = 0x6D;
345 public static final int VK_DECIMAL = 0x6E;
346 public static final int VK_DIVIDE = 0x6F;
347 public static final int VK_DELETE = 0x7F; /* ASCII DEL */
348 public static final int VK_NUM_LOCK = 0x90;
349 public static final int VK_SCROLL_LOCK = 0x91;
350
351 /** Constant for the F1 function key. */
352 public static final int VK_F1 = 0x70;
353
354 /** Constant for the F2 function key. */
355 public static final int VK_F2 = 0x71;
356
357 /** Constant for the F3 function key. */
358 public static final int VK_F3 = 0x72;
359
360 /** Constant for the F4 function key. */
361 public static final int VK_F4 = 0x73;
362
363 /** Constant for the F5 function key. */
364 public static final int VK_F5 = 0x74;
365
366 /** Constant for the F6 function key. */
367 public static final int VK_F6 = 0x75;
368
369 /** Constant for the F7 function key. */
370 public static final int VK_F7 = 0x76;
371
372 /** Constant for the F8 function key. */
373 public static final int VK_F8 = 0x77;
374
375 /** Constant for the F9 function key. */
376 public static final int VK_F9 = 0x78;
377
378 /** Constant for the F10 function key. */
379 public static final int VK_F10 = 0x79;
380
381 /** Constant for the F11 function key. */
382 public static final int VK_F11 = 0x7A;
383
384 /** Constant for the F12 function key. */
385 public static final int VK_F12 = 0x7B;
386
387 /**
388 * Constant for the F13 function key.
389 * @since 1.2
390 */
391 /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
392 public static final int VK_F13 = 0xF000;
393
394 /**
395 * Constant for the F14 function key.
396 * @since 1.2
397 */
398 public static final int VK_F14 = 0xF001;
399
400 /**
401 * Constant for the F15 function key.
402 * @since 1.2
403 */
404 public static final int VK_F15 = 0xF002;
405
406 /**
407 * Constant for the F16 function key.
408 * @since 1.2
409 */
410 public static final int VK_F16 = 0xF003;
411
412 /**
413 * Constant for the F17 function key.
414 * @since 1.2
415 */
416 public static final int VK_F17 = 0xF004;
417
418 /**
419 * Constant for the F18 function key.
420 * @since 1.2
421 */
422 public static final int VK_F18 = 0xF005;
423
424 /**
425 * Constant for the F19 function key.
426 * @since 1.2
427 */
428 public static final int VK_F19 = 0xF006;
429
430 /**
431 * Constant for the F20 function key.
432 * @since 1.2
433 */
434 public static final int VK_F20 = 0xF007;
435
436 /**
437 * Constant for the F21 function key.
438 * @since 1.2
439 */
440 public static final int VK_F21 = 0xF008;
441
442 /**
443 * Constant for the F22 function key.
444 * @since 1.2
445 */
446 public static final int VK_F22 = 0xF009;
447
448 /**
449 * Constant for the F23 function key.
450 * @since 1.2
451 */
452 public static final int VK_F23 = 0xF00A;
453
454 /**
455 * Constant for the F24 function key.
456 * @since 1.2
457 */
458 public static final int VK_F24 = 0xF00B;
459
460 public static final int VK_PRINTSCREEN = 0x9A;
461 public static final int VK_INSERT = 0x9B;
462 public static final int VK_HELP = 0x9C;
463 public static final int VK_META = 0x9D;
464
465 public static final int VK_BACK_QUOTE = 0xC0;
466 public static final int VK_QUOTE = 0xDE;
467
468 /**
469 * Constant for the numeric keypad <b>up</b> arrow key.
470 * @see #VK_UP
471 * @since 1.2
472 */
473 public static final int VK_KP_UP = 0xE0;
474
475 /**
476 * Constant for the numeric keypad <b>down</b> arrow key.
477 * @see #VK_DOWN
478 * @since 1.2
479 */
480 public static final int VK_KP_DOWN = 0xE1;
481
482 /**
483 * Constant for the numeric keypad <b>left</b> arrow key.
484 * @see #VK_LEFT
485 * @since 1.2
486 */
487 public static final int VK_KP_LEFT = 0xE2;
488
489 /**
490 * Constant for the numeric keypad <b>right</b> arrow key.
491 * @see #VK_RIGHT
492 * @since 1.2
493 */
494 public static final int VK_KP_RIGHT = 0xE3;
495
496 /* For European keyboards */
497 /** @since 1.2 */
498 public static final int VK_DEAD_GRAVE = 0x80;
499 /** @since 1.2 */
500 public static final int VK_DEAD_ACUTE = 0x81;
501 /** @since 1.2 */
502 public static final int VK_DEAD_CIRCUMFLEX = 0x82;
503 /** @since 1.2 */
504 public static final int VK_DEAD_TILDE = 0x83;
505 /** @since 1.2 */
506 public static final int VK_DEAD_MACRON = 0x84;
507 /** @since 1.2 */
508 public static final int VK_DEAD_BREVE = 0x85;
509 /** @since 1.2 */
510 public static final int VK_DEAD_ABOVEDOT = 0x86;
511 /** @since 1.2 */
512 public static final int VK_DEAD_DIAERESIS = 0x87;
513 /** @since 1.2 */
514 public static final int VK_DEAD_ABOVERING = 0x88;
515 /** @since 1.2 */
516 public static final int VK_DEAD_DOUBLEACUTE = 0x89;
517 /** @since 1.2 */
518 public static final int VK_DEAD_CARON = 0x8a;
519 /** @since 1.2 */
520 public static final int VK_DEAD_CEDILLA = 0x8b;
521 /** @since 1.2 */
522 public static final int VK_DEAD_OGONEK = 0x8c;
523 /** @since 1.2 */
524 public static final int VK_DEAD_IOTA = 0x8d;
525 /** @since 1.2 */
526 public static final int VK_DEAD_VOICED_SOUND = 0x8e;
527 /** @since 1.2 */
528 public static final int VK_DEAD_SEMIVOICED_SOUND = 0x8f;
529
530 /** @since 1.2 */
531 public static final int VK_AMPERSAND = 0x96;
532 /** @since 1.2 */
533 public static final int VK_ASTERISK = 0x97;
534 /** @since 1.2 */
535 public static final int VK_QUOTEDBL = 0x98;
536 /** @since 1.2 */
537 public static final int VK_LESS = 0x99;
538
539 /** @since 1.2 */
540 public static final int VK_GREATER = 0xa0;
541 /** @since 1.2 */
542 public static final int VK_BRACELEFT = 0xa1;
543 /** @since 1.2 */
544 public static final int VK_BRACERIGHT = 0xa2;
545
546 /**
547 * Constant for the "@" key.
548 * @since 1.2
549 */
550 public static final int VK_AT = 0x0200;
551
552 /**
553 * Constant for the ":" key.
554 * @since 1.2
555 */
556 public static final int VK_COLON = 0x0201;
557
558 /**
559 * Constant for the "^" key.
560 * @since 1.2
561 */
562 public static final int VK_CIRCUMFLEX = 0x0202;
563
564 /**
565 * Constant for the "$" key.
566 * @since 1.2
567 */
568 public static final int VK_DOLLAR = 0x0203;
569
570 /**
571 * Constant for the Euro currency sign key.
572 * @since 1.2
573 */
574 public static final int VK_EURO_SIGN = 0x0204;
575
576 /**
577 * Constant for the "!" key.
578 * @since 1.2
579 */
580 public static final int VK_EXCLAMATION_MARK = 0x0205;
581
582 /**
583 * Constant for the inverted exclamation mark key.
584 * @since 1.2
585 */
586 public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
587
588 /**
589 * Constant for the "(" key.
590 * @since 1.2
591 */
592 public static final int VK_LEFT_PARENTHESIS = 0x0207;
593
594 /**
595 * Constant for the "#" key.
596 * @since 1.2
597 */
598 public static final int VK_NUMBER_SIGN = 0x0208;
599
600 /**
601 * Constant for the "+" key.
602 * @since 1.2
603 */
604 public static final int VK_PLUS = 0x0209;
605
606 /**
607 * Constant for the ")" key.
608 * @since 1.2
609 */
610 public static final int VK_RIGHT_PARENTHESIS = 0x020A;
611
612 /**
613 * Constant for the "_" key.
614 * @since 1.2
615 */
616 public static final int VK_UNDERSCORE = 0x020B;
617
618 /**
619 * Constant for the Microsoft Windows "Windows" key.
620 * It is used for both the left and right version of the key.
621 * @see #getKeyLocation()
622 * @since 1.5
623 */
624 public static final int VK_WINDOWS = 0x020C;
625
626 /**
627 * Constant for the Microsoft Windows Context Menu key.
628 * @since 1.5
629 */
630 public static final int VK_CONTEXT_MENU = 0x020D;
631
632 /* for input method support on Asian Keyboards */
633
634 /* not clear what this means - listed in Microsoft Windows API */
635 public static final int VK_FINAL = 0x0018;
636
637 /** Constant for the Convert function key. */
638 /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
639 public static final int VK_CONVERT = 0x001C;
640
641 /** Constant for the Don't Convert function key. */
642 /* Japanese PC 106 keyboard: muhenkan */
643 public static final int VK_NONCONVERT = 0x001D;
644
645 /** Constant for the Accept or Commit function key. */
646 /* Japanese Solaris keyboard: kakutei */
647 public static final int VK_ACCEPT = 0x001E;
648
649 /* not clear what this means - listed in Microsoft Windows API */
650 public static final int VK_MODECHANGE = 0x001F;
651
652 /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
653 might still be used on other platforms */
654 public static final int VK_KANA = 0x0015;
655
656 /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
657 might still be used for other platforms */
658 public static final int VK_KANJI = 0x0019;
659
660 /**
661 * Constant for the Alphanumeric function key.
662 * @since 1.2
663 */
664 /* Japanese PC 106 keyboard: eisuu */
665 public static final int VK_ALPHANUMERIC = 0x00F0;
666
667 /**
668 * Constant for the Katakana function key.
669 * @since 1.2
670 */
671 /* Japanese PC 106 keyboard: katakana */
672 public static final int VK_KATAKANA = 0x00F1;
673
674 /**
675 * Constant for the Hiragana function key.
676 * @since 1.2
677 */
678 /* Japanese PC 106 keyboard: hiragana */
679 public static final int VK_HIRAGANA = 0x00F2;
680
681 /**
682 * Constant for the Full-Width Characters function key.
683 * @since 1.2
684 */
685 /* Japanese PC 106 keyboard: zenkaku */
686 public static final int VK_FULL_WIDTH = 0x00F3;
687
688 /**
689 * Constant for the Half-Width Characters function key.
690 * @since 1.2
691 */
692 /* Japanese PC 106 keyboard: hankaku */
693 public static final int VK_HALF_WIDTH = 0x00F4;
694
695 /**
696 * Constant for the Roman Characters function key.
697 * @since 1.2
698 */
699 /* Japanese PC 106 keyboard: roumaji */
700 public static final int VK_ROMAN_CHARACTERS = 0x00F5;
701
702 /**
703 * Constant for the All Candidates function key.
704 * @since 1.2
705 */
706 /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */
707 public static final int VK_ALL_CANDIDATES = 0x0100;
708
709 /**
710 * Constant for the Previous Candidate function key.
711 * @since 1.2
712 */
713 /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */
714 public static final int VK_PREVIOUS_CANDIDATE = 0x0101;
715
716 /**
717 * Constant for the Code Input function key.
718 * @since 1.2
719 */
720 /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */
721 public static final int VK_CODE_INPUT = 0x0102;
722
723 /**
724 * Constant for the Japanese-Katakana function key.
725 * This key switches to a Japanese input method and selects its Katakana input mode.
726 * @since 1.2
727 */
728 /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
729 public static final int VK_JAPANESE_KATAKANA = 0x0103;
730
731 /**
732 * Constant for the Japanese-Hiragana function key.
733 * This key switches to a Japanese input method and selects its Hiragana input mode.
734 * @since 1.2
735 */
736 /* Japanese Macintosh keyboard */
737 public static final int VK_JAPANESE_HIRAGANA = 0x0104;
738
739 /**
740 * Constant for the Japanese-Roman function key.
741 * This key switches to a Japanese input method and selects its Roman-Direct input mode.
742 * @since 1.2
743 */
744 /* Japanese Macintosh keyboard */
745 public static final int VK_JAPANESE_ROMAN = 0x0105;
746
747 /**
748 * Constant for the locking Kana function key.
749 * This key locks the keyboard into a Kana layout.
750 * @since 1.3
751 */
752 /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */
753 public static final int VK_KANA_LOCK = 0x0106;
754
755 /**
756 * Constant for the input method on/off key.
757 * @since 1.3
758 */
759 /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */
760 public static final int VK_INPUT_METHOD_ON_OFF = 0x0107;
761
762 /* for Sun keyboards */
763 /** @since 1.2 */
764 public static final int VK_CUT = 0xFFD1;
765 /** @since 1.2 */
766 public static final int VK_COPY = 0xFFCD;
767 /** @since 1.2 */
768 public static final int VK_PASTE = 0xFFCF;
769 /** @since 1.2 */
770 public static final int VK_UNDO = 0xFFCB;
771 /** @since 1.2 */
772 public static final int VK_AGAIN = 0xFFC9;
773 /** @since 1.2 */
774 public static final int VK_FIND = 0xFFD0;
775 /** @since 1.2 */
776 public static final int VK_PROPS = 0xFFCA;
777 /** @since 1.2 */
778 public static final int VK_STOP = 0xFFC8;
779
780 /**
781 * Constant for the Compose function key.
782 * @since 1.2
783 */
784 public static final int VK_COMPOSE = 0xFF20;
785
786 /**
787 * Constant for the AltGraph function key.
788 * @since 1.2
789 */
790 public static final int VK_ALT_GRAPH = 0xFF7E;
791
792 /**
793 * Constant for the Begin key.
794 * @since 1.5
795 */
796 public static final int VK_BEGIN = 0xFF58;
797
798 /**
799 * This value is used to indicate that the keyCode is unknown.
800 * KEY_TYPED events do not have a keyCode value; this value
801 * is used instead.
802 */
803 public static final int VK_UNDEFINED = 0x0;
804
805 /**
806 * KEY_PRESSED and KEY_RELEASED events which do not map to a
807 * valid Unicode character use this for the keyChar value.
808 */
809 public static final char CHAR_UNDEFINED = 0xFFFF;
810
811 /**
812 * A constant indicating that the keyLocation is indeterminate
813 * or not relevant.
814 * <code>KEY_TYPED</code> events do not have a keyLocation; this value
815 * is used instead.
816 * @since 1.4
817 */
818 public static final int KEY_LOCATION_UNKNOWN = 0;
819
820 /**
821 * A constant indicating that the key pressed or released
822 * is not distinguished as the left or right version of a key,
823 * and did not originate on the numeric keypad (or did not
824 * originate with a virtual key corresponding to the numeric
825 * keypad).
826 * @since 1.4
827 */
828 public static final int KEY_LOCATION_STANDARD = 1;
829
830 /**
831 * A constant indicating that the key pressed or released is in
832 * the left key location (there is more than one possible location
833 * for this key). Example: the left shift key.
834 * @since 1.4
835 */
836 public static final int KEY_LOCATION_LEFT = 2;
837
838 /**
839 * A constant indicating that the key pressed or released is in
840 * the right key location (there is more than one possible location
841 * for this key). Example: the right shift key.
842 * @since 1.4
843 */
844 public static final int KEY_LOCATION_RIGHT = 3;
845
846 /**
847 * A constant indicating that the key event originated on the
848 * numeric keypad or with a virtual key corresponding to the
849 * numeric keypad.
850 * @since 1.4
851 */
852 public static final int KEY_LOCATION_NUMPAD = 4;
853
854 /**
855 * The unique value assigned to each of the keys on the
856 * keyboard. There is a common set of key codes that
857 * can be fired by most keyboards.
858 * The symbolic name for a key code should be used rather
859 * than the code value itself.
860 *
861 * @serial
862 * @see #getKeyCode()
863 * @see #setKeyCode(int)
864 */
865 int keyCode;
866
867 /**
868 * <code>keyChar</code> is a valid unicode character
869 * that is fired by a key or a key combination on
870 * a keyboard.
871 *
872 * @serial
873 * @see #getKeyChar()
874 * @see #setKeyChar(char)
875 */
876 char keyChar;
877
878 /**
879 * The location of the key on the keyboard.
880 *
881 * Some keys occur more than once on a keyboard, e.g. the left and
882 * right shift keys. Additionally, some keys occur on the numeric
883 * keypad. This variable is used to distinguish such keys.
884 *
885 * The only legal values are <code>KEY_LOCATION_UNKNOWN</code>,
886 * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
887 * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
888 *
889 * @serial
890 * @see #getKeyLocation()
891 */
892 int keyLocation;
893
894 /*
895 * JDK 1.1 serialVersionUID
896 */
897 private static final long serialVersionUID = -2352130953028126954L;
898
899 static {
900 /* ensure that the necessary native libraries are loaded */
901 NativeLibLoader.loadLibraries();
902 if (!GraphicsEnvironment.isHeadless()) {
903 initIDs();
904 }
905 }
906
907 /**
908 * Initialize JNI field and method IDs for fields that may be
909 * accessed from C.
910 */
911 private static native void initIDs();
912
913 private KeyEvent(Component source, int id, long when, int modifiers,
914 int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
915 this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
916 this.isProxyActive = isProxyActive;
917 }
918
919 /**
920 * Constructs a <code>KeyEvent</code> object.
921 * <p>This method throws an
922 * <code>IllegalArgumentException</code> if <code>source</code>
923 * is <code>null</code>.
924 *
925 * @param source The <code>Component</code> that originated the event
926 * @param id An integer indicating the type of event.
927 * For information on allowable values, see
928 * the class description for {@link KeyEvent}
929 * @param when A long integer that specifies the time the event
930 * occurred.
931 * Passing negative or zero value
932 * is not recommended
933 * @param modifiers The modifier keys down during event (shift, ctrl,
934 * alt, meta).
935 * Passing negative value
936 * is not recommended.
937 * Zero value means that no modifiers were passed.
938 * Use either an extended _DOWN_MASK or old _MASK modifiers,
939 * however do not mix models in the one event.
940 * The extended modifiers are preferred for using
941 * @param keyCode The integer code for an actual key, or VK_UNDEFINED
942 * (for a key-typed event)
943 * @param keyChar The Unicode character generated by this event, or
944 * CHAR_UNDEFINED (for key-pressed and key-released
945 * events which do not map to a valid Unicode character)
946 * @param keyLocation Identifies the key location. The only legal
947 * values are <code>KEY_LOCATION_UNKNOWN</code>,
948 * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
949 * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
950 * @throws IllegalArgumentException
951 * if <code>id</code> is <code>KEY_TYPED</code> and
952 * <code>keyChar</code> is <code>CHAR_UNDEFINED</code>;
953 * or if <code>id</code> is <code>KEY_TYPED</code> and
954 * <code>keyCode</code> is not <code>VK_UNDEFINED</code>;
955 * or if <code>id</code> is <code>KEY_TYPED</code> and
956 * <code>keyLocation</code> is not <code>KEY_LOCATION_UNKNOWN</code>;
957 * or if <code>keyLocation</code> is not one of the legal
958 * values enumerated above.
959 * @throws IllegalArgumentException if <code>source</code> is null
960 * @see #getSource()
961 * @see #getID()
962 * @see #getWhen()
963 * @see #getModifiers()
964 * @see #getKeyCode()
965 * @see #getKeyChar()
966 * @see #getKeyLocation()
967 * @since 1.4
968 */
969 public KeyEvent(Component source, int id, long when, int modifiers,
970 int keyCode, char keyChar, int keyLocation) {
971 super(source, id, when, modifiers);
972 if (id == KEY_TYPED) {
973 if (keyChar == CHAR_UNDEFINED) {
974 throw new IllegalArgumentException("invalid keyChar");
975 }
976 if (keyCode != VK_UNDEFINED) {
977 throw new IllegalArgumentException("invalid keyCode");
978 }
979 if (keyLocation != KEY_LOCATION_UNKNOWN) {
980 throw new IllegalArgumentException("invalid keyLocation");
981 }
982 }
983
984 this.keyCode = keyCode;
985 this.keyChar = keyChar;
986
987 if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
988 (keyLocation > KEY_LOCATION_NUMPAD)) {
989 throw new IllegalArgumentException("invalid keyLocation");
990 }
991 this.keyLocation = keyLocation;
992 if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
993 setNewModifiers();
994 } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
995 setOldModifiers();
996 }
997 }
998
999 /**
1000 * Constructs a <code>KeyEvent</code> object.
1001 * <p> This method throws an
1002 * <code>IllegalArgumentException</code> if <code>source</code>
1003 * is <code>null</code>.
1004 *
1005 * @param source The <code>Component</code> that originated the event
1006 * @param id An integer indicating the type of event.
1007 * For information on allowable values, see
1008 * the class description for {@link KeyEvent}
1009 * @param when A long integer that specifies the time the event
1010 * occurred.
1011 * Passing negative or zero value
1012 * is not recommended
1013 * @param modifiers The modifier keys down during event (shift, ctrl,
1014 * alt, meta).
1015 * Passing negative value
1016 * is not recommended.
1017 * Zero value means that no modifiers were passed.
1018 * Use either an extended _DOWN_MASK or old _MASK modifiers,
1019 * however do not mix models in the one event.
1020 * The extended modifiers are preferred for using
1021 * @param keyCode The integer code for an actual key, or VK_UNDEFINED
1022 * (for a key-typed event)
1023 * @param keyChar The Unicode character generated by this event, or
1024 * CHAR_UNDEFINED (for key-pressed and key-released
1025 * events which do not map to a valid Unicode character)
1026 * @throws IllegalArgumentException if <code>id</code> is
1027 * <code>KEY_TYPED</code> and <code>keyChar</code> is
1028 * <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
1029 * <code>KEY_TYPED</code> and <code>keyCode</code> is not
1030 * <code>VK_UNDEFINED</code>
1031 * @throws IllegalArgumentException if <code>source</code> is null
1032 * @see #getSource()
1033 * @see #getID()
1034 * @see #getWhen()
1035 * @see #getModifiers()
1036 * @see #getKeyCode()
1037 * @see #getKeyChar()
1038 */
1039 public KeyEvent(Component source, int id, long when, int modifiers,
1040 int keyCode, char keyChar) {
1041 this(source, id, when, modifiers, keyCode, keyChar,
1042 KEY_LOCATION_UNKNOWN);
1043 }
1044
1045 /**
1046 * @deprecated as of JDK1.1
1047 */
1048 @Deprecated
1049 public KeyEvent(Component source, int id, long when, int modifiers,
1050 int keyCode) {
1051 this(source, id, when, modifiers, keyCode, (char)keyCode);
1052 }
1053
1054 /**
1055 * Returns the integer keyCode associated with the key in this event.
1056 *
1057 * @return the integer code for an actual key on the keyboard.
1058 * (For <code>KEY_TYPED</code> events, the keyCode is
1059 * <code>VK_UNDEFINED</code>.)
1060 */
1061 public int getKeyCode() {
1062 return keyCode;
1063 }
1064
1065 /**
1066 * Set the keyCode value to indicate a physical key.
1067 *
1068 * @param keyCode an integer corresponding to an actual key on the keyboard.
1069 */
1070 public void setKeyCode(int keyCode) {
1071 this.keyCode = keyCode;
1072 }
1073
1074 /**
1075 * Returns the character associated with the key in this event.
1076 * For example, the <code>KEY_TYPED</code> event for shift + "a"
1077 * returns the value for "A".
1078 * <p>
1079 * <code>KEY_PRESSED</code> and <code>KEY_RELEASED</code> events
1080 * are not intended for reporting of character input. Therefore,
1081 * the values returned by this method are guaranteed to be
1082 * meaningful only for <code>KEY_TYPED</code> events.
1083 *
1084 * @return the Unicode character defined for this key event.
1085 * If no valid Unicode character exists for this key event,
1086 * <code>CHAR_UNDEFINED</code> is returned.
1087 */
1088 public char getKeyChar() {
1089 return keyChar;
1090 }
1091
1092 /**
1093 * Set the keyChar value to indicate a logical character.
1094 *
1095 * @param keyChar a char corresponding to to the combination of keystrokes
1096 * that make up this event.
1097 */
1098 public void setKeyChar(char keyChar) {
1099 this.keyChar = keyChar;
1100 }
1101
1102 /**
1103 * Set the modifiers to indicate additional keys that were held down
1104 * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
1105 * <p>
1106 * NOTE: use of this method is not recommended, because many AWT
1107 * implementations do not recognize modifier changes. This is
1108 * especially true for <code>KEY_TYPED</code> events where the shift
1109 * modifier is changed.
1110 *
1111 * @param modifiers an integer combination of the modifier constants.
1112 * @see InputEvent
1113 * @deprecated as of JDK1.1.4
1114 */
1115 @Deprecated
1116 public void setModifiers(int modifiers) {
1117 this.modifiers = modifiers;
1118 if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1119 setNewModifiers();
1120 } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1121 setOldModifiers();
1122 }
1123 }
1124
1125 /**
1126 * Returns the location of the key that originated this key event.
1127 *
1128 * Some keys occur more than once on a keyboard, e.g. the left and
1129 * right shift keys. Additionally, some keys occur on the numeric
1130 * keypad. This provides a way of distinguishing such keys.
1131 *
1132 * @return the location of the key that was pressed or released.
1133 * Always returns <code>KEY_LOCATION_UNKNOWN</code> for
1134 * <code>KEY_TYPED</code> events.
1135 * @since 1.4
1136 */
1137 public int getKeyLocation() {
1138 return keyLocation;
1139 }
1140
1141 /**
1142 * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
1143 * These strings can be localized by changing the awt.properties file.
1144 *
1145 * @return a string containing a text description for a physical key,
1146 * identified by its keyCode
1147 */
1148 public static String getKeyText(int keyCode) {
1149 if (keyCode >= VK_0 && keyCode <= VK_9 ||
1150 keyCode >= VK_A && keyCode <= VK_Z) {
1151 return String.valueOf((char)keyCode);
1152 }
1153
1154 switch(keyCode) {
1155 case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
1156 case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
1157 case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
1158 case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
1159 case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
1160 case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
1161 case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
1162 case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
1163 case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
1164 case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
1165 case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
1166 case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
1167 case VK_END: return Toolkit.getProperty("AWT.end", "End");
1168 case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
1169 case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1170 case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
1171 case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1172 case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1173 case VK_BEGIN: return Toolkit.getProperty("AWT.begin", "Begin");
1174
1175 // modifiers
1176 case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
1177 case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
1178 case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
1179 case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
1180 case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
1181
1182 // punctuation
1183 case VK_COMMA: return Toolkit.getProperty("AWT.comma", "Comma");
1184 case VK_PERIOD: return Toolkit.getProperty("AWT.period", "Period");
1185 case VK_SLASH: return Toolkit.getProperty("AWT.slash", "Slash");
1186 case VK_SEMICOLON: return Toolkit.getProperty("AWT.semicolon", "Semicolon");
1187 case VK_EQUALS: return Toolkit.getProperty("AWT.equals", "Equals");
1188 case VK_OPEN_BRACKET: return Toolkit.getProperty("AWT.openBracket", "Open Bracket");
1189 case VK_BACK_SLASH: return Toolkit.getProperty("AWT.backSlash", "Back Slash");
1190 case VK_CLOSE_BRACKET: return Toolkit.getProperty("AWT.closeBracket", "Close Bracket");
1191
1192 // numpad numeric keys handled below
1193 case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
1194 case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
1195 case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,");
1196 case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
1197 case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
1198 case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
1199 case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
1200 case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
1201 case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
1202
1203 case VK_WINDOWS: return Toolkit.getProperty("AWT.windows", "Windows");
1204 case VK_CONTEXT_MENU: return Toolkit.getProperty("AWT.context", "Context Menu");
1205
1206 case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
1207 case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
1208 case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
1209 case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
1210 case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
1211 case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
1212 case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
1213 case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
1214 case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
1215 case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
1216 case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
1217 case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
1218 case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");
1219 case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");
1220 case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");
1221 case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");
1222 case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");
1223 case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");
1224 case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");
1225 case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");
1226 case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");
1227 case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");
1228 case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");
1229 case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");
1230
1231 case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
1232 case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
1233 case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
1234 case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
1235 case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
1236
1237 case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");
1238 case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1239 case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1240 case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1241
1242 case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
1243 case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
1244 case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex");
1245 case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
1246 case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
1247 case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
1248 case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot");
1249 case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis");
1250 case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring");
1251 case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute");
1252 case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
1253 case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla");
1254 case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
1255 case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
1256 case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound");
1257 case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound");
1258
1259 case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand");
1260 case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk");
1261 case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
1262 case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less");
1263 case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater");
1264 case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
1265 case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace");
1266 case VK_AT: return Toolkit.getProperty("AWT.at", "At");
1267 case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon");
1268 case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex");
1269 case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar");
1270 case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro");
1271 case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark");
1272 case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark");
1273 case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis");
1274 case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign");
1275 case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus");
1276 case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus");
1277 case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis");
1278 case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore");
1279
1280 case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
1281 case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
1282 case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
1283 case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
1284 case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
1285 case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
1286 case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
1287 case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric");
1288 case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana");
1289 case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana");
1290 case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
1291 case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
1292 case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters");
1293 case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates");
1294 case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate");
1295 case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input");
1296 case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana");
1297 case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana");
1298 case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman");
1299 case VK_KANA_LOCK: return Toolkit.getProperty("AWT.kanaLock", "Kana Lock");
1300 case VK_INPUT_METHOD_ON_OFF: return Toolkit.getProperty("AWT.inputMethodOnOff", "Input Method On/Off");
1301
1302 case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again");
1303 case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo");
1304 case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy");
1305 case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste");
1306 case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut");
1307 case VK_FIND: return Toolkit.getProperty("AWT.find", "Find");
1308 case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props");
1309 case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop");
1310 }
1311
1312 if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
1313 String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
1314 char c = (char)(keyCode - VK_NUMPAD0 + '0');
1315 return numpad + "-" + c;
1316 }
1317
1318 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1319 return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1320 }
1321
1322 /**
1323 * Returns a <code>String</code> describing the modifier key(s),
1324 * such as "Shift", or "Ctrl+Shift". These strings can be
1325 * localized by changing the <code>awt.properties</code> file.
1326 * <p>
1327 * Note that <code>InputEvent.ALT_MASK</code> and
1328 * <code>InputEvent.BUTTON2_MASK</code> have the same value,
1329 * so the string "Alt" is returned for both modifiers. Likewise,
1330 * <code>InputEvent.META_MASK</code> and
1331 * <code>InputEvent.BUTTON3_MASK</code> have the same value,
1332 * so the string "Meta" is returned for both modifiers.
1333 *
1334 * @return string a text description of the combination of modifier
1335 * keys that were held down during the event
1336 * @see InputEvent#getModifiersExText(int)
1337 */
1338 public static String getKeyModifiersText(int modifiers) {
1339 StringBuilder buf = new StringBuilder();
1340 if ((modifiers & InputEvent.META_MASK) != 0) {
1341 buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1342 buf.append("+");
1343 }
1344 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1345 buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1346 buf.append("+");
1347 }
1348 if ((modifiers & InputEvent.ALT_MASK) != 0) {
1349 buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1350 buf.append("+");
1351 }
1352 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1353 buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1354 buf.append("+");
1355 }
1356 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1357 buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
1358 buf.append("+");
1359 }
1360 if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1361 buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
1362 buf.append("+");
1363 }
1364 if (buf.length() > 0) {
1365 buf.setLength(buf.length()-1); // remove trailing '+'
1366 }
1367 return buf.toString();
1368 }
1369
1370
1371 /**
1372 * Returns whether the key in this event is an "action" key.
1373 * Typically an action key does not fire a unicode character and is
1374 * not a modifier key.
1375 *
1376 * @return <code>true</code> if the key is an "action" key,
1377 * <code>false</code> otherwise
1378 */
1379 public boolean isActionKey() {
1380 switch (keyCode) {
1381 case VK_HOME:
1382 case VK_END:
1383 case VK_PAGE_UP:
1384 case VK_PAGE_DOWN:
1385 case VK_UP:
1386 case VK_DOWN:
1387 case VK_LEFT:
1388 case VK_RIGHT:
1389 case VK_BEGIN:
1390
1391 case VK_KP_LEFT:
1392 case VK_KP_UP:
1393 case VK_KP_RIGHT:
1394 case VK_KP_DOWN:
1395
1396 case VK_F1:
1397 case VK_F2:
1398 case VK_F3:
1399 case VK_F4:
1400 case VK_F5:
1401 case VK_F6:
1402 case VK_F7:
1403 case VK_F8:
1404 case VK_F9:
1405 case VK_F10:
1406 case VK_F11:
1407 case VK_F12:
1408 case VK_F13:
1409 case VK_F14:
1410 case VK_F15:
1411 case VK_F16:
1412 case VK_F17:
1413 case VK_F18:
1414 case VK_F19:
1415 case VK_F20:
1416 case VK_F21:
1417 case VK_F22:
1418 case VK_F23:
1419 case VK_F24:
1420 case VK_PRINTSCREEN:
1421 case VK_SCROLL_LOCK:
1422 case VK_CAPS_LOCK:
1423 case VK_NUM_LOCK:
1424 case VK_PAUSE:
1425 case VK_INSERT:
1426
1427 case VK_FINAL:
1428 case VK_CONVERT:
1429 case VK_NONCONVERT:
1430 case VK_ACCEPT:
1431 case VK_MODECHANGE:
1432 case VK_KANA:
1433 case VK_KANJI:
1434 case VK_ALPHANUMERIC:
1435 case VK_KATAKANA:
1436 case VK_HIRAGANA:
1437 case VK_FULL_WIDTH:
1438 case VK_HALF_WIDTH:
1439 case VK_ROMAN_CHARACTERS:
1440 case VK_ALL_CANDIDATES:
1441 case VK_PREVIOUS_CANDIDATE:
1442 case VK_CODE_INPUT:
1443 case VK_JAPANESE_KATAKANA:
1444 case VK_JAPANESE_HIRAGANA:
1445 case VK_JAPANESE_ROMAN:
1446 case VK_KANA_LOCK:
1447 case VK_INPUT_METHOD_ON_OFF:
1448
1449 case VK_AGAIN:
1450 case VK_UNDO:
1451 case VK_COPY:
1452 case VK_PASTE:
1453 case VK_CUT:
1454 case VK_FIND:
1455 case VK_PROPS:
1456 case VK_STOP:
1457
1458 case VK_HELP:
1459 case VK_WINDOWS:
1460 case VK_CONTEXT_MENU:
1461 return true;
1462 }
1463 return false;
1464 }
1465
1466 /**
1467 * Returns a parameter string identifying this event.
1468 * This method is useful for event logging and for debugging.
1469 *
1470 * @return a string identifying the event and its attributes
1471 */
1472 public String paramString() {
1473 StringBuilder str = new StringBuilder(100);
1474
1475 switch (id) {
1476 case KEY_PRESSED:
1477 str.append("KEY_PRESSED");
1478 break;
1479 case KEY_RELEASED:
1480 str.append("KEY_RELEASED");
1481 break;
1482 case KEY_TYPED:
1483 str.append("KEY_TYPED");
1484 break;
1485 default:
1486 str.append("unknown type");
1487 break;
1488 }
1489
1490 str.append(",keyCode=").append(keyCode);
1491 str.append(",keyText=").append(getKeyText(keyCode));
1492
1493 /* Some keychars don't print well, e.g. escape, backspace,
1494 * tab, return, delete, cancel. Get keyText for the keyCode
1495 * instead of the keyChar.
1496 */
1497 str.append(",keyChar=");
1498 switch (keyChar) {
1499 case '\b':
1500 str.append(getKeyText(VK_BACK_SPACE));
1501 break;
1502 case '\t':
1503 str.append(getKeyText(VK_TAB));
1504 break;
1505 case '\n':
1506 str.append(getKeyText(VK_ENTER));
1507 break;
1508 case '\u0018':
1509 str.append(getKeyText(VK_CANCEL));
1510 break;
1511 case '\u001b':
1512 str.append(getKeyText(VK_ESCAPE));
1513 break;
1514 case '\u007f':
1515 str.append(getKeyText(VK_DELETE));
1516 break;
1517 case CHAR_UNDEFINED:
1518 str.append(Toolkit.getProperty("AWT.undefined", "Undefined"));
1519 str.append(" keyChar");
1520 break;
1521 default:
1522 str.append("'").append(keyChar).append("'");
1523 break;
1524 }
1525
1526 if (getModifiers() != 0) {
1527 str.append(",modifiers=").append(getKeyModifiersText(modifiers));
1528 }
1529 if (getModifiersEx() != 0) {
1530 str.append(",extModifiers=").append(getModifiersExText(modifiers));
1531 }
1532
1533 str.append(",keyLocation=");
1534 switch (keyLocation) {
1535 case KEY_LOCATION_UNKNOWN:
1536 str.append("KEY_LOCATION_UNKNOWN");
1537 break;
1538 case KEY_LOCATION_STANDARD:
1539 str.append("KEY_LOCATION_STANDARD");
1540 break;
1541 case KEY_LOCATION_LEFT:
1542 str.append("KEY_LOCATION_LEFT");
1543 break;
1544 case KEY_LOCATION_RIGHT:
1545 str.append("KEY_LOCATION_RIGHT");
1546 break;
1547 case KEY_LOCATION_NUMPAD:
1548 str.append("KEY_LOCATION_NUMPAD");
1549 break;
1550 default:
1551 str.append("KEY_LOCATION_UNKNOWN");
1552 break;
1553 }
1554
1555 return str.toString();
1556 }
1557
1558 /**
1559 * Sets new modifiers by the old ones. The key modifiers
1560 * override overlaping mouse modifiers.
1561 */
1562 private void setNewModifiers() {
1563 if ((modifiers & SHIFT_MASK) != 0) {
1564 modifiers |= SHIFT_DOWN_MASK;
1565 }
1566 if ((modifiers & ALT_MASK) != 0) {
1567 modifiers |= ALT_DOWN_MASK;
1568 }
1569 if ((modifiers & CTRL_MASK) != 0) {
1570 modifiers |= CTRL_DOWN_MASK;
1571 }
1572 if ((modifiers & META_MASK) != 0) {
1573 modifiers |= META_DOWN_MASK;
1574 }
1575 if ((modifiers & ALT_GRAPH_MASK) != 0) {
1576 modifiers |= ALT_GRAPH_DOWN_MASK;
1577 }
1578 if ((modifiers & BUTTON1_MASK) != 0) {
1579 modifiers |= BUTTON1_DOWN_MASK;
1580 }
1581 }
1582
1583 /**
1584 * Sets old modifiers by the new ones.
1585 */
1586 private void setOldModifiers() {
1587 if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1588 modifiers |= SHIFT_MASK;
1589 }
1590 if ((modifiers & ALT_DOWN_MASK) != 0) {
1591 modifiers |= ALT_MASK;
1592 }
1593 if ((modifiers & CTRL_DOWN_MASK) != 0) {
1594 modifiers |= CTRL_MASK;
1595 }
1596 if ((modifiers & META_DOWN_MASK) != 0) {
1597 modifiers |= META_MASK;
1598 }
1599 if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1600 modifiers |= ALT_GRAPH_MASK;
1601 }
1602 if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1603 modifiers |= BUTTON1_MASK;
1604 }
1605 }
1606
1607 /**
1608 * Sets new modifiers by the old ones. The key modifiers
1609 * override overlaping mouse modifiers.
1610 * @serial
1611 */
1612 private void readObject(ObjectInputStream s)
1613 throws IOException, ClassNotFoundException {
1614 s.defaultReadObject();
1615 if (getModifiers() != 0 && getModifiersEx() == 0) {
1616 setNewModifiers();
1617 }
1618 }
1619 }