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}
41  * or {@code KeyAdapter} object which registered to receive such
42  * events using the component's {@code addKeyListener} method.
43  * ({@code KeyAdapter} objects implement the
44  * {@code KeyListener} interface.)  Each such listener object
45  * gets this {@code KeyEvent} 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.  The platform steward reserves the right to change these values as needed
136  * to accommodate a wider range of keyboards in the future.
137  * <p>
138  * An unspecified behavior will be caused if the {@code id} parameter
139  * of any particular {@code KeyEvent} instance is not
140  * in the range from {@code KEY_FIRST} to {@code KEY_LAST}.
141  *
142  * @author Carl Quinn
143  * @author Amy Fowler
144  * @author Norbert Lindenberg
145  *
146  * @see KeyAdapter
147  * @see KeyListener
148  * @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html">Tutorial: Writing a Key Listener</a>
149  *
150  * @since 1.1
151  */
152 public class KeyEvent extends InputEvent {
153 
154     /**
155      * Stores the state of native event dispatching system
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     /** Constant for the ENTER virtual key. */
198     public static final int VK_ENTER          = '\n';
199 
200     /** Constant for the BACK_SPACE virtual key. */
201     public static final int VK_BACK_SPACE     = '\b';
202 
203     /** Constant for the TAB virtual key. */
204     public static final int VK_TAB            = '\t';
205 
206     /** Constant for the CANCEL virtual key. */
207     public static final int VK_CANCEL         = 0x03;
208 
209     /** Constant for the CLEAR virtual key. */
210     public static final int VK_CLEAR          = 0x0C;
211 
212     /** Constant for the SHIFT virtual key. */
213     public static final int VK_SHIFT          = 0x10;
214 
215     /** Constant for the CONTROL virtual key. */
216     public static final int VK_CONTROL        = 0x11;
217 
218     /** Constant for the ALT virtual key. */
219     public static final int VK_ALT            = 0x12;
220 
221     /** Constant for the PAUSE virtual key. */
222     public static final int VK_PAUSE          = 0x13;
223 
224     /** Constant for the CAPS_LOCK virtual key. */
225     public static final int VK_CAPS_LOCK      = 0x14;
226 
227     /** Constant for the ESCAPE virtual key. */
228     public static final int VK_ESCAPE         = 0x1B;
229 
230     /** Constant for the SPACE virtual key. */
231     public static final int VK_SPACE          = 0x20;
232 
233     /** Constant for the PAGE_UP virtual key. */
234     public static final int VK_PAGE_UP        = 0x21;
235 
236     /** Constant for the PAGE_DOWN virtual key. */
237     public static final int VK_PAGE_DOWN      = 0x22;
238 
239     /** Constant for the END virtual key. */
240     public static final int VK_END            = 0x23;
241 
242     /** Constant for the HOME virtual key. */
243     public static final int VK_HOME           = 0x24;
244 
245     /**
246      * Constant for the non-numpad <b>left</b> arrow key.
247      * @see #VK_KP_LEFT
248      */
249     public static final int VK_LEFT           = 0x25;
250 
251     /**
252      * Constant for the non-numpad <b>up</b> arrow key.
253      * @see #VK_KP_UP
254      */
255     public static final int VK_UP             = 0x26;
256 
257     /**
258      * Constant for the non-numpad <b>right</b> arrow key.
259      * @see #VK_KP_RIGHT
260      */
261     public static final int VK_RIGHT          = 0x27;
262 
263     /**
264      * Constant for the non-numpad <b>down</b> arrow key.
265      * @see #VK_KP_DOWN
266      */
267     public static final int VK_DOWN           = 0x28;
268 
269     /**
270      * Constant for the comma key, ","
271      */
272     public static final int VK_COMMA          = 0x2C;
273 
274     /**
275      * Constant for the minus key, "-"
276      * @since 1.2
277      */
278     public static final int VK_MINUS          = 0x2D;
279 
280     /**
281      * Constant for the period key, "."
282      */
283     public static final int VK_PERIOD         = 0x2E;
284 
285     /**
286      * Constant for the forward slash key, "/"
287      */
288     public static final int VK_SLASH          = 0x2F;
289 
290     /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
291 
292     /** Constant for the "0" key. */
293     public static final int VK_0              = 0x30;
294 
295     /** Constant for the "1" key. */
296     public static final int VK_1              = 0x31;
297 
298     /** Constant for the "2" key. */
299     public static final int VK_2              = 0x32;
300 
301     /** Constant for the "3" key. */
302     public static final int VK_3              = 0x33;
303 
304     /** Constant for the "4" key. */
305     public static final int VK_4              = 0x34;
306 
307     /** Constant for the "5" key. */
308     public static final int VK_5              = 0x35;
309 
310     /** Constant for the "6" key. */
311     public static final int VK_6              = 0x36;
312 
313     /** Constant for the "7" key. */
314     public static final int VK_7              = 0x37;
315 
316     /** Constant for the "8" key. */
317     public static final int VK_8              = 0x38;
318 
319     /** Constant for the "9" key. */
320     public static final int VK_9              = 0x39;
321 
322     /**
323      * Constant for the semicolon key, ";"
324      */
325     public static final int VK_SEMICOLON      = 0x3B;
326 
327     /**
328      * Constant for the equals key, "="
329      */
330     public static final int VK_EQUALS         = 0x3D;
331 
332     /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
333 
334     /** Constant for the "A" key. */
335     public static final int VK_A              = 0x41;
336 
337     /** Constant for the "B" key. */
338     public static final int VK_B              = 0x42;
339 
340     /** Constant for the "C" key. */
341     public static final int VK_C              = 0x43;
342 
343     /** Constant for the "D" key. */
344     public static final int VK_D              = 0x44;
345 
346     /** Constant for the "E" key. */
347     public static final int VK_E              = 0x45;
348 
349     /** Constant for the "F" key. */
350     public static final int VK_F              = 0x46;
351 
352     /** Constant for the "G" key. */
353     public static final int VK_G              = 0x47;
354 
355     /** Constant for the "H" key. */
356     public static final int VK_H              = 0x48;
357 
358     /** Constant for the "I" key. */
359     public static final int VK_I              = 0x49;
360 
361     /** Constant for the "J" key. */
362     public static final int VK_J              = 0x4A;
363 
364     /** Constant for the "K" key. */
365     public static final int VK_K              = 0x4B;
366 
367     /** Constant for the "L" key. */
368     public static final int VK_L              = 0x4C;
369 
370     /** Constant for the "M" key. */
371     public static final int VK_M              = 0x4D;
372 
373     /** Constant for the "N" key. */
374     public static final int VK_N              = 0x4E;
375 
376     /** Constant for the "O" key. */
377     public static final int VK_O              = 0x4F;
378 
379     /** Constant for the "P" key. */
380     public static final int VK_P              = 0x50;
381 
382     /** Constant for the "Q" key. */
383     public static final int VK_Q              = 0x51;
384 
385     /** Constant for the "R" key. */
386     public static final int VK_R              = 0x52;
387 
388     /** Constant for the "S" key. */
389     public static final int VK_S              = 0x53;
390 
391     /** Constant for the "T" key. */
392     public static final int VK_T              = 0x54;
393 
394     /** Constant for the "U" key. */
395     public static final int VK_U              = 0x55;
396 
397     /** Constant for the "V" key. */
398     public static final int VK_V              = 0x56;
399 
400     /** Constant for the "W" key. */
401     public static final int VK_W              = 0x57;
402 
403     /** Constant for the "X" key. */
404     public static final int VK_X              = 0x58;
405 
406     /** Constant for the "Y" key. */
407     public static final int VK_Y              = 0x59;
408 
409     /** Constant for the "Z" key. */
410     public static final int VK_Z              = 0x5A;
411 
412     /**
413      * Constant for the open bracket key, "["
414      */
415     public static final int VK_OPEN_BRACKET   = 0x5B;
416 
417     /**
418      * Constant for the back slash key, "\"
419      */
420     public static final int VK_BACK_SLASH     = 0x5C;
421 
422     /**
423      * Constant for the close bracket key, "]"
424      */
425     public static final int VK_CLOSE_BRACKET  = 0x5D;
426 
427     /** Constant for the number pad "0" key. */
428     public static final int VK_NUMPAD0        = 0x60;
429 
430     /** Constant for the number pad "1" key. */
431     public static final int VK_NUMPAD1        = 0x61;
432 
433     /** Constant for the number pad "2" key. */
434     public static final int VK_NUMPAD2        = 0x62;
435 
436     /** Constant for the number pad "3" key. */
437     public static final int VK_NUMPAD3        = 0x63;
438 
439     /** Constant for the number pad "4" key. */
440     public static final int VK_NUMPAD4        = 0x64;
441 
442     /** Constant for the number pad "5" key. */
443     public static final int VK_NUMPAD5        = 0x65;
444 
445     /** Constant for the number pad "6" key. */
446     public static final int VK_NUMPAD6        = 0x66;
447 
448     /** Constant for the number pad "7" key. */
449     public static final int VK_NUMPAD7        = 0x67;
450 
451     /** Constant for the number pad "8" key. */
452     public static final int VK_NUMPAD8        = 0x68;
453 
454     /** Constant for the number pad "9" key. */
455     public static final int VK_NUMPAD9        = 0x69;
456 
457     /** Constant for the number pad multiply key. */
458     public static final int VK_MULTIPLY       = 0x6A;
459 
460     /** Constant for the number pad add key. */
461     public static final int VK_ADD            = 0x6B;
462 
463     /**
464      * This constant is obsolete, and is included only for backwards
465      * compatibility.
466      * @see #VK_SEPARATOR
467      */
468     public static final int VK_SEPARATER      = 0x6C;
469 
470     /**
471      * Constant for the Numpad Separator key.
472      * @since 1.4
473      */
474     public static final int VK_SEPARATOR      = VK_SEPARATER;
475 
476     /** Constant for the number pad subtract key. */
477     public static final int VK_SUBTRACT       = 0x6D;
478 
479     /** Constant for the number pad decimal point key. */
480     public static final int VK_DECIMAL        = 0x6E;
481 
482     /** Constant for the number pad divide key. */
483     public static final int VK_DIVIDE         = 0x6F;
484 
485     /** Constant for the delete key. */
486     public static final int VK_DELETE         = 0x7F; /* ASCII DEL */
487 
488     /** Constant for the NUM_LOCK key. */
489     public static final int VK_NUM_LOCK       = 0x90;
490 
491     /** Constant for the SCROLL_LOCK key. */
492     public static final int VK_SCROLL_LOCK    = 0x91;
493 
494     /** Constant for the F1 function key. */
495     public static final int VK_F1             = 0x70;
496 
497     /** Constant for the F2 function key. */
498     public static final int VK_F2             = 0x71;
499 
500     /** Constant for the F3 function key. */
501     public static final int VK_F3             = 0x72;
502 
503     /** Constant for the F4 function key. */
504     public static final int VK_F4             = 0x73;
505 
506     /** Constant for the F5 function key. */
507     public static final int VK_F5             = 0x74;
508 
509     /** Constant for the F6 function key. */
510     public static final int VK_F6             = 0x75;
511 
512     /** Constant for the F7 function key. */
513     public static final int VK_F7             = 0x76;
514 
515     /** Constant for the F8 function key. */
516     public static final int VK_F8             = 0x77;
517 
518     /** Constant for the F9 function key. */
519     public static final int VK_F9             = 0x78;
520 
521     /** Constant for the F10 function key. */
522     public static final int VK_F10            = 0x79;
523 
524     /** Constant for the F11 function key. */
525     public static final int VK_F11            = 0x7A;
526 
527     /** Constant for the F12 function key. */
528     public static final int VK_F12            = 0x7B;
529 
530     /**
531      * Constant for the F13 function key.
532      * @since 1.2
533      */
534     /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
535     public static final int VK_F13            = 0xF000;
536 
537     /**
538      * Constant for the F14 function key.
539      * @since 1.2
540      */
541     public static final int VK_F14            = 0xF001;
542 
543     /**
544      * Constant for the F15 function key.
545      * @since 1.2
546      */
547     public static final int VK_F15            = 0xF002;
548 
549     /**
550      * Constant for the F16 function key.
551      * @since 1.2
552      */
553     public static final int VK_F16            = 0xF003;
554 
555     /**
556      * Constant for the F17 function key.
557      * @since 1.2
558      */
559     public static final int VK_F17            = 0xF004;
560 
561     /**
562      * Constant for the F18 function key.
563      * @since 1.2
564      */
565     public static final int VK_F18            = 0xF005;
566 
567     /**
568      * Constant for the F19 function key.
569      * @since 1.2
570      */
571     public static final int VK_F19            = 0xF006;
572 
573     /**
574      * Constant for the F20 function key.
575      * @since 1.2
576      */
577     public static final int VK_F20            = 0xF007;
578 
579     /**
580      * Constant for the F21 function key.
581      * @since 1.2
582      */
583     public static final int VK_F21            = 0xF008;
584 
585     /**
586      * Constant for the F22 function key.
587      * @since 1.2
588      */
589     public static final int VK_F22            = 0xF009;
590 
591     /**
592      * Constant for the F23 function key.
593      * @since 1.2
594      */
595     public static final int VK_F23            = 0xF00A;
596 
597     /**
598      * Constant for the F24 function key.
599      * @since 1.2
600      */
601     public static final int VK_F24            = 0xF00B;
602 
603     /**  Constant for the PRINTSCREEN key. */
604     public static final int VK_PRINTSCREEN    = 0x9A;
605 
606     /**  Constant for the INSERT key. */
607     public static final int VK_INSERT         = 0x9B;
608 
609     /**  Constant for the HELP key. */
610     public static final int VK_HELP           = 0x9C;
611 
612     /**  Constant for the META key. */
613     public static final int VK_META           = 0x9D;
614 
615     /**  Constant for the BACK_QUOTE  key. */
616     public static final int VK_BACK_QUOTE     = 0xC0;
617 
618     /**  Constant for the QUOTE key. */
619     public static final int VK_QUOTE          = 0xDE;
620 
621     /**
622      * Constant for the numeric keypad <b>up</b> arrow key.
623      * @see #VK_UP
624      * @since 1.2
625      */
626     public static final int VK_KP_UP          = 0xE0;
627 
628     /**
629      * Constant for the numeric keypad <b>down</b> arrow key.
630      * @see #VK_DOWN
631      * @since 1.2
632      */
633     public static final int VK_KP_DOWN        = 0xE1;
634 
635     /**
636      * Constant for the numeric keypad <b>left</b> arrow key.
637      * @see #VK_LEFT
638      * @since 1.2
639      */
640     public static final int VK_KP_LEFT        = 0xE2;
641 
642     /**
643      * Constant for the numeric keypad <b>right</b> arrow key.
644      * @see #VK_RIGHT
645      * @since 1.2
646      */
647     public static final int VK_KP_RIGHT       = 0xE3;
648 
649     /* For European keyboards */
650     /** @since 1.2 */
651     public static final int VK_DEAD_GRAVE               = 0x80;
652     /** @since 1.2 */
653     public static final int VK_DEAD_ACUTE               = 0x81;
654     /** @since 1.2 */
655     public static final int VK_DEAD_CIRCUMFLEX          = 0x82;
656     /** @since 1.2 */
657     public static final int VK_DEAD_TILDE               = 0x83;
658     /** @since 1.2 */
659     public static final int VK_DEAD_MACRON              = 0x84;
660     /** @since 1.2 */
661     public static final int VK_DEAD_BREVE               = 0x85;
662     /** @since 1.2 */
663     public static final int VK_DEAD_ABOVEDOT            = 0x86;
664     /** @since 1.2 */
665     public static final int VK_DEAD_DIAERESIS           = 0x87;
666     /** @since 1.2 */
667     public static final int VK_DEAD_ABOVERING           = 0x88;
668     /** @since 1.2 */
669     public static final int VK_DEAD_DOUBLEACUTE         = 0x89;
670     /** @since 1.2 */
671     public static final int VK_DEAD_CARON               = 0x8a;
672     /** @since 1.2 */
673     public static final int VK_DEAD_CEDILLA             = 0x8b;
674     /** @since 1.2 */
675     public static final int VK_DEAD_OGONEK              = 0x8c;
676     /** @since 1.2 */
677     public static final int VK_DEAD_IOTA                = 0x8d;
678     /** @since 1.2 */
679     public static final int VK_DEAD_VOICED_SOUND        = 0x8e;
680     /** @since 1.2 */
681     public static final int VK_DEAD_SEMIVOICED_SOUND    = 0x8f;
682 
683     /** @since 1.2 */
684     public static final int VK_AMPERSAND                = 0x96;
685     /** @since 1.2 */
686     public static final int VK_ASTERISK                 = 0x97;
687     /** @since 1.2 */
688     public static final int VK_QUOTEDBL                 = 0x98;
689     /** @since 1.2 */
690     public static final int VK_LESS                     = 0x99;
691 
692     /** @since 1.2 */
693     public static final int VK_GREATER                  = 0xa0;
694     /** @since 1.2 */
695     public static final int VK_BRACELEFT                = 0xa1;
696     /** @since 1.2 */
697     public static final int VK_BRACERIGHT               = 0xa2;
698 
699     /**
700      * Constant for the "@" key.
701      * @since 1.2
702      */
703     public static final int VK_AT                       = 0x0200;
704 
705     /**
706      * Constant for the ":" key.
707      * @since 1.2
708      */
709     public static final int VK_COLON                    = 0x0201;
710 
711     /**
712      * Constant for the "^" key.
713      * @since 1.2
714      */
715     public static final int VK_CIRCUMFLEX               = 0x0202;
716 
717     /**
718      * Constant for the "$" key.
719      * @since 1.2
720      */
721     public static final int VK_DOLLAR                   = 0x0203;
722 
723     /**
724      * Constant for the Euro currency sign key.
725      * @since 1.2
726      */
727     public static final int VK_EURO_SIGN                = 0x0204;
728 
729     /**
730      * Constant for the "!" key.
731      * @since 1.2
732      */
733     public static final int VK_EXCLAMATION_MARK         = 0x0205;
734 
735     /**
736      * Constant for the inverted exclamation mark key.
737      * @since 1.2
738      */
739     public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
740 
741     /**
742      * Constant for the "(" key.
743      * @since 1.2
744      */
745     public static final int VK_LEFT_PARENTHESIS         = 0x0207;
746 
747     /**
748      * Constant for the "#" key.
749      * @since 1.2
750      */
751     public static final int VK_NUMBER_SIGN              = 0x0208;
752 
753     /**
754      * Constant for the "+" key.
755      * @since 1.2
756      */
757     public static final int VK_PLUS                     = 0x0209;
758 
759     /**
760      * Constant for the ")" key.
761      * @since 1.2
762      */
763     public static final int VK_RIGHT_PARENTHESIS        = 0x020A;
764 
765     /**
766      * Constant for the "_" key.
767      * @since 1.2
768      */
769     public static final int VK_UNDERSCORE               = 0x020B;
770 
771     /**
772      * Constant for the Microsoft Windows "Windows" key.
773      * It is used for both the left and right version of the key.
774      * @see #getKeyLocation()
775      * @since 1.5
776      */
777     public static final int VK_WINDOWS                  = 0x020C;
778 
779     /**
780      * Constant for the Microsoft Windows Context Menu key.
781      * @since 1.5
782      */
783     public static final int VK_CONTEXT_MENU             = 0x020D;
784 
785     /* for input method support on Asian Keyboards */
786 
787     /* not clear what this means - listed in Microsoft Windows API */
788     /** Constant for the FINAL key. */
789     public static final int VK_FINAL                    = 0x0018;
790 
791     /** Constant for the Convert function key. */
792     /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
793     public static final int VK_CONVERT                  = 0x001C;
794 
795     /** Constant for the Don't Convert function key. */
796     /* Japanese PC 106 keyboard: muhenkan */
797     public static final int VK_NONCONVERT               = 0x001D;
798 
799     /** Constant for the Accept or Commit function key. */
800     /* Japanese Solaris keyboard: kakutei */
801     public static final int VK_ACCEPT                   = 0x001E;
802 
803     /* not clear what this means - listed in Microsoft Windows API */
804     /** Constant for the MODECHANGE key. */
805     public static final int VK_MODECHANGE               = 0x001F;
806 
807     /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
808        might still be used on other platforms */
809     /**
810      * Constant for the KANA lock key.
811      * @see #VK_KANA_LOCK
812      **/
813     public static final int VK_KANA                     = 0x0015;
814 
815     /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
816        might still be used for other platforms */
817     /**
818      * Constant for KANJI.
819      * @see #VK_INPUT_METHOD_ON_OFF
820      */
821     public static final int VK_KANJI                    = 0x0019;
822 
823     /**
824      * Constant for the Alphanumeric function key.
825      * @since 1.2
826      */
827     /* Japanese PC 106 keyboard: eisuu */
828     public static final int VK_ALPHANUMERIC             = 0x00F0;
829 
830     /**
831      * Constant for the Katakana function key.
832      * @since 1.2
833      */
834     /* Japanese PC 106 keyboard: katakana */
835     public static final int VK_KATAKANA                 = 0x00F1;
836 
837     /**
838      * Constant for the Hiragana function key.
839      * @since 1.2
840      */
841     /* Japanese PC 106 keyboard: hiragana */
842     public static final int VK_HIRAGANA                 = 0x00F2;
843 
844     /**
845      * Constant for the Full-Width Characters function key.
846      * @since 1.2
847      */
848     /* Japanese PC 106 keyboard: zenkaku */
849     public static final int VK_FULL_WIDTH               = 0x00F3;
850 
851     /**
852      * Constant for the Half-Width Characters function key.
853      * @since 1.2
854      */
855     /* Japanese PC 106 keyboard: hankaku */
856     public static final int VK_HALF_WIDTH               = 0x00F4;
857 
858     /**
859      * Constant for the Roman Characters function key.
860      * @since 1.2
861      */
862     /* Japanese PC 106 keyboard: roumaji */
863     public static final int VK_ROMAN_CHARACTERS         = 0x00F5;
864 
865     /**
866      * Constant for the All Candidates function key.
867      * @since 1.2
868      */
869     /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */
870     public static final int VK_ALL_CANDIDATES           = 0x0100;
871 
872     /**
873      * Constant for the Previous Candidate function key.
874      * @since 1.2
875      */
876     /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */
877     public static final int VK_PREVIOUS_CANDIDATE       = 0x0101;
878 
879     /**
880      * Constant for the Code Input function key.
881      * @since 1.2
882      */
883     /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */
884     public static final int VK_CODE_INPUT               = 0x0102;
885 
886     /**
887      * Constant for the Japanese-Katakana function key.
888      * This key switches to a Japanese input method and selects its Katakana input mode.
889      * @since 1.2
890      */
891     /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
892     public static final int VK_JAPANESE_KATAKANA        = 0x0103;
893 
894     /**
895      * Constant for the Japanese-Hiragana function key.
896      * This key switches to a Japanese input method and selects its Hiragana input mode.
897      * @since 1.2
898      */
899     /* Japanese Macintosh keyboard */
900     public static final int VK_JAPANESE_HIRAGANA        = 0x0104;
901 
902     /**
903      * Constant for the Japanese-Roman function key.
904      * This key switches to a Japanese input method and selects its Roman-Direct input mode.
905      * @since 1.2
906      */
907     /* Japanese Macintosh keyboard */
908     public static final int VK_JAPANESE_ROMAN           = 0x0105;
909 
910     /**
911      * Constant for the locking Kana function key.
912      * This key locks the keyboard into a Kana layout.
913      * @since 1.3
914      */
915     /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */
916     public static final int VK_KANA_LOCK                = 0x0106;
917 
918     /**
919      * Constant for the input method on/off key.
920      * @since 1.3
921      */
922     /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */
923     public static final int VK_INPUT_METHOD_ON_OFF      = 0x0107;
924 
925     /* for Sun keyboards */
926     /** @since 1.2 */
927     public static final int VK_CUT                      = 0xFFD1;
928     /** @since 1.2 */
929     public static final int VK_COPY                     = 0xFFCD;
930     /** @since 1.2 */
931     public static final int VK_PASTE                    = 0xFFCF;
932     /** @since 1.2 */
933     public static final int VK_UNDO                     = 0xFFCB;
934     /** @since 1.2 */
935     public static final int VK_AGAIN                    = 0xFFC9;
936     /** @since 1.2 */
937     public static final int VK_FIND                     = 0xFFD0;
938     /** @since 1.2 */
939     public static final int VK_PROPS                    = 0xFFCA;
940     /** @since 1.2 */
941     public static final int VK_STOP                     = 0xFFC8;
942 
943     /**
944      * Constant for the Compose function key.
945      * @since 1.2
946      */
947     public static final int VK_COMPOSE                  = 0xFF20;
948 
949     /**
950      * Constant for the AltGraph function key.
951      * @since 1.2
952      */
953     public static final int VK_ALT_GRAPH                = 0xFF7E;
954 
955     /**
956      * Constant for the Begin key.
957      * @since 1.5
958      */
959     public static final int VK_BEGIN                    = 0xFF58;
960 
961     /**
962      * This value is used to indicate that the keyCode is unknown.
963      * KEY_TYPED events do not have a keyCode value; this value
964      * is used instead.
965      */
966     public static final int VK_UNDEFINED      = 0x0;
967 
968     /**
969      * KEY_PRESSED and KEY_RELEASED events which do not map to a
970      * valid Unicode character use this for the keyChar value.
971      */
972     public static final char CHAR_UNDEFINED   = 0xFFFF;
973 
974     /**
975      * A constant indicating that the keyLocation is indeterminate
976      * or not relevant.
977      * {@code KEY_TYPED} events do not have a keyLocation; this value
978      * is used instead.
979      * @since 1.4
980      */
981     public static final int KEY_LOCATION_UNKNOWN  = 0;
982 
983     /**
984      * A constant indicating that the key pressed or released
985      * is not distinguished as the left or right version of a key,
986      * and did not originate on the numeric keypad (or did not
987      * originate with a virtual key corresponding to the numeric
988      * keypad).
989      * @since 1.4
990      */
991     public static final int KEY_LOCATION_STANDARD = 1;
992 
993     /**
994      * A constant indicating that the key pressed or released is in
995      * the left key location (there is more than one possible location
996      * for this key).  Example: the left shift key.
997      * @since 1.4
998      */
999     public static final int KEY_LOCATION_LEFT     = 2;
1000 
1001     /**
1002      * A constant indicating that the key pressed or released is in
1003      * the right key location (there is more than one possible location
1004      * for this key).  Example: the right shift key.
1005      * @since 1.4
1006      */
1007     public static final int KEY_LOCATION_RIGHT    = 3;
1008 
1009     /**
1010      * A constant indicating that the key event originated on the
1011      * numeric keypad or with a virtual key corresponding to the
1012      * numeric keypad.
1013      * @since 1.4
1014      */
1015     public static final int KEY_LOCATION_NUMPAD   = 4;
1016 
1017     /**
1018      * The unique value assigned to each of the keys on the
1019      * keyboard.  There is a common set of key codes that
1020      * can be fired by most keyboards.
1021      * The symbolic name for a key code should be used rather
1022      * than the code value itself.
1023      *
1024      * @serial
1025      * @see #getKeyCode()
1026      * @see #setKeyCode(int)
1027      */
1028     int  keyCode;
1029 
1030     /**
1031      * {@code keyChar} is a valid unicode character
1032      * that is fired by a key or a key combination on
1033      * a keyboard.
1034      *
1035      * @serial
1036      * @see #getKeyChar()
1037      * @see #setKeyChar(char)
1038      */
1039     char keyChar;
1040 
1041     /**
1042      * The location of the key on the keyboard.
1043      *
1044      * Some keys occur more than once on a keyboard, e.g. the left and
1045      * right shift keys.  Additionally, some keys occur on the numeric
1046      * keypad.  This variable is used to distinguish such keys.
1047      *
1048      * The only legal values are {@code KEY_LOCATION_UNKNOWN},
1049      * {@code KEY_LOCATION_STANDARD}, {@code KEY_LOCATION_LEFT},
1050      * {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}.
1051      *
1052      * @serial
1053      * @see #getKeyLocation()
1054      */
1055     int keyLocation;
1056 
1057     //set from native code.
1058     private transient long rawCode = 0;
1059     private transient long primaryLevelUnicode = 0;
1060     private transient long scancode = 0; // for MS Windows only
1061     private transient long extendedKeyCode = 0;
1062 
1063     /*
1064      * JDK 1.1 serialVersionUID
1065      */
1066     private static final long serialVersionUID = -2352130953028126954L;
1067 
1068     static {
1069         /* ensure that the necessary native libraries are loaded */
NativeLibLoader.loadLibraries()1070         NativeLibLoader.loadLibraries();
1071         if (!GraphicsEnvironment.isHeadless()) {
initIDs()1072             initIDs();
1073         }
1074 
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; } @Override public boolean isProxyActive(KeyEvent ev) { return ev.isProxyActive; } })1075         AWTAccessor.setKeyEventAccessor(
1076             new AWTAccessor.KeyEventAccessor() {
1077                 public void setRawCode(KeyEvent ev, long rawCode) {
1078                     ev.rawCode = rawCode;
1079                 }
1080 
1081                 public void setPrimaryLevelUnicode(KeyEvent ev,
1082                                                    long primaryLevelUnicode) {
1083                     ev.primaryLevelUnicode = primaryLevelUnicode;
1084                 }
1085 
1086                 public void setExtendedKeyCode(KeyEvent ev,
1087                                                long extendedKeyCode) {
1088                     ev.extendedKeyCode = extendedKeyCode;
1089                 }
1090 
1091                 public Component getOriginalSource( KeyEvent ev ) {
1092                     return ev.originalSource;
1093                 }
1094 
1095                 @Override
1096                 public boolean isProxyActive(KeyEvent ev) {
1097                     return ev.isProxyActive;
1098                 }
1099             });
1100     }
1101 
1102     /**
1103      * Initialize JNI field and method IDs for fields that may be
1104      * accessed from C.
1105      */
initIDs()1106     private static native void initIDs();
1107 
1108     /**
1109      * The original event source.
1110      *
1111      * Event source can be changed during processing, but in some cases
1112      * we need to be able to obtain original source.
1113      *
1114      * @since 1.8
1115      */
1116     private Component originalSource;
1117 
KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation, boolean isProxyActive)1118     private KeyEvent(Component source, int id, long when, int modifiers,
1119                     int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
1120         this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
1121         this.isProxyActive = isProxyActive;
1122     }
1123 
1124     /**
1125      * Constructs a {@code KeyEvent} object.
1126      * <p>This method throws an
1127      * {@code IllegalArgumentException} if {@code source}
1128      * is {@code null}.
1129      *
1130      * @param source    The {@code Component} that originated the event
1131      * @param id              An integer indicating the type of event.
1132      *                  For information on allowable values, see
1133      *                  the class description for {@link KeyEvent}
1134      * @param when      A long integer that specifies the time the event
1135      *                  occurred.
1136      *                     Passing negative or zero value
1137      *                     is not recommended
1138      * @param modifiers The modifier keys down during event (shift, ctrl,
1139      *                  alt, meta).
1140      *                     Passing negative value
1141      *                     is not recommended.
1142      *                     Zero value means that no modifiers were passed.
1143      *                  Use either an extended _DOWN_MASK or old _MASK modifiers,
1144      *                  however do not mix models in the one event.
1145      *                  The extended modifiers are preferred for using
1146      * @param keyCode   The integer code for an actual key, or VK_UNDEFINED
1147      *                  (for a key-typed event)
1148      * @param keyChar   The Unicode character generated by this event, or
1149      *                  CHAR_UNDEFINED (for key-pressed and key-released
1150      *                  events which do not map to a valid Unicode character)
1151      * @param keyLocation  Identifies the key location.  The only legal
1152      *        values are {@code KEY_LOCATION_UNKNOWN},
1153      *        {@code KEY_LOCATION_STANDARD}, {@code KEY_LOCATION_LEFT},
1154      *        {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}.
1155      * @throws IllegalArgumentException
1156      *     if {@code id} is {@code KEY_TYPED} and
1157      *       {@code keyChar} is {@code CHAR_UNDEFINED};
1158      *     or if {@code id} is {@code KEY_TYPED} and
1159      *       {@code keyCode} is not {@code VK_UNDEFINED};
1160      *     or if {@code id} is {@code KEY_TYPED} and
1161      *       {@code keyLocation} is not {@code KEY_LOCATION_UNKNOWN};
1162      *     or if {@code keyLocation} is not one of the legal
1163      *       values enumerated above.
1164      * @throws IllegalArgumentException if {@code source} is null
1165      * @see #getSource()
1166      * @see #getID()
1167      * @see #getWhen()
1168      * @see #getModifiers()
1169      * @see #getKeyCode()
1170      * @see #getKeyChar()
1171      * @see #getKeyLocation()
1172      * @since 1.4
1173      */
1174     @SuppressWarnings("deprecation")
KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)1175     public KeyEvent(Component source, int id, long when, int modifiers,
1176                     int keyCode, char keyChar, int keyLocation) {
1177         super(source, id, when, modifiers);
1178         if (id == KEY_TYPED) {
1179             if (keyChar == CHAR_UNDEFINED) {
1180                 throw new IllegalArgumentException("invalid keyChar");
1181             }
1182             if (keyCode != VK_UNDEFINED) {
1183                 throw new IllegalArgumentException("invalid keyCode");
1184             }
1185             if (keyLocation != KEY_LOCATION_UNKNOWN) {
1186                 throw new IllegalArgumentException("invalid keyLocation");
1187             }
1188         }
1189 
1190         this.keyCode = keyCode;
1191         this.keyChar = keyChar;
1192 
1193         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
1194             (keyLocation > KEY_LOCATION_NUMPAD)) {
1195             throw new IllegalArgumentException("invalid keyLocation");
1196         }
1197         this.keyLocation = keyLocation;
1198         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1199             setNewModifiers();
1200         } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1201             setOldModifiers();
1202         }
1203         originalSource = source;
1204     }
1205 
1206     /**
1207      * Constructs a {@code KeyEvent} object.
1208      * <p> This method throws an
1209      * {@code IllegalArgumentException} if {@code source}
1210      * is {@code null}.
1211      *
1212      * @param source    The {@code Component} that originated the event
1213      * @param id              An integer indicating the type of event.
1214      *                  For information on allowable values, see
1215      *                  the class description for {@link KeyEvent}
1216      * @param when      A long integer that specifies the time the event
1217      *                  occurred.
1218      *                     Passing negative or zero value
1219      *                     is not recommended
1220      * @param modifiers The modifier keys down during event (shift, ctrl,
1221      *                  alt, meta).
1222      *                     Passing negative value
1223      *                     is not recommended.
1224      *                     Zero value means that no modifiers were passed.
1225      *                  Use either an extended _DOWN_MASK or old _MASK modifiers,
1226      *                  however do not mix models in the one event.
1227      *                  The extended modifiers are preferred for using
1228      * @param keyCode   The integer code for an actual key, or VK_UNDEFINED
1229      *                  (for a key-typed event)
1230      * @param keyChar   The Unicode character generated by this event, or
1231      *                  CHAR_UNDEFINED (for key-pressed and key-released
1232      *                  events which do not map to a valid Unicode character)
1233      * @throws IllegalArgumentException  if {@code id} is
1234      *     {@code KEY_TYPED} and {@code keyChar} is
1235      *     {@code CHAR_UNDEFINED}; or if {@code id} is
1236      *     {@code KEY_TYPED} and {@code keyCode} is not
1237      *     {@code VK_UNDEFINED}
1238      * @throws IllegalArgumentException if {@code source} is null
1239      * @see #getSource()
1240      * @see #getID()
1241      * @see #getWhen()
1242      * @see #getModifiers()
1243      * @see #getKeyCode()
1244      * @see #getKeyChar()
1245      */
KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)1246     public KeyEvent(Component source, int id, long when, int modifiers,
1247                     int keyCode, char keyChar) {
1248         this(source, id, when, modifiers, keyCode, keyChar,
1249           KEY_LOCATION_UNKNOWN);
1250     }
1251 
1252     /**
1253      * @deprecated as of JDK1.1; use {@link #KeyEvent(Component, int, long, int, int, char)} instead
1254      * @param source    The {@code Component} that originated the event
1255      * @param id              An integer indicating the type of event.
1256      *                  For information on allowable values, see
1257      *                  the class description for {@link KeyEvent}
1258      * @param when      A long integer that specifies the time the event
1259      *                  occurred.
1260      *                     Passing negative or zero value
1261      *                     is not recommended
1262      * @param modifiers The modifier keys down during event (shift, ctrl,
1263      *                  alt, meta).
1264      *                     Passing negative value
1265      *                     is not recommended.
1266      *                     Zero value means that no modifiers were passed.
1267      *                  Use either an extended _DOWN_MASK or old _MASK modifiers,
1268      *                  however do not mix models in the one event.
1269      *                  The extended modifiers are preferred for using
1270      * @param keyCode   The integer code for an actual key, or VK_UNDEFINED
1271      *                  (for a key-typed event)
1272      */
1273     @Deprecated
KeyEvent(Component source, int id, long when, int modifiers, int keyCode)1274     public KeyEvent(Component source, int id, long when, int modifiers,
1275                     int keyCode) {
1276         this(source, id, when, modifiers, keyCode, (char)keyCode);
1277     }
1278 
1279     /**
1280      * Returns the integer keyCode associated with the key in this event.
1281      *
1282      * @return the integer code for an actual key on the keyboard.
1283      *         (For {@code KEY_TYPED} events, the keyCode is
1284      *         {@code VK_UNDEFINED}.)
1285      */
getKeyCode()1286     public int getKeyCode() {
1287         return keyCode;
1288     }
1289 
1290     /**
1291      * Set the keyCode value to indicate a physical key.
1292      *
1293      * @param keyCode an integer corresponding to an actual key on the keyboard.
1294      */
setKeyCode(int keyCode)1295     public void setKeyCode(int keyCode) {
1296         this.keyCode = keyCode;
1297     }
1298 
1299     /**
1300      * Returns the character associated with the key in this event.
1301      * For example, the {@code KEY_TYPED} event for shift + "a"
1302      * returns the value for "A".
1303      * <p>
1304      * {@code KEY_PRESSED} and {@code KEY_RELEASED} events
1305      * are not intended for reporting of character input.  Therefore,
1306      * the values returned by this method are guaranteed to be
1307      * meaningful only for {@code KEY_TYPED} events.
1308      *
1309      * @return the Unicode character defined for this key event.
1310      *         If no valid Unicode character exists for this key event,
1311      *         {@code CHAR_UNDEFINED} is returned.
1312      */
getKeyChar()1313     public char getKeyChar() {
1314         return keyChar;
1315     }
1316 
1317     /**
1318      * Set the keyChar value to indicate a logical character.
1319      *
1320      * @param keyChar a char corresponding to the combination of keystrokes
1321      *                that make up this event.
1322      */
setKeyChar(char keyChar)1323     public void setKeyChar(char keyChar) {
1324         this.keyChar = keyChar;
1325     }
1326 
1327     /**
1328      * Set the modifiers to indicate additional keys that were held down
1329      * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
1330      * <p>
1331      * NOTE:  use of this method is not recommended, because many AWT
1332      * implementations do not recognize modifier changes.  This is
1333      * especially true for {@code KEY_TYPED} events where the shift
1334      * modifier is changed.
1335      *
1336      * @param modifiers an integer combination of the modifier constants.
1337      * @see InputEvent
1338      * @deprecated as of JDK1.1.4
1339      */
1340     @Deprecated
setModifiers(int modifiers)1341     public void setModifiers(int modifiers) {
1342         this.modifiers = modifiers;
1343         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1344             setNewModifiers();
1345         } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1346             setOldModifiers();
1347         }
1348     }
1349 
1350     /**
1351      * Returns the location of the key that originated this key event.
1352      *
1353      * Some keys occur more than once on a keyboard, e.g. the left and
1354      * right shift keys.  Additionally, some keys occur on the numeric
1355      * keypad.  This provides a way of distinguishing such keys.
1356      *
1357      * @return the location of the key that was pressed or released.
1358      *         Always returns {@code KEY_LOCATION_UNKNOWN} for
1359      *         {@code KEY_TYPED} events.
1360      * @since 1.4
1361      */
getKeyLocation()1362     public int getKeyLocation() {
1363         return keyLocation;
1364     }
1365 
1366     /**
1367      * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
1368      * These strings can be localized by changing the awt.properties file.
1369      *
1370      * @param keyCode the key whose description is to be returned
1371      * @return a string containing a text description for a physical key,
1372      *         identified by its keyCode
1373      */
getKeyText(int keyCode)1374     public static String getKeyText(int keyCode) {
1375         if (keyCode >= VK_0 && keyCode <= VK_9 ||
1376             keyCode >= VK_A && keyCode <= VK_Z) {
1377             return String.valueOf((char)keyCode);
1378         }
1379 
1380         switch(keyCode) {
1381           case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
1382           case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
1383           case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
1384           case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
1385           case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
1386           case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
1387           case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
1388           case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
1389           case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
1390           case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
1391           case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
1392           case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
1393           case VK_END: return Toolkit.getProperty("AWT.end", "End");
1394           case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
1395           case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1396           case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
1397           case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1398           case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1399           case VK_BEGIN: return Toolkit.getProperty("AWT.begin", "Begin");
1400 
1401           // modifiers
1402           case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
1403           case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
1404           case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
1405           case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
1406           case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
1407 
1408           // punctuation
1409           case VK_COMMA: return Toolkit.getProperty("AWT.comma", "Comma");
1410           case VK_PERIOD: return Toolkit.getProperty("AWT.period", "Period");
1411           case VK_SLASH: return Toolkit.getProperty("AWT.slash", "Slash");
1412           case VK_SEMICOLON: return Toolkit.getProperty("AWT.semicolon", "Semicolon");
1413           case VK_EQUALS: return Toolkit.getProperty("AWT.equals", "Equals");
1414           case VK_OPEN_BRACKET: return Toolkit.getProperty("AWT.openBracket", "Open Bracket");
1415           case VK_BACK_SLASH: return Toolkit.getProperty("AWT.backSlash", "Back Slash");
1416           case VK_CLOSE_BRACKET: return Toolkit.getProperty("AWT.closeBracket", "Close Bracket");
1417 
1418           // numpad numeric keys handled below
1419           case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
1420           case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
1421           case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,");
1422           case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
1423           case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
1424           case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
1425           case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
1426           case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
1427           case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
1428 
1429           case VK_WINDOWS: return Toolkit.getProperty("AWT.windows", "Windows");
1430           case VK_CONTEXT_MENU: return Toolkit.getProperty("AWT.context", "Context Menu");
1431 
1432           case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
1433           case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
1434           case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
1435           case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
1436           case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
1437           case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
1438           case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
1439           case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
1440           case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
1441           case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
1442           case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
1443           case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
1444           case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");
1445           case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");
1446           case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");
1447           case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");
1448           case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");
1449           case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");
1450           case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");
1451           case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");
1452           case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");
1453           case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");
1454           case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");
1455           case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");
1456 
1457           case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
1458           case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
1459           case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
1460           case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
1461           case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
1462 
1463           case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");
1464           case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1465           case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1466           case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1467 
1468           case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
1469           case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
1470           case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex");
1471           case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
1472           case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
1473           case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
1474           case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot");
1475           case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis");
1476           case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring");
1477           case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute");
1478           case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
1479           case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla");
1480           case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
1481           case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
1482           case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound");
1483           case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound");
1484 
1485           case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand");
1486           case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk");
1487           case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
1488           case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less");
1489           case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater");
1490           case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
1491           case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace");
1492           case VK_AT: return Toolkit.getProperty("AWT.at", "At");
1493           case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon");
1494           case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex");
1495           case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar");
1496           case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro");
1497           case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark");
1498           case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark");
1499           case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis");
1500           case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign");
1501           case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus");
1502           case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus");
1503           case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis");
1504           case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore");
1505 
1506           case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
1507           case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
1508           case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
1509           case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
1510           case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
1511           case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
1512           case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
1513           case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric");
1514           case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana");
1515           case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana");
1516           case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
1517           case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
1518           case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters");
1519           case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates");
1520           case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate");
1521           case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input");
1522           case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana");
1523           case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana");
1524           case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman");
1525           case VK_KANA_LOCK: return Toolkit.getProperty("AWT.kanaLock", "Kana Lock");
1526           case VK_INPUT_METHOD_ON_OFF: return Toolkit.getProperty("AWT.inputMethodOnOff", "Input Method On/Off");
1527 
1528           case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again");
1529           case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo");
1530           case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy");
1531           case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste");
1532           case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut");
1533           case VK_FIND: return Toolkit.getProperty("AWT.find", "Find");
1534           case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props");
1535           case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop");
1536         }
1537 
1538         if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
1539             String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
1540             char c = (char)(keyCode - VK_NUMPAD0 + '0');
1541             return numpad + "-" + c;
1542         }
1543 
1544         if ((keyCode & 0x01000000) != 0) {
1545             return String.valueOf((char)(keyCode ^ 0x01000000 ));
1546         }
1547         String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1548         return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1549     }
1550 
1551     /**
1552      * Returns a {@code String} describing the modifier key(s),
1553      * such as "Shift", or "Ctrl+Shift".  These strings can be
1554      * localized by changing the {@code awt.properties} file.
1555      * <p>
1556      * Note that {@code InputEvent.ALT_MASK} and
1557      * {@code InputEvent.BUTTON2_MASK} have the same value,
1558      * so the string "Alt" is returned for both modifiers.  Likewise,
1559      * {@code InputEvent.META_MASK} and
1560      * {@code InputEvent.BUTTON3_MASK} have the same value,
1561      * so the string "Meta" is returned for both modifiers.
1562      *
1563      * @param modifiers the modifier mask to be processed
1564      * @return string a text description of the combination of modifier
1565      *                keys that were held down during the event
1566      * @see InputEvent#getModifiersExText(int)
1567      * @deprecated It is recommended that extended modifier keys and
1568      *             {@link InputEvent#getModifiersExText(int)} be used instead
1569      */
1570     @Deprecated(since = "9")
getKeyModifiersText(int modifiers)1571     public static String getKeyModifiersText(int modifiers) {
1572         StringBuilder buf = new StringBuilder();
1573         if ((modifiers & InputEvent.META_MASK) != 0) {
1574             buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1575             buf.append("+");
1576         }
1577         if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1578             buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1579             buf.append("+");
1580         }
1581         if ((modifiers & InputEvent.ALT_MASK) != 0) {
1582             buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1583             buf.append("+");
1584         }
1585         if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1586             buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1587             buf.append("+");
1588         }
1589         if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1590             buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
1591             buf.append("+");
1592         }
1593         if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1594             buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
1595             buf.append("+");
1596         }
1597         if (buf.length() > 0) {
1598             buf.setLength(buf.length()-1); // remove trailing '+'
1599         }
1600         return buf.toString();
1601     }
1602 
1603 
1604     /**
1605      * Returns whether the key in this event is an "action" key.
1606      * Typically an action key does not fire a unicode character and is
1607      * not a modifier key.
1608      *
1609      * @return {@code true} if the key is an "action" key,
1610      *         {@code false} otherwise
1611      */
isActionKey()1612     public boolean isActionKey() {
1613         switch (keyCode) {
1614           case VK_HOME:
1615           case VK_END:
1616           case VK_PAGE_UP:
1617           case VK_PAGE_DOWN:
1618           case VK_UP:
1619           case VK_DOWN:
1620           case VK_LEFT:
1621           case VK_RIGHT:
1622           case VK_BEGIN:
1623 
1624           case VK_KP_LEFT:
1625           case VK_KP_UP:
1626           case VK_KP_RIGHT:
1627           case VK_KP_DOWN:
1628 
1629           case VK_F1:
1630           case VK_F2:
1631           case VK_F3:
1632           case VK_F4:
1633           case VK_F5:
1634           case VK_F6:
1635           case VK_F7:
1636           case VK_F8:
1637           case VK_F9:
1638           case VK_F10:
1639           case VK_F11:
1640           case VK_F12:
1641           case VK_F13:
1642           case VK_F14:
1643           case VK_F15:
1644           case VK_F16:
1645           case VK_F17:
1646           case VK_F18:
1647           case VK_F19:
1648           case VK_F20:
1649           case VK_F21:
1650           case VK_F22:
1651           case VK_F23:
1652           case VK_F24:
1653           case VK_PRINTSCREEN:
1654           case VK_SCROLL_LOCK:
1655           case VK_CAPS_LOCK:
1656           case VK_NUM_LOCK:
1657           case VK_PAUSE:
1658           case VK_INSERT:
1659 
1660           case VK_FINAL:
1661           case VK_CONVERT:
1662           case VK_NONCONVERT:
1663           case VK_ACCEPT:
1664           case VK_MODECHANGE:
1665           case VK_KANA:
1666           case VK_KANJI:
1667           case VK_ALPHANUMERIC:
1668           case VK_KATAKANA:
1669           case VK_HIRAGANA:
1670           case VK_FULL_WIDTH:
1671           case VK_HALF_WIDTH:
1672           case VK_ROMAN_CHARACTERS:
1673           case VK_ALL_CANDIDATES:
1674           case VK_PREVIOUS_CANDIDATE:
1675           case VK_CODE_INPUT:
1676           case VK_JAPANESE_KATAKANA:
1677           case VK_JAPANESE_HIRAGANA:
1678           case VK_JAPANESE_ROMAN:
1679           case VK_KANA_LOCK:
1680           case VK_INPUT_METHOD_ON_OFF:
1681 
1682           case VK_AGAIN:
1683           case VK_UNDO:
1684           case VK_COPY:
1685           case VK_PASTE:
1686           case VK_CUT:
1687           case VK_FIND:
1688           case VK_PROPS:
1689           case VK_STOP:
1690 
1691           case VK_HELP:
1692           case VK_WINDOWS:
1693           case VK_CONTEXT_MENU:
1694               return true;
1695         }
1696         return false;
1697     }
1698 
1699     /**
1700      * Returns a parameter string identifying this event.
1701      * This method is useful for event logging and for debugging.
1702      *
1703      * @return a string identifying the event and its attributes
1704      */
1705     @SuppressWarnings("deprecation")
paramString()1706     public String paramString() {
1707         StringBuilder str = new StringBuilder(100);
1708 
1709         switch (id) {
1710           case KEY_PRESSED:
1711             str.append("KEY_PRESSED");
1712             break;
1713           case KEY_RELEASED:
1714             str.append("KEY_RELEASED");
1715             break;
1716           case KEY_TYPED:
1717             str.append("KEY_TYPED");
1718             break;
1719           default:
1720             str.append("unknown type");
1721             break;
1722         }
1723 
1724         str.append(",keyCode=").append(keyCode);
1725         str.append(",keyText=").append(getKeyText(keyCode));
1726 
1727         /* Some keychars don't print well, e.g. escape, backspace,
1728          * tab, return, delete, cancel.  Get keyText for the keyCode
1729          * instead of the keyChar.
1730          */
1731         str.append(",keyChar=");
1732         switch (keyChar) {
1733           case '\b':
1734             str.append(getKeyText(VK_BACK_SPACE));
1735             break;
1736           case '\t':
1737             str.append(getKeyText(VK_TAB));
1738             break;
1739           case '\n':
1740             str.append(getKeyText(VK_ENTER));
1741             break;
1742           case '\u0018':
1743             str.append(getKeyText(VK_CANCEL));
1744             break;
1745           case '\u001b':
1746             str.append(getKeyText(VK_ESCAPE));
1747             break;
1748           case '\u007f':
1749             str.append(getKeyText(VK_DELETE));
1750             break;
1751           case CHAR_UNDEFINED:
1752             str.append(Toolkit.getProperty("AWT.undefined", "Undefined"));
1753             str.append(" keyChar");
1754             break;
1755           default:
1756             str.append("'").append(keyChar).append("'");
1757             break;
1758         }
1759 
1760         if (getModifiers() != 0) {
1761             str.append(",modifiers=").append(getKeyModifiersText(modifiers));
1762         }
1763         if (getModifiersEx() != 0) {
1764             str.append(",extModifiers=").append(getModifiersExText(modifiers));
1765         }
1766 
1767         str.append(",keyLocation=");
1768         switch (keyLocation) {
1769           case KEY_LOCATION_UNKNOWN:
1770             str.append("KEY_LOCATION_UNKNOWN");
1771             break;
1772           case KEY_LOCATION_STANDARD:
1773             str.append("KEY_LOCATION_STANDARD");
1774             break;
1775           case KEY_LOCATION_LEFT:
1776             str.append("KEY_LOCATION_LEFT");
1777             break;
1778           case KEY_LOCATION_RIGHT:
1779             str.append("KEY_LOCATION_RIGHT");
1780             break;
1781           case KEY_LOCATION_NUMPAD:
1782             str.append("KEY_LOCATION_NUMPAD");
1783             break;
1784           default:
1785             str.append("KEY_LOCATION_UNKNOWN");
1786             break;
1787         }
1788         str.append(",rawCode=").append(rawCode);
1789         str.append(",primaryLevelUnicode=").append(primaryLevelUnicode);
1790         str.append(",scancode=").append(scancode);
1791         str.append(",extendedKeyCode=0x").append(Long.toHexString(extendedKeyCode));
1792 
1793         return str.toString();
1794     }
1795     /**
1796      * Returns an extended key code for the event.
1797      * The extended key code is a unique id assigned to  a key on the keyboard
1798      * just like {@code keyCode}. However, unlike {@code keyCode}, this value depends on the
1799      * current keyboard layout. For instance, pressing the left topmost letter key
1800      * in a common English layout produces the same value as {@code keyCode}, {@code VK_Q}.
1801      * Pressing the same key in a regular Russian layout gives another code, unique for the
1802      * letter "Cyrillic I short".
1803      *
1804      * @return an extended key code for the event
1805      * @since 1.7
1806      */
getExtendedKeyCode()1807     public  int getExtendedKeyCode() {
1808         return (int)extendedKeyCode;
1809     }
1810     /**
1811      * Returns an extended key code for a unicode character.
1812      *
1813      * @param c the unicode character to be processed
1814      * @return for a unicode character with a corresponding {@code VK_} constant -- this
1815      *   {@code VK_} constant; for a character appearing on the primary
1816      *   level of a known keyboard layout -- a unique integer.
1817      *   If a character does not appear on the primary level of a known keyboard,
1818      *   {@code VK_UNDEFINED} is returned.
1819      *
1820      * @since 1.7
1821      */
getExtendedKeyCodeForChar(int c)1822     public static int getExtendedKeyCodeForChar(int c) {
1823         // Return a keycode (if any) associated with a character.
1824         return sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(c);
1825     }
1826 
1827     /**
1828      * Sets new modifiers by the old ones. The key modifiers
1829      * override overlapping mouse modifiers.
1830      */
1831     @SuppressWarnings("deprecation")
setNewModifiers()1832     private void setNewModifiers() {
1833         if ((modifiers & SHIFT_MASK) != 0) {
1834             modifiers |= SHIFT_DOWN_MASK;
1835         }
1836         if ((modifiers & ALT_MASK) != 0) {
1837             modifiers |= ALT_DOWN_MASK;
1838         }
1839         if ((modifiers & CTRL_MASK) != 0) {
1840             modifiers |= CTRL_DOWN_MASK;
1841         }
1842         if ((modifiers & META_MASK) != 0) {
1843             modifiers |= META_DOWN_MASK;
1844         }
1845         if ((modifiers & ALT_GRAPH_MASK) != 0) {
1846             modifiers |= ALT_GRAPH_DOWN_MASK;
1847         }
1848         if ((modifiers & BUTTON1_MASK) != 0) {
1849             modifiers |= BUTTON1_DOWN_MASK;
1850         }
1851     }
1852 
1853     /**
1854      * Sets old modifiers by the new ones.
1855      */
1856     @SuppressWarnings("deprecation")
setOldModifiers()1857     private void setOldModifiers() {
1858         if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1859             modifiers |= SHIFT_MASK;
1860         }
1861         if ((modifiers & ALT_DOWN_MASK) != 0) {
1862             modifiers |= ALT_MASK;
1863         }
1864         if ((modifiers & CTRL_DOWN_MASK) != 0) {
1865             modifiers |= CTRL_MASK;
1866         }
1867         if ((modifiers & META_DOWN_MASK) != 0) {
1868             modifiers |= META_MASK;
1869         }
1870         if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1871             modifiers |= ALT_GRAPH_MASK;
1872         }
1873         if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1874             modifiers |= BUTTON1_MASK;
1875         }
1876     }
1877 
1878     /**
1879      * Sets new modifiers by the old ones. The key modifiers
1880      * override overlapping mouse modifiers.
1881      * @serial
1882      */
1883     @SuppressWarnings("deprecation")
readObject(ObjectInputStream s)1884     private void readObject(ObjectInputStream s)
1885       throws IOException, ClassNotFoundException {
1886         s.defaultReadObject();
1887         if (getModifiers() != 0 && getModifiersEx() == 0) {
1888             setNewModifiers();
1889         }
1890     }
1891 }
1892