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
23  * questions.
24  */
25 
26 package java.awt.event;
27 
28 import java.awt.Component;
29 import java.awt.GraphicsEnvironment;
30 import java.awt.Toolkit;
31 import java.io.IOException;
32 import java.io.ObjectInputStream;
33 import sun.awt.AWTAccessor;
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 {@code VK_UNDEFINED}. The {@code getExtendedKeyCode} method
69  * may also be used with many international keyboard layouts.
70  *
71  * <p>
72  * <em>"Key pressed" and "key released" events</em> are lower-level and depend
73  * on the platform and keyboard layout. They are generated whenever a key is
74  * pressed or released, and are the only way to find out about keys that don't
75  * generate character input (e.g., action keys, modifier keys, etc.). The key
76  * being pressed or released is indicated by the {@code getKeyCode} and {@code getExtendedKeyCode}
77  * methods, which return a virtual key code.
78  *
79  * <p>
80  * <em>Virtual key codes</em> are used to report which keyboard key has
81  * been pressed, rather than a character generated by the combination
82  * of one or more keystrokes (such as "A", which comes from shift and "a").
83  *
84  * <p>
85  * For example, pressing the Shift key will cause a KEY_PRESSED event
86  * with a VK_SHIFT keyCode, while pressing the 'a' key will result in
87  * a VK_A keyCode.  After the 'a' key is released, a KEY_RELEASED event
88  * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
89  * value of 'A' is generated.
90  *
91  * <p>
92  * Pressing and releasing a key on the keyboard results in the generating
93  * the following key events (in order):
94  * <PRE>
95  *    {@code KEY_PRESSED}
96  *    {@code KEY_TYPED} (is only generated if a valid Unicode character could be generated.)
97  *    {@code KEY_RELEASED}
98  * </PRE>
99  *
100  * But in some cases (e.g. auto-repeat or input method is activated) the order
101  * could be different (and platform dependent).
102  *
103  * <p>
104  * Notes:
105  * <ul>
106  * <li>Key combinations which do not result in Unicode characters, such as action
107  * keys like F1 and the HELP key, do not generate KEY_TYPED events.
108  * <li>Not all keyboards or systems are capable of generating all
109  * virtual key codes.  No attempt is made in Java to generate these keys
110  * artificially.
111  * <li>Virtual key codes do not identify a physical key: they depend on the
112  * platform and keyboard layout. For example, the key that generates VK_Q
113  * when using a U.S. keyboard layout will generate VK_A when using a French
114  * keyboard layout.
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="https://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
156      * - true, if when the event was created event proxying
157      *         mechanism was active
158      * - false, if it was inactive
159      * Used in Component.dispatchEventImpl to correctly dispatch
160      * events when proxy is active
161      */
162     private boolean isProxyActive = false;
163 
164     /**
165      * The first number in the range of ids used for key events.
166      */
167     public static final int KEY_FIRST = 400;
168 
169     /**
170      * The last number in the range of ids used for key events.
171      */
172     public static final int KEY_LAST  = 402;
173 
174     /**
175      * The "key typed" event.  This event is generated when a character is
176      * entered.  In the simplest case, it is produced by a single key press.
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 
232     /**
233      * Constant for the non-numpad <b>down</b> arrow key.
234      * @see #VK_KP_DOWN
235      */
236     public static final int VK_DOWN           = 0x28;
237 
238     /**
239      * Constant for the comma key, ","
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 
375     /** Constant for the F7 function key. */
376     public static final int VK_F7             = 0x76;
377 
378     /** Constant for the F8 function key. */
379     public static final int VK_F8             = 0x77;
380 
381     /** Constant for the F9 function key. */
382     public static final int VK_F9             = 0x78;
383 
384     /** Constant for the F10 function key. */
385     public static final int VK_F10            = 0x79;
386 
387     /** Constant for the F11 function key. */
388     public static final int VK_F11            = 0x7A;
389 
390     /** Constant for the F12 function key. */
391     public static final int VK_F12            = 0x7B;
392 
393     /**
394      * Constant for the F13 function key.
395      * @since 1.2
396      */
397     /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
398     public static final int VK_F13            = 0xF000;
399 
400     /**
401      * Constant for the F14 function key.
402      * @since 1.2
403      */
404     public static final int VK_F14            = 0xF001;
405 
406     /**
407      * Constant for the F15 function key.
408      * @since 1.2
409      */
410     public static final int VK_F15            = 0xF002;
411 
412     /**
413      * Constant for the F16 function key.
414      * @since 1.2
415      */
416     public static final int VK_F16            = 0xF003;
417 
418     /**
419      * Constant for the F17 function key.
420      * @since 1.2
421      */
422     public static final int VK_F17            = 0xF004;
423 
424     /**
425      * Constant for the F18 function key.
426      * @since 1.2
427      */
428     public static final int VK_F18            = 0xF005;
429 
430     /**
431      * Constant for the F19 function key.
432      * @since 1.2
433      */
434     public static final int VK_F19            = 0xF006;
435 
436     /**
437      * Constant for the F20 function key.
438      * @since 1.2
439      */
440     public static final int VK_F20            = 0xF007;
441 
442     /**
443      * Constant for the F21 function key.
444      * @since 1.2
445      */
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
492      */
493     public static final int VK_KP_LEFT        = 0xE2;
494 
495     /**
496      * Constant for the numeric keypad <b>right</b> arrow key.
497      * @see #VK_RIGHT
498      * @since 1.2
499      */
500     public static final int VK_KP_RIGHT       = 0xE3;
501 
502     /* For European keyboards */
503     /** @since 1.2 */
504     public static final int VK_DEAD_GRAVE               = 0x80;
505     /** @since 1.2 */
506     public static final int VK_DEAD_ACUTE               = 0x81;
507     /** @since 1.2 */
508     public static final int VK_DEAD_CIRCUMFLEX          = 0x82;
509     /** @since 1.2 */
510     public static final int VK_DEAD_TILDE               = 0x83;
511     /** @since 1.2 */
512     public static final int VK_DEAD_MACRON              = 0x84;
513     /** @since 1.2 */
514     public static final int VK_DEAD_BREVE               = 0x85;
515     /** @since 1.2 */
516     public static final int VK_DEAD_ABOVEDOT            = 0x86;
517     /** @since 1.2 */
518     public static final int VK_DEAD_DIAERESIS           = 0x87;
519     /** @since 1.2 */
520     public static final int VK_DEAD_ABOVERING           = 0x88;
521     /** @since 1.2 */
522     public static final int VK_DEAD_DOUBLEACUTE         = 0x89;
523     /** @since 1.2 */
524     public static final int VK_DEAD_CARON               = 0x8a;
525     /** @since 1.2 */
526     public static final int VK_DEAD_CEDILLA             = 0x8b;
527     /** @since 1.2 */
528     public static final int VK_DEAD_OGONEK              = 0x8c;
529     /** @since 1.2 */
530     public static final int VK_DEAD_IOTA                = 0x8d;
531     /** @since 1.2 */
532     public static final int VK_DEAD_VOICED_SOUND        = 0x8e;
533     /** @since 1.2 */
534     public static final int VK_DEAD_SEMIVOICED_SOUND    = 0x8f;
535 
536     /** @since 1.2 */
537     public static final int VK_AMPERSAND                = 0x96;
538     /** @since 1.2 */
539     public static final int VK_ASTERISK                 = 0x97;
540     /** @since 1.2 */
541     public static final int VK_QUOTEDBL                 = 0x98;
542     /** @since 1.2 */
543     public static final int VK_LESS                     = 0x99;
544 
545     /** @since 1.2 */
546     public static final int VK_GREATER                  = 0xa0;
547     /** @since 1.2 */
548     public static final int VK_BRACELEFT                = 0xa1;
549     /** @since 1.2 */
550     public static final int VK_BRACERIGHT               = 0xa2;
551 
552     /**
553      * Constant for the "@" key.
554      * @since 1.2
555      */
556     public static final int VK_AT                       = 0x0200;
557 
558     /**
559      * Constant for the ":" key.
560      * @since 1.2
561      */
562     public static final int VK_COLON                    = 0x0201;
563 
564     /**
565      * Constant for the "^" key.
566      * @since 1.2
567      */
568     public static final int VK_CIRCUMFLEX               = 0x0202;
569 
570     /**
571      * Constant for the "$" key.
572      * @since 1.2
573      */
574     public static final int VK_DOLLAR                   = 0x0203;
575 
576     /**
577      * Constant for the Euro currency sign key.
578      * @since 1.2
579      */
580     public static final int VK_EURO_SIGN                = 0x0204;
581 
582     /**
583      * Constant for the "!" key.
584      * @since 1.2
585      */
586     public static final int VK_EXCLAMATION_MARK         = 0x0205;
587 
588     /**
589      * Constant for the inverted exclamation mark key.
590      * @since 1.2
591      */
592     public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
593 
594     /**
595      * Constant for the "(" key.
596      * @since 1.2
597      */
598     public static final int VK_LEFT_PARENTHESIS         = 0x0207;
599 
600     /**
601      * Constant for the "#" key.
602      * @since 1.2
603      */
604     public static final int VK_NUMBER_SIGN              = 0x0208;
605 
606     /**
607      * Constant for the "+" key.
608      * @since 1.2
609      */
610     public static final int VK_PLUS                     = 0x0209;
611 
612     /**
613      * Constant for the ")" key.
614      * @since 1.2
615      */
616     public static final int VK_RIGHT_PARENTHESIS        = 0x020A;
617 
618     /**
619      * Constant for the "_" key.
620      * @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      */
684     /* Japanese PC 106 keyboard: hiragana */
685     public static final int VK_HIRAGANA                 = 0x00F2;
686 
687     /**
688      * Constant for the Full-Width Characters function key.
689      * @since 1.2
690      */
691     /* Japanese PC 106 keyboard: zenkaku */
692     public static final int VK_FULL_WIDTH               = 0x00F3;
693 
694     /**
695      * Constant for the Half-Width Characters function key.
696      * @since 1.2
697      */
698     /* Japanese PC 106 keyboard: hankaku */
699     public static final int VK_HALF_WIDTH               = 0x00F4;
700 
701     /**
702      * Constant for the Roman Characters function key.
703      * @since 1.2
704      */
705     /* Japanese PC 106 keyboard: roumaji */
706     public static final int VK_ROMAN_CHARACTERS         = 0x00F5;
707 
708     /**
709      * Constant for the All Candidates function key.
710      * @since 1.2
711      */
712     /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */
713     public static final int VK_ALL_CANDIDATES           = 0x0100;
714 
715     /**
716      * Constant for the Previous Candidate function key.
717      * @since 1.2
718      */
719     /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */
720     public static final int VK_PREVIOUS_CANDIDATE       = 0x0101;
721 
722     /**
723      * Constant for the Code Input function key.
724      * @since 1.2
725      */
726     /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */
727     public static final int VK_CODE_INPUT               = 0x0102;
728 
729     /**
730      * Constant for the Japanese-Katakana function key.
731      * This key switches to a Japanese input method and selects its Katakana input mode.
732      * @since 1.2
733      */
734     /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
735     public static final int VK_JAPANESE_KATAKANA        = 0x0103;
736 
737     /**
738      * Constant for the Japanese-Hiragana function key.
739      * This key switches to a Japanese input method and selects its Hiragana input mode.
740      * @since 1.2
741      */
742     /* Japanese Macintosh keyboard */
743     public static final int VK_JAPANESE_HIRAGANA        = 0x0104;
744 
745     /**
746      * Constant for the Japanese-Roman function key.
747      * This key switches to a Japanese input method and selects its Roman-Direct input mode.
748      * @since 1.2
749      */
750     /* Japanese Macintosh keyboard */
751     public static final int VK_JAPANESE_ROMAN           = 0x0105;
752 
753     /**
754      * Constant for the locking Kana function key.
755      * This key locks the keyboard into a Kana layout.
756      * @since 1.3
757      */
758     /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */
759     public static final int VK_KANA_LOCK                = 0x0106;
760 
761     /**
762      * Constant for the input method on/off key.
763      * @since 1.3
764      */
765     /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */
766     public static final int VK_INPUT_METHOD_ON_OFF      = 0x0107;
767 
768     /* for Sun keyboards */
769     /** @since 1.2 */
770     public static final int VK_CUT                      = 0xFFD1;
771     /** @since 1.2 */
772     public static final int VK_COPY                     = 0xFFCD;
773     /** @since 1.2 */
774     public static final int VK_PASTE                    = 0xFFCF;
775     /** @since 1.2 */
776     public static final int VK_UNDO                     = 0xFFCB;
777     /** @since 1.2 */
778     public static final int VK_AGAIN                    = 0xFFC9;
779     /** @since 1.2 */
780     public static final int VK_FIND                     = 0xFFD0;
781     /** @since 1.2 */
782     public static final int VK_PROPS                    = 0xFFCA;
783     /** @since 1.2 */
784     public static final int VK_STOP                     = 0xFFC8;
785 
786     /**
787      * Constant for the Compose function key.
788      * @since 1.2
789      */
790     public static final int VK_COMPOSE                  = 0xFF20;
791 
792     /**
793      * Constant for the AltGraph function key.
794      * @since 1.2
795      */
796     public static final int VK_ALT_GRAPH                = 0xFF7E;
797 
798     /**
799      * Constant for the Begin key.
800      * @since 1.5
801      */
802     public static final int VK_BEGIN                    = 0xFF58;
803 
804     /**
805      * This value is used to indicate that the keyCode is unknown.
806      * KEY_TYPED events do not have a keyCode value; this value
807      * is used instead.
808      */
809     public static final int VK_UNDEFINED      = 0x0;
810 
811     /**
812      * KEY_PRESSED and KEY_RELEASED events which do not map to a
813      * valid Unicode character use this for the keyChar value.
814      */
815     public static final char CHAR_UNDEFINED   = 0xFFFF;
816 
817     /**
818      * A constant indicating that the keyLocation is indeterminate
819      * or not relevant.
820      * <code>KEY_TYPED</code> events do not have a keyLocation; this value
821      * is used instead.
822      * @since 1.4
823      */
824     public static final int KEY_LOCATION_UNKNOWN  = 0;
825 
826     /**
827      * A constant indicating that the key pressed or released
828      * is not distinguished as the left or right version of a key,
829      * and did not originate on the numeric keypad (or did not
830      * originate with a virtual key corresponding to the numeric
831      * keypad).
832      * @since 1.4
833      */
834     public static final int KEY_LOCATION_STANDARD = 1;
835 
836     /**
837      * A constant indicating that the key pressed or released is in
838      * the left key location (there is more than one possible location
839      * for this key).  Example: the left shift key.
840      * @since 1.4
841      */
842     public static final int KEY_LOCATION_LEFT     = 2;
843 
844     /**
845      * A constant indicating that the key pressed or released is in
846      * the right key location (there is more than one possible location
847      * for this key).  Example: the right shift key.
848      * @since 1.4
849      */
850     public static final int KEY_LOCATION_RIGHT    = 3;
851 
852     /**
853      * A constant indicating that the key event originated on the
854      * numeric keypad or with a virtual key corresponding to the
855      * numeric keypad.
856      * @since 1.4
857      */
858     public static final int KEY_LOCATION_NUMPAD   = 4;
859 
860     /**
861      * The unique value assigned to each of the keys on the
862      * keyboard.  There is a common set of key codes that
863      * can be fired by most keyboards.
864      * The symbolic name for a key code should be used rather
865      * than the code value itself.
866      *
867      * @serial
868      * @see #getKeyCode()
869      * @see #setKeyCode(int)
870      */
871     int  keyCode;
872 
873     /**
874      * <code>keyChar</code> is a valid unicode character
875      * that is fired by a key or a key combination on
876      * a keyboard.
877      *
878      * @serial
879      * @see #getKeyChar()
880      * @see #setKeyChar(char)
881      */
882     char keyChar;
883 
884     /**
885      * The location of the key on the keyboard.
886      *
887      * Some keys occur more than once on a keyboard, e.g. the left and
888      * right shift keys.  Additionally, some keys occur on the numeric
889      * keypad.  This variable is used to distinguish such keys.
890      *
891      * The only legal values are <code>KEY_LOCATION_UNKNOWN</code>,
892      * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
893      * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
894      *
895      * @serial
896      * @see #getKeyLocation()
897      */
898     int keyLocation;
899 
900     //set from native code.
901     private transient long rawCode = 0;
902     private transient long primaryLevelUnicode = 0;
903     private transient long scancode = 0; // for MS Windows only
904     private transient long extendedKeyCode = 0;
905 
906     /*
907      * JDK 1.1 serialVersionUID
908      */
909     private static final long serialVersionUID = -2352130953028126954L;
910 
911     static {
912         /* ensure that the necessary native libraries are loaded */
NativeLibLoader.loadLibraries()913         NativeLibLoader.loadLibraries();
914         if (!GraphicsEnvironment.isHeadless()) {
initIDs()915             initIDs();
916         }
917 
AWTAccessor.setKeyEventAccessor( new AWTAccessor.KeyEventAccessor() { public void setRawCode(KeyEvent ev, long rawCode) { ev.rawCode = rawCode; } public void setPrimaryLevelUnicode(KeyEvent ev, long primaryLevelUnicode) { ev.primaryLevelUnicode = primaryLevelUnicode; } public void setExtendedKeyCode(KeyEvent ev, long extendedKeyCode) { ev.extendedKeyCode = extendedKeyCode; } public Component getOriginalSource( KeyEvent ev ) { return ev.originalSource; } })918         AWTAccessor.setKeyEventAccessor(
919             new AWTAccessor.KeyEventAccessor() {
920                 public void setRawCode(KeyEvent ev, long rawCode) {
921                     ev.rawCode = rawCode;
922                 }
923 
924                 public void setPrimaryLevelUnicode(KeyEvent ev,
925                                                    long primaryLevelUnicode) {
926                     ev.primaryLevelUnicode = primaryLevelUnicode;
927                 }
928 
929                 public void setExtendedKeyCode(KeyEvent ev,
930                                                long extendedKeyCode) {
931                     ev.extendedKeyCode = extendedKeyCode;
932                 }
933 
934                 public Component getOriginalSource( KeyEvent ev ) {
935                     return ev.originalSource;
936                 }
937             });
938     }
939 
940     /**
941      * Initialize JNI field and method IDs for fields that may be
942      * accessed from C.
943      */
initIDs()944     private static native void initIDs();
945 
946     /**
947      * The original event source.
948      *
949      * Event source can be changed during processing, but in some cases
950      * we need to be able to obtain original source.
951      */
952     private Component originalSource;
953 
KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation, boolean isProxyActive)954     private KeyEvent(Component source, int id, long when, int modifiers,
955                     int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
956         this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
957         this.isProxyActive = isProxyActive;
958     }
959 
960     /**
961      * Constructs a <code>KeyEvent</code> object.
962      * <p>This method throws an
963      * <code>IllegalArgumentException</code> if <code>source</code>
964      * is <code>null</code>.
965      *
966      * @param source    The <code>Component</code> that originated the event
967      * @param id              An integer indicating the type of event.
968      *                  For information on allowable values, see
969      *                  the class description for {@link KeyEvent}
970      * @param when      A long integer that specifies the time the event
971      *                  occurred.
972      *                     Passing negative or zero value
973      *                     is not recommended
974      * @param modifiers The modifier keys down during event (shift, ctrl,
975      *                  alt, meta).
976      *                     Passing negative value
977      *                     is not recommended.
978      *                     Zero value means that no modifiers were passed.
979      *                  Use either an extended _DOWN_MASK or old _MASK modifiers,
980      *                  however do not mix models in the one event.
981      *                  The extended modifiers are preferred for using
982      * @param keyCode   The integer code for an actual key, or VK_UNDEFINED
983      *                  (for a key-typed event)
984      * @param keyChar   The Unicode character generated by this event, or
985      *                  CHAR_UNDEFINED (for key-pressed and key-released
986      *                  events which do not map to a valid Unicode character)
987      * @param keyLocation  Identifies the key location.  The only legal
988      *        values are <code>KEY_LOCATION_UNKNOWN</code>,
989      *        <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
990      *        <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
991      * @throws IllegalArgumentException
992      *     if <code>id</code> is <code>KEY_TYPED</code> and
993      *       <code>keyChar</code> is <code>CHAR_UNDEFINED</code>;
994      *     or if <code>id</code> is <code>KEY_TYPED</code> and
995      *       <code>keyCode</code> is not <code>VK_UNDEFINED</code>;
996      *     or if <code>id</code> is <code>KEY_TYPED</code> and
997      *       <code>keyLocation</code> is not <code>KEY_LOCATION_UNKNOWN</code>;
998      *     or if <code>keyLocation</code> is not one of the legal
999      *       values enumerated above.
1000      * @throws IllegalArgumentException if <code>source</code> is null
1001      * @see #getSource()
1002      * @see #getID()
1003      * @see #getWhen()
1004      * @see #getModifiers()
1005      * @see #getKeyCode()
1006      * @see #getKeyChar()
1007      * @see #getKeyLocation()
1008      * @since 1.4
1009      */
KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)1010     public KeyEvent(Component source, int id, long when, int modifiers,
1011                     int keyCode, char keyChar, int keyLocation) {
1012         super(source, id, when, modifiers);
1013         if (id == KEY_TYPED) {
1014             if (keyChar == CHAR_UNDEFINED) {
1015                 throw new IllegalArgumentException("invalid keyChar");
1016             }
1017             if (keyCode != VK_UNDEFINED) {
1018                 throw new IllegalArgumentException("invalid keyCode");
1019             }
1020             if (keyLocation != KEY_LOCATION_UNKNOWN) {
1021                 throw new IllegalArgumentException("invalid keyLocation");
1022             }
1023         }
1024 
1025         this.keyCode = keyCode;
1026         this.keyChar = keyChar;
1027 
1028         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
1029             (keyLocation > KEY_LOCATION_NUMPAD)) {
1030             throw new IllegalArgumentException("invalid keyLocation");
1031         }
1032         this.keyLocation = keyLocation;
1033         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1034             setNewModifiers();
1035         } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1036             setOldModifiers();
1037         }
1038         originalSource = source;
1039     }
1040 
1041     /**
1042      * Constructs a <code>KeyEvent</code> object.
1043      * <p> This method throws an
1044      * <code>IllegalArgumentException</code> if <code>source</code>
1045      * is <code>null</code>.
1046      *
1047      * @param source    The <code>Component</code> that originated the event
1048      * @param id              An integer indicating the type of event.
1049      *                  For information on allowable values, see
1050      *                  the class description for {@link KeyEvent}
1051      * @param when      A long integer that specifies the time the event
1052      *                  occurred.
1053      *                     Passing negative or zero value
1054      *                     is not recommended
1055      * @param modifiers The modifier keys down during event (shift, ctrl,
1056      *                  alt, meta).
1057      *                     Passing negative value
1058      *                     is not recommended.
1059      *                     Zero value means that no modifiers were passed.
1060      *                  Use either an extended _DOWN_MASK or old _MASK modifiers,
1061      *                  however do not mix models in the one event.
1062      *                  The extended modifiers are preferred for using
1063      * @param keyCode   The integer code for an actual key, or VK_UNDEFINED
1064      *                  (for a key-typed event)
1065      * @param keyChar   The Unicode character generated by this event, or
1066      *                  CHAR_UNDEFINED (for key-pressed and key-released
1067      *                  events which do not map to a valid Unicode character)
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      */
KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)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
KeyEvent(Component source, int id, long when, int modifiers, int keyCode)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      */
getKeyCode()1103     public int getKeyCode() {
1104         return keyCode;
1105     }
1106 
1107     /**
1108      * Set the keyCode value to indicate a physical key.
1109      *
1110      * @param keyCode an integer corresponding to an actual key on the keyboard.
1111      */
setKeyCode(int keyCode)1112     public void setKeyCode(int keyCode) {
1113         this.keyCode = keyCode;
1114     }
1115 
1116     /**
1117      * Returns the character associated with the key in this event.
1118      * For example, the <code>KEY_TYPED</code> event for shift + "a"
1119      * returns the value for "A".
1120      * <p>
1121      * <code>KEY_PRESSED</code> and <code>KEY_RELEASED</code> events
1122      * are not intended for reporting of character input.  Therefore,
1123      * the values returned by this method are guaranteed to be
1124      * meaningful only for <code>KEY_TYPED</code> events.
1125      *
1126      * @return the Unicode character defined for this key event.
1127      *         If no valid Unicode character exists for this key event,
1128      *         <code>CHAR_UNDEFINED</code> is returned.
1129      */
getKeyChar()1130     public char getKeyChar() {
1131         return keyChar;
1132     }
1133 
1134     /**
1135      * Set the keyChar value to indicate a logical character.
1136      *
1137      * @param keyChar a char corresponding to to the combination of keystrokes
1138      *                that make up this event.
1139      */
setKeyChar(char keyChar)1140     public void setKeyChar(char keyChar) {
1141         this.keyChar = keyChar;
1142     }
1143 
1144     /**
1145      * Set the modifiers to indicate additional keys that were held down
1146      * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
1147      * <p>
1148      * NOTE:  use of this method is not recommended, because many AWT
1149      * implementations do not recognize modifier changes.  This is
1150      * especially true for <code>KEY_TYPED</code> events where the shift
1151      * modifier is changed.
1152      *
1153      * @param modifiers an integer combination of the modifier constants.
1154      * @see InputEvent
1155      * @deprecated as of JDK1.1.4
1156      */
1157     @Deprecated
setModifiers(int modifiers)1158     public void setModifiers(int modifiers) {
1159         this.modifiers = modifiers;
1160         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1161             setNewModifiers();
1162         } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1163             setOldModifiers();
1164         }
1165     }
1166 
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      */
getKeyLocation()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      */
getKeyText(int keyCode)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");
1207           case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
1208           case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
1209           case VK_END: return Toolkit.getProperty("AWT.end", "End");
1210           case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
1211           case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1212           case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
1213           case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1214           case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1215           case VK_BEGIN: return Toolkit.getProperty("AWT.begin", "Begin");
1216 
1217           // modifiers
1218           case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
1219           case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
1220           case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
1221           case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
1222           case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
1223 
1224           // punctuation
1225           case VK_COMMA: return Toolkit.getProperty("AWT.comma", "Comma");
1226           case VK_PERIOD: return Toolkit.getProperty("AWT.period", "Period");
1227           case VK_SLASH: return Toolkit.getProperty("AWT.slash", "Slash");
1228           case VK_SEMICOLON: return Toolkit.getProperty("AWT.semicolon", "Semicolon");
1229           case VK_EQUALS: return Toolkit.getProperty("AWT.equals", "Equals");
1230           case VK_OPEN_BRACKET: return Toolkit.getProperty("AWT.openBracket", "Open Bracket");
1231           case VK_BACK_SLASH: return Toolkit.getProperty("AWT.backSlash", "Back Slash");
1232           case VK_CLOSE_BRACKET: return Toolkit.getProperty("AWT.closeBracket", "Close Bracket");
1233 
1234           // numpad numeric keys handled below
1235           case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
1236           case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
1237           case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,");
1238           case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
1239           case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
1240           case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
1241           case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
1242           case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
1243           case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
1244 
1245           case VK_WINDOWS: return Toolkit.getProperty("AWT.windows", "Windows");
1246           case VK_CONTEXT_MENU: return Toolkit.getProperty("AWT.context", "Context Menu");
1247 
1248           case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
1249           case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
1250           case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
1251           case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
1252           case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
1253           case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
1254           case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
1255           case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
1256           case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
1257           case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
1258           case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
1259           case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
1260           case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");
1261           case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");
1262           case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");
1263           case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");
1264           case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");
1265           case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");
1266           case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");
1267           case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");
1268           case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");
1269           case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");
1270           case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");
1271           case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");
1272 
1273           case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
1274           case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
1275           case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
1276           case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
1277           case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
1278 
1279           case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");
1280           case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1281           case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1282           case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1283 
1284           case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
1285           case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
1286           case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex");
1287           case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
1288           case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
1289           case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
1290           case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot");
1291           case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis");
1292           case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring");
1293           case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute");
1294           case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
1295           case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla");
1296           case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
1297           case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
1298           case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound");
1299           case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound");
1300 
1301           case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand");
1302           case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk");
1303           case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
1304           case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less");
1305           case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater");
1306           case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
1307           case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace");
1308           case VK_AT: return Toolkit.getProperty("AWT.at", "At");
1309           case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon");
1310           case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex");
1311           case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar");
1312           case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro");
1313           case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark");
1314           case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark");
1315           case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis");
1316           case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign");
1317           case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus");
1318           case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus");
1319           case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis");
1320           case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore");
1321 
1322           case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
1323           case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
1324           case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
1325           case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
1326           case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
1327           case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
1328           case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
1329           case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric");
1330           case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana");
1331           case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana");
1332           case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
1333           case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
1334           case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters");
1335           case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates");
1336           case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate");
1337           case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input");
1338           case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana");
1339           case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana");
1340           case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman");
1341           case VK_KANA_LOCK: return Toolkit.getProperty("AWT.kanaLock", "Kana Lock");
1342           case VK_INPUT_METHOD_ON_OFF: return Toolkit.getProperty("AWT.inputMethodOnOff", "Input Method On/Off");
1343 
1344           case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again");
1345           case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo");
1346           case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy");
1347           case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste");
1348           case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut");
1349           case VK_FIND: return Toolkit.getProperty("AWT.find", "Find");
1350           case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props");
1351           case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop");
1352         }
1353 
1354         if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
1355             String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
1356             char c = (char)(keyCode - VK_NUMPAD0 + '0');
1357             return numpad + "-" + c;
1358         }
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      */
getKeyModifiersText(int modifiers)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"));
1399             buf.append("+");
1400         }
1401         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1402             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
1403             buf.append("+");
1404         }
1405         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1406             buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
1407             buf.append("+");
1408         }
1409         if (buf.length() > 0) {
1410             buf.setLength(buf.length()-1); // remove trailing '+'
1411         }
1412         return buf.toString();
1413     }
1414 
1415 
1416     /**
1417      * Returns whether the key in this event is an "action" key.
1418      * Typically an action key does not fire a unicode character and is
1419      * not a modifier key.
1420      *
1421      * @return <code>true</code> if the key is an "action" key,
1422      *         <code>false</code> otherwise
1423      */
isActionKey()1424     public boolean isActionKey() {
1425         switch (keyCode) {
1426           case VK_HOME:
1427           case VK_END:
1428           case VK_PAGE_UP:
1429           case VK_PAGE_DOWN:
1430           case VK_UP:
1431           case VK_DOWN:
1432           case VK_LEFT:
1433           case VK_RIGHT:
1434           case VK_BEGIN:
1435 
1436           case VK_KP_LEFT:
1437           case VK_KP_UP:
1438           case VK_KP_RIGHT:
1439           case VK_KP_DOWN:
1440 
1441           case VK_F1:
1442           case VK_F2:
1443           case VK_F3:
1444           case VK_F4:
1445           case VK_F5:
1446           case VK_F6:
1447           case VK_F7:
1448           case VK_F8:
1449           case VK_F9:
1450           case VK_F10:
1451           case VK_F11:
1452           case VK_F12:
1453           case VK_F13:
1454           case VK_F14:
1455           case VK_F15:
1456           case VK_F16:
1457           case VK_F17:
1458           case VK_F18:
1459           case VK_F19:
1460           case VK_F20:
1461           case VK_F21:
1462           case VK_F22:
1463           case VK_F23:
1464           case VK_F24:
1465           case VK_PRINTSCREEN:
1466           case VK_SCROLL_LOCK:
1467           case VK_CAPS_LOCK:
1468           case VK_NUM_LOCK:
1469           case VK_PAUSE:
1470           case VK_INSERT:
1471 
1472           case VK_FINAL:
1473           case VK_CONVERT:
1474           case VK_NONCONVERT:
1475           case VK_ACCEPT:
1476           case VK_MODECHANGE:
1477           case VK_KANA:
1478           case VK_KANJI:
1479           case VK_ALPHANUMERIC:
1480           case VK_KATAKANA:
1481           case VK_HIRAGANA:
1482           case VK_FULL_WIDTH:
1483           case VK_HALF_WIDTH:
1484           case VK_ROMAN_CHARACTERS:
1485           case VK_ALL_CANDIDATES:
1486           case VK_PREVIOUS_CANDIDATE:
1487           case VK_CODE_INPUT:
1488           case VK_JAPANESE_KATAKANA:
1489           case VK_JAPANESE_HIRAGANA:
1490           case VK_JAPANESE_ROMAN:
1491           case VK_KANA_LOCK:
1492           case VK_INPUT_METHOD_ON_OFF:
1493 
1494           case VK_AGAIN:
1495           case VK_UNDO:
1496           case VK_COPY:
1497           case VK_PASTE:
1498           case VK_CUT:
1499           case VK_FIND:
1500           case VK_PROPS:
1501           case VK_STOP:
1502 
1503           case VK_HELP:
1504           case VK_WINDOWS:
1505           case VK_CONTEXT_MENU:
1506               return true;
1507         }
1508         return false;
1509     }
1510 
1511     /**
1512      * Returns a parameter string identifying this event.
1513      * This method is useful for event logging and for debugging.
1514      *
1515      * @return a string identifying the event and its attributes
1516      */
paramString()1517     public String paramString() {
1518         StringBuilder str = new StringBuilder(100);
1519 
1520         switch (id) {
1521           case KEY_PRESSED:
1522             str.append("KEY_PRESSED");
1523             break;
1524           case KEY_RELEASED:
1525             str.append("KEY_RELEASED");
1526             break;
1527           case KEY_TYPED:
1528             str.append("KEY_TYPED");
1529             break;
1530           default:
1531             str.append("unknown type");
1532             break;
1533         }
1534 
1535         str.append(",keyCode=").append(keyCode);
1536         str.append(",keyText=").append(getKeyText(keyCode));
1537 
1538         /* Some keychars don't print well, e.g. escape, backspace,
1539          * tab, return, delete, cancel.  Get keyText for the keyCode
1540          * instead of the keyChar.
1541          */
1542         str.append(",keyChar=");
1543         switch (keyChar) {
1544           case '\b':
1545             str.append(getKeyText(VK_BACK_SPACE));
1546             break;
1547           case '\t':
1548             str.append(getKeyText(VK_TAB));
1549             break;
1550           case '\n':
1551             str.append(getKeyText(VK_ENTER));
1552             break;
1553           case '\u0018':
1554             str.append(getKeyText(VK_CANCEL));
1555             break;
1556           case '\u001b':
1557             str.append(getKeyText(VK_ESCAPE));
1558             break;
1559           case '\u007f':
1560             str.append(getKeyText(VK_DELETE));
1561             break;
1562           case CHAR_UNDEFINED:
1563             str.append(Toolkit.getProperty("AWT.undefined", "Undefined"));
1564             str.append(" keyChar");
1565             break;
1566           default:
1567             str.append("'").append(keyChar).append("'");
1568             break;
1569         }
1570 
1571         if (getModifiers() != 0) {
1572             str.append(",modifiers=").append(getKeyModifiersText(modifiers));
1573         }
1574         if (getModifiersEx() != 0) {
1575             str.append(",extModifiers=").append(getModifiersExText(modifiers));
1576         }
1577 
1578         str.append(",keyLocation=");
1579         switch (keyLocation) {
1580           case KEY_LOCATION_UNKNOWN:
1581             str.append("KEY_LOCATION_UNKNOWN");
1582             break;
1583           case KEY_LOCATION_STANDARD:
1584             str.append("KEY_LOCATION_STANDARD");
1585             break;
1586           case KEY_LOCATION_LEFT:
1587             str.append("KEY_LOCATION_LEFT");
1588             break;
1589           case KEY_LOCATION_RIGHT:
1590             str.append("KEY_LOCATION_RIGHT");
1591             break;
1592           case KEY_LOCATION_NUMPAD:
1593             str.append("KEY_LOCATION_NUMPAD");
1594             break;
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      */
getExtendedKeyCode()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      */
getExtendedKeyCodeForChar(int c)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      */
setNewModifiers()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         }
1652         if ((modifiers & META_MASK) != 0) {
1653             modifiers |= META_DOWN_MASK;
1654         }
1655         if ((modifiers & ALT_GRAPH_MASK) != 0) {
1656             modifiers |= ALT_GRAPH_DOWN_MASK;
1657         }
1658         if ((modifiers & BUTTON1_MASK) != 0) {
1659             modifiers |= BUTTON1_DOWN_MASK;
1660         }
1661     }
1662 
1663     /**
1664      * Sets old modifiers by the new ones.
1665      */
setOldModifiers()1666     private void setOldModifiers() {
1667         if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1668             modifiers |= SHIFT_MASK;
1669         }
1670         if ((modifiers & ALT_DOWN_MASK) != 0) {
1671             modifiers |= ALT_MASK;
1672         }
1673         if ((modifiers & CTRL_DOWN_MASK) != 0) {
1674             modifiers |= CTRL_MASK;
1675         }
1676         if ((modifiers & META_DOWN_MASK) != 0) {
1677             modifiers |= META_MASK;
1678         }
1679         if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1680             modifiers |= ALT_GRAPH_MASK;
1681         }
1682         if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1683             modifiers |= BUTTON1_MASK;
1684         }
1685     }
1686 
1687     /**
1688      * Sets new modifiers by the old ones. The key modifiers
1689      * override overlaping mouse modifiers.
1690      * @serial
1691      */
readObject(ObjectInputStream s)1692     private void readObject(ObjectInputStream s)
1693       throws IOException, ClassNotFoundException {
1694         s.defaultReadObject();
1695         if (getModifiers() != 0 && getModifiersEx() == 0) {
1696             setNewModifiers();
1697         }
1698     }
1699 }
1700