KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > event > KeyEvent


1 /*
2  * @(#)KeyEvent.java 1.76 07/11/22
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.event;
9
10 import java.awt.Event JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.GraphicsEnvironment JavaDoc;
13 import java.awt.Toolkit JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.ObjectInputStream JavaDoc;
16
17 /**
18  * An event which indicates that a keystroke occurred in a component.
19  * <p>
20  * This low-level event is generated by a component object (such as a text
21  * field) when a key is pressed, released, or typed.
22  * The event is passed to every <code>KeyListener</code>
23  * or <code>KeyAdapter</code> object which registered to receive such
24  * events using the component's <code>addKeyListener</code> method.
25  * (<code>KeyAdapter</code> objects implement the
26  * <code>KeyListener</code> interface.) Each such listener object
27  * gets this <code>KeyEvent</code> when the event occurs.
28  * <p>
29  * <em>"Key typed" events</em> are higher-level and generally do not depend on
30  * the platform or keyboard layout. They are generated when a Unicode character
31  * is entered, and are the preferred way to find out about character input.
32  * In the simplest case, a key typed event is produced by a single key press
33  * (e.g., 'a'). Often, however, characters are produced by series of key
34  * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to
35  * key typed events may be many-to-one or many-to-many. Key releases are not
36  * usually necessary to generate a key typed event, but there are some cases
37  * where the key typed event is not generated until a key is released (e.g.,
38  * entering ASCII sequences via the Alt-Numpad method in Windows).
39  * No key typed events are generated for keys that don't generate Unicode
40  * characters (e.g., action keys, modifier keys, etc.).
41  * <p>
42  * The getKeyChar method always returns a valid Unicode character or
43  * CHAR_UNDEFINED. Character input is reported by KEY_TYPED events:
44  * KEY_PRESSED and KEY_RELEASED events are not necessarily associated
45  * with character input. Therefore, the result of the getKeyChar method
46  * is guaranteed to be meaningful only for KEY_TYPED events.
47  * <p>
48  * For key pressed and key released events, the getKeyCode method returns
49  * the event's keyCode. For key typed events, the getKeyCode method
50  * always returns VK_UNDEFINED.
51  *
52  * <p>
53  * <em>"Key pressed" and "key released" events</em> are lower-level and depend
54  * on the platform and keyboard layout. They are generated whenever a key is
55  * pressed or released, and are the only way to find out about keys that don't
56  * generate character input (e.g., action keys, modifier keys, etc.). The key
57  * being pressed or released is indicated by the getKeyCode method, which returns
58  * a virtual key code.
59  *
60  * <p>
61  * <em>Virtual key codes</em> are used to report which keyboard key has
62  * been pressed, rather than a character generated by the combination
63  * of one or more keystrokes (such as "A", which comes from shift and "a").
64  *
65  * <p>
66  * For example, pressing the Shift key will cause a KEY_PRESSED event
67  * with a VK_SHIFT keyCode, while pressing the 'a' key will result in
68  * a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event
69  * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
70  * value of 'A' is generated.
71  *
72  * <p>
73  * Notes:
74  * <ul>
75  * <li>Key combinations which do not result in Unicode characters, such as action
76  * keys like F1 and the HELP key, do not generate KEY_TYPED events.
77  * <li>Not all keyboards or systems are capable of generating all
78  * virtual key codes. No attempt is made in Java to generate these keys
79  * artificially.
80  * <li>Virtual key codes do not identify a physical key: they depend on the
81  * platform and keyboard layout. For example, the key that generates VK_Q
82  * when using a U.S. keyboard layout will generate VK_A when using a French
83  * keyboard layout.
84  * <li>Not all characters have a keycode associated with them. For example,
85  * there is no keycode for the question mark because there is no keyboard
86  * for which it appears on the primary layer.
87  * <li>In order to support the platform-independent handling of action keys,
88  * the Java platform uses a few additional virtual key constants for functions
89  * that would otherwise have to be recognized by interpreting virtual key codes
90  * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
91  * is returned instead of VK_CONVERT with the ALT modifier.
92  * </ul>
93  *
94  * <p>
95  * WARNING: Aside from those keys that are defined by the Java language
96  * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
97  * constants. Sun reserves the right to change these values as needed
98  * to accomodate a wider range of keyboards in the future.
99  *
100  * @author Carl Quinn
101  * @author Amy Fowler
102  * @author Norbert Lindenberg
103  * @version 1.76 11/22/07
104  *
105  * @see KeyAdapter
106  * @see KeyListener
107  * @see <a HREF="http://java.sun.com/docs/books/tutorial/post1.0/ui/keylistener.html">Tutorial: Writing a Key Listener</a>
108  * @see <a HREF="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
109  *
110  * @since 1.1
111  */

112 public class KeyEvent extends InputEvent JavaDoc {
113
114     /**
115      * Stores the state of native event dispatching system
116      * - true, if when the event was created event proxying
117      * mechanism was active
118      * - false, if it was inactive
119      * Used in Component.dispatchEventImpl to correctly dispatch
120      * events when proxy is active
121      */

122     private boolean isProxyActive = false;
123
124     /**
125      * The first number in the range of ids used for key events.
126      */

127     public static final int KEY_FIRST = 400;
128
129     /**
130      * The last number in the range of ids used for key events.
131      */

132     public static final int KEY_LAST = 402;
133
134     /**
135      * The "key typed" event. This event is generated when a character is
136      * entered. In the simplest case, it is produced by a single key press.
137      * Often, however, characters are produced by series of key presses, and
138      * the mapping from key pressed events to key typed events may be
139      * many-to-one or many-to-many.
140      */

141     public static final int KEY_TYPED = KEY_FIRST;
142
143     /**
144      * The "key pressed" event. This event is generated when a key
145      * is pushed down.
146      */

147     public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
148

149     /**
150      * The "key released" event. This event is generated when a key
151      * is let up.
152      */

153     public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
154

155     /* Virtual key codes. */
156
157     public static final int VK_ENTER = '\n';
158     public static final int VK_BACK_SPACE = '\b';
159     public static final int VK_TAB = '\t';
160     public static final int VK_CANCEL = 0x03;
161     public static final int VK_CLEAR = 0x0C;
162     public static final int VK_SHIFT = 0x10;
163     public static final int VK_CONTROL = 0x11;
164     public static final int VK_ALT = 0x12;
165     public static final int VK_PAUSE = 0x13;
166     public static final int VK_CAPS_LOCK = 0x14;
167     public static final int VK_ESCAPE = 0x1B;
168     public static final int VK_SPACE = 0x20;
169     public static final int VK_PAGE_UP = 0x21;
170     public static final int VK_PAGE_DOWN = 0x22;
171     public static final int VK_END = 0x23;
172     public static final int VK_HOME = 0x24;
173
174     /**
175      * Constant for the non-numpad <b>left</b> arrow key.
176      * @see #VK_KP_LEFT
177      */

178     public static final int VK_LEFT = 0x25;
179
180     /**
181      * Constant for the non-numpad <b>up</b> arrow key.
182      * @see #VK_KP_UP
183      */

184     public static final int VK_UP = 0x26;
185
186     /**
187      * Constant for the non-numpad <b>right</b> arrow key.
188      * @see #VK_KP_RIGHT
189      */

190     public static final int VK_RIGHT = 0x27;
191
192     /**
193      * Constant for the non-numpad <b>down</b> arrow key.
194      * @see #VK_KP_DOWN
195      */

196     public static final int VK_DOWN = 0x28;
197
198     /**
199      * Constant for the comma key, ","
200      */

201     public static final int VK_COMMA = 0x2C;
202
203     /**
204      * Constant for the minus key, "-"
205      * @since 1.2
206      */

207     public static final int VK_MINUS = 0x2D;
208
209     /**
210      * Constant for the period key, "."
211      */

212     public static final int VK_PERIOD = 0x2E;
213
214     /**
215      * Constant for the forward slash key, "/"
216      */

217     public static final int VK_SLASH = 0x2F;
218
219     /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
220     public static final int VK_0 = 0x30;
221     public static final int VK_1 = 0x31;
222     public static final int VK_2 = 0x32;
223     public static final int VK_3 = 0x33;
224     public static final int VK_4 = 0x34;
225     public static final int VK_5 = 0x35;
226     public static final int VK_6 = 0x36;
227     public static final int VK_7 = 0x37;
228     public static final int VK_8 = 0x38;
229     public static final int VK_9 = 0x39;
230
231     /**
232      * Constant for the semicolon key, ";"
233      */

234     public static final int VK_SEMICOLON = 0x3B;
235
236     /**
237      * Constant for the equals key, "="
238      */

239     public static final int VK_EQUALS = 0x3D;
240
241     /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
242     public static final int VK_A = 0x41;
243     public static final int VK_B = 0x42;
244     public static final int VK_C = 0x43;
245     public static final int VK_D = 0x44;
246     public static final int VK_E = 0x45;
247     public static final int VK_F = 0x46;
248     public static final int VK_G = 0x47;
249     public static final int VK_H = 0x48;
250     public static final int VK_I = 0x49;
251     public static final int VK_J = 0x4A;
252     public static final int VK_K = 0x4B;
253     public static final int VK_L = 0x4C;
254     public static final int VK_M = 0x4D;
255     public static final int VK_N = 0x4E;
256     public static final int VK_O = 0x4F;
257     public static final int VK_P = 0x50;
258     public static final int VK_Q = 0x51;
259     public static final int VK_R = 0x52;
260     public static final int VK_S = 0x53;
261     public static final int VK_T = 0x54;
262     public static final int VK_U = 0x55;
263     public static final int VK_V = 0x56;
264     public static final int VK_W = 0x57;
265     public static final int VK_X = 0x58;
266     public static final int VK_Y = 0x59;
267     public static final int VK_Z = 0x5A;
268
269     /**
270      * Constant for the open bracket key, "["
271      */

272     public static final int VK_OPEN_BRACKET = 0x5B;
273
274     /**
275      * Constant for the back slash key, "\"
276      */

277     public static final int VK_BACK_SLASH = 0x5C;
278
279     /**
280      * Constant for the close bracket key, "]"
281      */

282     public static final int VK_CLOSE_BRACKET = 0x5D;
283
284     public static final int VK_NUMPAD0 = 0x60;
285     public static final int VK_NUMPAD1 = 0x61;
286     public static final int VK_NUMPAD2 = 0x62;
287     public static final int VK_NUMPAD3 = 0x63;
288     public static final int VK_NUMPAD4 = 0x64;
289     public static final int VK_NUMPAD5 = 0x65;
290     public static final int VK_NUMPAD6 = 0x66;
291     public static final int VK_NUMPAD7 = 0x67;
292     public static final int VK_NUMPAD8 = 0x68;
293     public static final int VK_NUMPAD9 = 0x69;
294     public static final int VK_MULTIPLY = 0x6A;
295     public static final int VK_ADD = 0x6B;
296
297     /**
298      * This constant is obsolete, and is included only for backwards
299      * compatibility.
300      * @see #VK_SEPARATOR
301      */

302     public static final int VK_SEPARATER = 0x6C;
303
304     /**
305      * Constant for the Numpad Separator key.
306      * @since 1.4
307      */

308     public static final int VK_SEPARATOR = VK_SEPARATER;
309
310     public static final int VK_SUBTRACT = 0x6D;
311     public static final int VK_DECIMAL = 0x6E;
312     public static final int VK_DIVIDE = 0x6F;
313     public static final int VK_DELETE = 0x7F; /* ASCII DEL */
314     public static final int VK_NUM_LOCK = 0x90;
315     public static final int VK_SCROLL_LOCK = 0x91;
316
317     /** Constant for the F1 function key. */
318     public static final int VK_F1 = 0x70;
319
320     /** Constant for the F2 function key. */
321     public static final int VK_F2 = 0x71;
322
323     /** Constant for the F3 function key. */
324     public static final int VK_F3 = 0x72;
325
326     /** Constant for the F4 function key. */
327     public static final int VK_F4 = 0x73;
328
329     /** Constant for the F5 function key. */
330     public static final int VK_F5 = 0x74;
331
332     /** Constant for the F6 function key. */
333     public static final int VK_F6 = 0x75;
334
335     /** Constant for the F7 function key. */
336     public static final int VK_F7 = 0x76;
337
338     /** Constant for the F8 function key. */
339     public static final int VK_F8 = 0x77;
340
341     /** Constant for the F9 function key. */
342     public static final int VK_F9 = 0x78;
343
344     /** Constant for the F10 function key. */
345     public static final int VK_F10 = 0x79;
346
347     /** Constant for the F11 function key. */
348     public static final int VK_F11 = 0x7A;
349
350     /** Constant for the F12 function key. */
351     public static final int VK_F12 = 0x7B;
352
353     /**
354      * Constant for the F13 function key.
355      * @since 1.2
356      */

357     /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
358     public static final int VK_F13 = 0xF000;
359  
360     /**
361      * Constant for the F14 function key.
362      * @since 1.2
363      */

364     public static final int VK_F14 = 0xF001;
365  
366     /**
367      * Constant for the F15 function key.
368      * @since 1.2
369      */

370     public static final int VK_F15 = 0xF002;
371  
372     /**
373      * Constant for the F16 function key.
374      * @since 1.2
375      */

376     public static final int VK_F16 = 0xF003;
377  
378     /**
379      * Constant for the F17 function key.
380      * @since 1.2
381      */

382     public static final int VK_F17 = 0xF004;
383  
384     /**
385      * Constant for the F18 function key.
386      * @since 1.2
387      */

388     public static final int VK_F18 = 0xF005;
389  
390     /**
391      * Constant for the F19 function key.
392      * @since 1.2
393      */

394     public static final int VK_F19 = 0xF006;
395  
396     /**
397      * Constant for the F20 function key.
398      * @since 1.2
399      */

400     public static final int VK_F20 = 0xF007;
401  
402     /**
403      * Constant for the F21 function key.
404      * @since 1.2
405      */

406     public static final int VK_F21 = 0xF008;
407  
408     /**
409      * Constant for the F22 function key.
410      * @since 1.2
411      */

412     public static final int VK_F22 = 0xF009;
413  
414     /**
415      * Constant for the F23 function key.
416      * @since 1.2
417      */

418     public static final int VK_F23 = 0xF00A;
419  
420     /**
421      * Constant for the F24 function key.
422      * @since 1.2
423      */

424     public static final int VK_F24 = 0xF00B;
425  
426     public static final int VK_PRINTSCREEN = 0x9A;
427     public static final int VK_INSERT = 0x9B;
428     public static final int VK_HELP = 0x9C;
429     public static final int VK_META = 0x9D;
430
431     public static final int VK_BACK_QUOTE = 0xC0;
432     public static final int VK_QUOTE = 0xDE;
433
434     /**
435      * Constant for the numeric keypad <b>up</b> arrow key.
436      * @see #VK_UP
437      * @since 1.2
438      */

439     public static final int VK_KP_UP = 0xE0;
440
441     /**
442      * Constant for the numeric keypad <b>down</b> arrow key.
443      * @see #VK_DOWN
444      * @since 1.2
445      */

446     public static final int VK_KP_DOWN = 0xE1;
447
448     /**
449      * Constant for the numeric keypad <b>left</b> arrow key.
450      * @see #VK_LEFT
451      * @since 1.2
452      */

453     public static final int VK_KP_LEFT = 0xE2;
454
455     /**
456      * Constant for the numeric keypad <b>right</b> arrow key.
457      * @see #VK_RIGHT
458      * @since 1.2
459      */

460     public static final int VK_KP_RIGHT = 0xE3;
461     
462     /* For European keyboards */
463     /** @since 1.2 */
464     public static final int VK_DEAD_GRAVE = 0x80;
465     /** @since 1.2 */
466     public static final int VK_DEAD_ACUTE = 0x81;
467     /** @since 1.2 */
468     public static final int VK_DEAD_CIRCUMFLEX = 0x82;
469     /** @since 1.2 */
470     public static final int VK_DEAD_TILDE = 0x83;
471     /** @since 1.2 */
472     public static final int VK_DEAD_MACRON = 0x84;
473     /** @since 1.2 */
474     public static final int VK_DEAD_BREVE = 0x85;
475     /** @since 1.2 */
476     public static final int VK_DEAD_ABOVEDOT = 0x86;
477     /** @since 1.2 */
478     public static final int VK_DEAD_DIAERESIS = 0x87;
479     /** @since 1.2 */
480     public static final int VK_DEAD_ABOVERING = 0x88;
481     /** @since 1.2 */
482     public static final int VK_DEAD_DOUBLEACUTE = 0x89;
483     /** @since 1.2 */
484     public static final int VK_DEAD_CARON = 0x8a;
485     /** @since 1.2 */
486     public static final int VK_DEAD_CEDILLA = 0x8b;
487     /** @since 1.2 */
488     public static final int VK_DEAD_OGONEK = 0x8c;
489     /** @since 1.2 */
490     public static final int VK_DEAD_IOTA = 0x8d;
491     /** @since 1.2 */
492     public static final int VK_DEAD_VOICED_SOUND = 0x8e;
493     /** @since 1.2 */
494     public static final int VK_DEAD_SEMIVOICED_SOUND = 0x8f;
495
496     /** @since 1.2 */
497     public static final int VK_AMPERSAND = 0x96;
498     /** @since 1.2 */
499     public static final int VK_ASTERISK = 0x97;
500     /** @since 1.2 */
501     public static final int VK_QUOTEDBL = 0x98;
502     /** @since 1.2 */
503     public static final int VK_LESS = 0x99;
504
505     /** @since 1.2 */
506     public static final int VK_GREATER = 0xa0;
507     /** @since 1.2 */
508     public static final int VK_BRACELEFT = 0xa1;
509     /** @since 1.2 */
510     public static final int VK_BRACERIGHT = 0xa2;
511
512     /**
513      * Constant for the "@" key.
514      * @since 1.2
515      */

516     public static final int VK_AT = 0x0200;
517  
518     /**
519      * Constant for the ":" key.
520      * @since 1.2
521      */

522     public static final int VK_COLON = 0x0201;
523  
524     /**
525      * Constant for the "^" key.
526      * @since 1.2
527      */

528     public static final int VK_CIRCUMFLEX = 0x0202;
529  
530     /**
531      * Constant for the "$" key.
532      * @since 1.2
533      */

534     public static final int VK_DOLLAR = 0x0203;
535  
536     /**
537      * Constant for the Euro currency sign key.
538      * @since 1.2
539      */

540     public static final int VK_EURO_SIGN = 0x0204;
541  
542     /**
543      * Constant for the "!" key.
544      * @since 1.2
545      */

546     public static final int VK_EXCLAMATION_MARK = 0x0205;
547  
548     /**
549      * Constant for the inverted exclamation mark key.
550      * @since 1.2
551      */

552     public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
553  
554     /**
555      * Constant for the "(" key.
556      * @since 1.2
557      */

558     public static final int VK_LEFT_PARENTHESIS = 0x0207;
559  
560     /**
561      * Constant for the "#" key.
562      * @since 1.2
563      */

564     public static final int VK_NUMBER_SIGN = 0x0208;
565  
566     /**
567      * Constant for the "+" key.
568      * @since 1.2
569      */

570     public static final int VK_PLUS = 0x0209;
571  
572     /**
573      * Constant for the ")" key.
574      * @since 1.2
575      */

576     public static final int VK_RIGHT_PARENTHESIS = 0x020A;
577  
578     /**
579      * Constant for the "_" key.
580      * @since 1.2
581      */

582     public static final int VK_UNDERSCORE = 0x020B;
583  
584     /**
585      * Constant for the Microsoft Windows "Windows" key.
586      * It is used for both the left and right version of the key.
587      * @see #getKeyLocation()
588      * @since 1.5
589      */

590     public static final int VK_WINDOWS = 0x020C;
591  
592     /**
593      * Constant for the Microsoft Windows Context Menu key.
594      * @since 1.5
595      */

596     public static final int VK_CONTEXT_MENU = 0x020D;
597  
598     /* for input method support on Asian Keyboards */
599
600     /* not clear what this means - listed in Microsoft Windows API */
601     public static final int VK_FINAL = 0x0018;
602     
603     /** Constant for the Convert function key. */
604     /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
605     public static final int VK_CONVERT = 0x001C;
606
607     /** Constant for the Don't Convert function key. */
608     /* Japanese PC 106 keyboard: muhenkan */
609     public static final int VK_NONCONVERT = 0x001D;
610     
611     /** Constant for the Accept or Commit function key. */
612     /* Japanese Solaris keyboard: kakutei */
613     public static final int VK_ACCEPT = 0x001E;
614
615     /* not clear what this means - listed in Microsoft Windows API */
616     public static final int VK_MODECHANGE = 0x001F;
617
618     /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
619        might still be used on other platforms */

620     public static final int VK_KANA = 0x0015;
621
622     /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
623        might still be used for other platforms */

624     public static final int VK_KANJI = 0x0019;
625
626     /**
627      * Constant for the Alphanumeric function key.
628      * @since 1.2
629      */

630     /* Japanese PC 106 keyboard: eisuu */
631     public static final int VK_ALPHANUMERIC = 0x00F0;
632  
633     /**
634      * Constant for the Katakana function key.
635      * @since 1.2
636      */

637     /* Japanese PC 106 keyboard: katakana */
638     public static final int VK_KATAKANA = 0x00F1;
639  
640     /**
641      * Constant for the Hiragana function key.
642      * @since 1.2
643      */

644     /* Japanese PC 106 keyboard: hiragana */
645     public static final int VK_HIRAGANA = 0x00F2;
646  
647     /**
648      * Constant for the Full-Width Characters function key.
649      * @since 1.2
650      */

651     /* Japanese PC 106 keyboard: zenkaku */
652     public static final int VK_FULL_WIDTH = 0x00F3;
653  
654     /**
655      * Constant for the Half-Width Characters function key.
656      * @since 1.2
657      */

658     /* Japanese PC 106 keyboard: hankaku */
659     public static final int VK_HALF_WIDTH = 0x00F4;
660  
661     /**
662      * Constant for the Roman Characters function key.
663      * @since 1.2
664      */

665     /* Japanese PC 106 keyboard: roumaji */
666     public static final int VK_ROMAN_CHARACTERS = 0x00F5;
667  
668     /**
669      * Constant for the All Candidates function key.
670      * @since 1.2
671      */

672     /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */
673     public static final int VK_ALL_CANDIDATES = 0x0100;
674  
675     /**
676      * Constant for the Previous Candidate function key.
677      * @since 1.2
678      */

679     /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */
680     public static final int VK_PREVIOUS_CANDIDATE = 0x0101;
681  
682     /**
683      * Constant for the Code Input function key.
684      * @since 1.2
685      */

686     /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */
687     public static final int VK_CODE_INPUT = 0x0102;
688  
689     /**
690      * Constant for the Japanese-Katakana function key.
691      * This key switches to a Japanese input method and selects its Katakana input mode.
692      * @since 1.2
693      */

694     /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
695     public static final int VK_JAPANESE_KATAKANA = 0x0103;
696  
697     /**
698      * Constant for the Japanese-Hiragana function key.
699      * This key switches to a Japanese input method and selects its Hiragana input mode.
700      * @since 1.2
701      */

702     /* Japanese Macintosh keyboard */
703     public static final int VK_JAPANESE_HIRAGANA = 0x0104;
704  
705     /**
706      * Constant for the Japanese-Roman function key.
707      * This key switches to a Japanese input method and selects its Roman-Direct input mode.
708      * @since 1.2
709      */

710     /* Japanese Macintosh keyboard */
711     public static final int VK_JAPANESE_ROMAN = 0x0105;
712
713     /**
714      * Constant for the locking Kana function key.
715      * This key locks the keyboard into a Kana layout.
716      * @since 1.3
717      */

718     /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */
719     public static final int VK_KANA_LOCK = 0x0106;
720
721     /**
722      * Constant for the input method on/off key.
723      * @since 1.3
724      */

725     /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */
726     public static final int VK_INPUT_METHOD_ON_OFF = 0x0107;
727
728     /* for Sun keyboards */
729     /** @since 1.2 */
730     public static final int VK_CUT = 0xFFD1;
731     /** @since 1.2 */
732     public static final int VK_COPY = 0xFFCD;
733     /** @since 1.2 */
734     public static final int VK_PASTE = 0xFFCF;
735     /** @since 1.2 */
736     public static final int VK_UNDO = 0xFFCB;
737     /** @since 1.2 */
738     public static final int VK_AGAIN = 0xFFC9;
739     /** @since 1.2 */
740     public static final int VK_FIND = 0xFFD0;
741     /** @since 1.2 */
742     public static final int VK_PROPS = 0xFFCA;
743     /** @since 1.2 */
744     public static final int VK_STOP = 0xFFC8;
745     
746     /**
747      * Constant for the Compose function key.
748      * @since 1.2
749      */

750     public static final int VK_COMPOSE = 0xFF20;
751  
752     /**
753      * Constant for the AltGraph function key.
754      * @since 1.2
755      */

756     public static final int VK_ALT_GRAPH = 0xFF7E;
757
758     /**
759      * Constant for the Begin key.
760      * @since 1.5
761      */

762     public static final int VK_BEGIN = 0xFF58;
763
764     /**
765      * This value is used to indicate that the keyCode is unknown.
766      * KEY_TYPED events do not have a keyCode value; this value
767      * is used instead.
768      */

769     public static final int VK_UNDEFINED = 0x0;
770
771     /**
772      * KEY_PRESSED and KEY_RELEASED events which do not map to a
773      * valid Unicode character use this for the keyChar value.
774      */

775     public static final char CHAR_UNDEFINED = 0xFFFF;
776
777     /**
778      * A constant indicating that the keyLocation is indeterminate
779      * or not relevant.
780      * <code>KEY_TYPED</code> events do not have a keyLocation; this value
781      * is used instead.
782      * @since 1.4
783      */

784     public static final int KEY_LOCATION_UNKNOWN = 0;
785
786     /**
787      * A constant indicating that the key pressed or released
788      * is not distinguished as the left or right version of a key,
789      * and did not originate on the numeric keypad (or did not
790      * originate with a virtual key corresponding to the numeric
791      * keypad).
792      * @since 1.4
793      */

794     public static final int KEY_LOCATION_STANDARD = 1;
795
796     /**
797      * A constant indicating that the key pressed or released is in
798      * the left key location (there is more than one possible location
799      * for this key). Example: the left shift key.
800      * @since 1.4
801      */

802     public static final int KEY_LOCATION_LEFT = 2;
803
804     /**
805      * A constant indicating that the key pressed or released is in
806      * the right key location (there is more than one possible location
807      * for this key). Example: the right shift key.
808      * @since 1.4
809      */

810     public static final int KEY_LOCATION_RIGHT = 3;
811
812     /**
813      * A constant indicating that the key event originated on the
814      * numeric keypad or with a virtual key corresponding to the
815      * numeric keypad.
816      * @since 1.4
817      */

818     public static final int KEY_LOCATION_NUMPAD = 4;
819
820     /**
821      * The unique value assigned to each of the keys on the
822      * keyboard. There is a common set of key codes that
823      * can be fired by most keyboards.
824      * The symbolic name for a key code should be used rather
825      * than the code value itself.
826      *
827      * @serial
828      * @see #getKeyCode()
829      * @see #setKeyCode(int)
830      */

831     int keyCode;
832
833     /**
834      * <code>keyChar</code> is a valid unicode character
835      * that is fired by a key or a key combination on
836      * a keyboard.
837      *
838      * @serial
839      * @see #getKeyChar()
840      * @see #setKeyChar(char)
841      */

842     char keyChar;
843
844     /**
845      * The location of the key on the keyboard.
846      *
847      * Some keys occur more than once on a keyboard, e.g. the left and
848      * right shift keys. Additionally, some keys occur on the numeric
849      * keypad. This variable is used to distinguish such keys.
850      *
851      * The only legal values are <code>KEY_LOCATION_UNKNOWN</code>,
852      * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
853      * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
854      *
855      * @serial
856      * @see #getKeyLocation()
857      */

858     int keyLocation;
859
860     //set from native code.
861
private transient long rawCode = 0;
862
863     /*
864      * JDK 1.1 serialVersionUID
865      */

866     private static final long serialVersionUID = -2352130953028126954L;
867
868     static {
869         /* ensure that the necessary native libraries are loaded */
870     NativeLibLoader.loadLibraries();
871         if (!GraphicsEnvironment.isHeadless()) {
872             initIDs();
873         }
874     }
875
876     /**
877      * Initialize JNI field and method IDs for fields that may be
878      * accessed from C.
879      */

880     private static native void initIDs();
881
882     /**
883      * Constructs a <code>KeyEvent</code> object.
884      * <p>Note that passing in an invalid <code>id</code> results in
885      * unspecified behavior. This method throws an
886      * <code>IllegalArgumentException</code> if <code>source</code>
887      * is <code>null</code>.
888      *
889      * @param source the <code>Component</code> that originated the event
890      * @param id an integer identifying the type of event
891      * @param when a long integer that specifies the time the event
892      * occurred
893      * @param modifiers the modifier keys down during event (shift, ctrl,
894      * alt, meta)
895      * Either extended _DOWN_MASK or old _MASK modifiers
896      * should be used, but both models should not be mixed
897      * in one event. Use of the extended modifiers is
898      * preferred.
899      * @param keyCode the integer code for an actual key, or VK_UNDEFINED
900      * (for a key-typed event)
901      * @param keyChar the Unicode character generated by this event, or
902      * CHAR_UNDEFINED (for key-pressed and key-released
903      * events which do not map to a valid Unicode character)
904      * @param keyLocation identifies the key location. The only legal
905      * values are <code>KEY_LOCATION_UNKNOWN</code>,
906      * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
907      * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
908      * @throws IllegalArgumentException
909      * if <code>id</code> is <code>KEY_TYPED</code> and
910      * <code>keyChar</code> is <code>CHAR_UNDEFINED</code>;
911      * or if <code>id</code> is <code>KEY_TYPED</code> and
912      * <code>keyCode</code> is not <code>VK_UNDEFINED</code>;
913      * or if <code>id</code> is <code>KEY_TYPED</code> and
914      * <code>keyLocation</code> is not <code>KEY_LOCATION_UNKNOWN</code>;
915      * or if <code>keyLocation</code> is not one of the legal
916      * values enumerated above.
917      * @throws IllegalArgumentException if <code>source</code> is null
918      * @since 1.4
919      */

920     private KeyEvent(Component JavaDoc source, int id, long when, int modifiers,
921                     int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
922         this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
923         this.isProxyActive = isProxyActive;
924     }
925     public KeyEvent(Component JavaDoc source, int id, long when, int modifiers,
926                     int keyCode, char keyChar, int keyLocation) {
927         super(source, id, when, modifiers);
928         if (id == KEY_TYPED) {
929             if (keyChar == CHAR_UNDEFINED) {
930                 throw new IllegalArgumentException JavaDoc("invalid keyChar");
931             }
932             if (keyCode != VK_UNDEFINED) {
933                 throw new IllegalArgumentException JavaDoc("invalid keyCode");
934             }
935             if (keyLocation != KEY_LOCATION_UNKNOWN) {
936                 throw new IllegalArgumentException JavaDoc("invalid keyLocation");
937             }
938         }
939
940         this.keyCode = keyCode;
941         this.keyChar = keyChar;
942
943         if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
944             (keyLocation > KEY_LOCATION_NUMPAD)) {
945             throw new IllegalArgumentException JavaDoc("invalid keyLocation");
946         }
947         this.keyLocation = keyLocation;
948         if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
949         setNewModifiers();
950     } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
951         setOldModifiers();
952     }
953     }
954
955     /**
956      * Constructs a <code>KeyEvent</code> object.
957      * <p>Note that passing in an invalid <code>id</code> results in
958      * unspecified behavior. This method throws an
959      * <code>IllegalArgumentException</code> if <code>source</code>
960      * is <code>null</code>.
961      *
962      * @param source the <code>Component</code> that originated the event
963      * @param id an integer identifying the type of event
964      * @param when a long integer that specifies the time the event
965      * occurred
966      * @param modifiers the modifier keys down during event (shift, ctrl,
967      * alt, meta)
968      * Either extended _DOWN_MASK or old _MASK modifiers
969      * should be used, but both models should not be mixed
970      * in one event. Use of the extended modifiers is
971      * preferred.
972      * @param keyCode the integer code for an actual key, or VK_UNDEFINED
973      * (for a key-typed event)
974      * @param keyChar the Unicode character generated by this event, or
975      * CHAR_UNDEFINED (for key-pressed and key-released
976      * events which do not map to a valid Unicode character)
977      * @throws IllegalArgumentException if <code>id</code> is
978      * <code>KEY_TYPED</code> and <code>keyChar</code> is
979      * <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
980      * <code>KEY_TYPED</code> and <code>keyCode</code> is not
981      * <code>VK_UNDEFINED</code>
982      * @throws IllegalArgumentException if <code>source</code> is null
983      */

984     public KeyEvent(Component JavaDoc source, int id, long when, int modifiers,
985                     int keyCode, char keyChar) {
986         this(source, id, when, modifiers, keyCode, keyChar,
987           KEY_LOCATION_UNKNOWN);
988     }
989
990     /**
991      * @deprecated as of JDK1.1
992      */

993     @Deprecated JavaDoc
994     public KeyEvent(Component JavaDoc source, int id, long when, int modifiers,
995                     int keyCode) {
996         this(source, id, when, modifiers, keyCode, (char)keyCode);
997     }
998
999     /**
1000     * Returns the integer keyCode associated with the key in this event.
1001     *
1002     * @return the integer code for an actual key on the keyboard.
1003     * (For <code>KEY_TYPED</code> events, the keyCode is
1004     * <code>VK_UNDEFINED</code>.)
1005     */

1006    public int getKeyCode() {
1007        return keyCode;
1008    }
1009
1010    /**
1011     * Set the keyCode value to indicate a physical key.
1012     *
1013     * @param keyCode an integer corresponding to an actual key on the keyboard.
1014     */

1015    public void setKeyCode(int keyCode) {
1016        this.keyCode = keyCode;
1017    }
1018
1019    /**
1020     * Returns the character associated with the key in this event.
1021     * For example, the <code>KEY_TYPED</code> event for shift + "a"
1022     * returns the value for "A".
1023     * <p>
1024     * <code>KEY_PRESSED</code> and <code>KEY_RELEASED</code> events
1025     * are not intended for reporting of character input. Therefore,
1026     * the values returned by this method are guaranteed to be
1027     * meaningful only for <code>KEY_TYPED</code> events.
1028     *
1029     * @return the Unicode character defined for this key event.
1030     * If no valid Unicode character exists for this key event,
1031     * <code>CHAR_UNDEFINED</code> is returned.
1032     */

1033    public char getKeyChar() {
1034        return keyChar;
1035    }
1036
1037    /**
1038     * Set the keyChar value to indicate a logical character.
1039     *
1040     * @param keyChar a char corresponding to to the combination of keystrokes
1041     * that make up this event.
1042     */

1043    public void setKeyChar(char keyChar) {
1044        this.keyChar = keyChar;
1045    }
1046
1047    /**
1048     * Set the modifiers to indicate additional keys that were held down
1049     * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
1050     * <p>
1051     * NOTE: use of this method is not recommended, because many AWT
1052     * implementations do not recognize modifier changes. This is
1053     * especially true for <code>KEY_TYPED</code> events where the shift
1054     * modifier is changed.
1055     *
1056     * @param modifiers an integer combination of the modifier constants.
1057     * @see InputEvent
1058     * @deprecated as of JDK1.1.4
1059     */

1060    @Deprecated JavaDoc
1061    public void setModifiers(int modifiers) {
1062        this.modifiers = modifiers;
1063        if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1064        setNewModifiers();
1065    } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1066        setOldModifiers();
1067    }
1068    }
1069
1070    /**
1071     * Returns the location of the key that originated this key event.
1072     *
1073     * Some keys occur more than once on a keyboard, e.g. the left and
1074     * right shift keys. Additionally, some keys occur on the numeric
1075     * keypad. This provides a way of distinguishing such keys.
1076     *
1077     * @return the location of the key that was pressed or released.
1078     * Always returns <code>KEY_LOCATION_UNKNOWN</code> for
1079     * <code>KEY_TYPED</code> events.
1080     * @since 1.4
1081     */

1082    public int getKeyLocation() {
1083        return keyLocation;
1084    }
1085
1086    /**
1087     * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
1088     * These strings can be localized by changing the awt.properties file.
1089     *
1090     * @return a string containing a text description for a physical key,
1091     * identified by its keyCode
1092     */

1093    public static String JavaDoc getKeyText(int keyCode) {
1094        if (keyCode >= VK_0 && keyCode <= VK_9 ||
1095            keyCode >= VK_A && keyCode <= VK_Z) {
1096            return String.valueOf((char)keyCode);
1097        }
1098
1099        switch(keyCode) {
1100          case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
1101          case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
1102          case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
1103          case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
1104          case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
1105          case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
1106          case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
1107          case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
1108          case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
1109          case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
1110          case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
1111          case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
1112          case VK_END: return Toolkit.getProperty("AWT.end", "End");
1113          case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
1114          case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1115          case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
1116          case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1117          case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1118          case VK_BEGIN: return Toolkit.getProperty("AWT.begin", "Begin");
1119
1120          // modifiers
1121
case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
1122          case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
1123          case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
1124          case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
1125          case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
1126
1127          // punctuation
1128
case VK_COMMA: return Toolkit.getProperty("AWT.comma", "Comma");
1129          case VK_PERIOD: return Toolkit.getProperty("AWT.period", "Period");
1130          case VK_SLASH: return Toolkit.getProperty("AWT.slash", "Slash");
1131          case VK_SEMICOLON: return Toolkit.getProperty("AWT.semicolon", "Semicolon");
1132          case VK_EQUALS: return Toolkit.getProperty("AWT.equals", "Equals");
1133          case VK_OPEN_BRACKET: return Toolkit.getProperty("AWT.openBracket", "Open Bracket");
1134          case VK_BACK_SLASH: return Toolkit.getProperty("AWT.backSlash", "Back Slash");
1135          case VK_CLOSE_BRACKET: return Toolkit.getProperty("AWT.closeBracket", "Close Bracket");
1136
1137          // numpad numeric keys handled below
1138
case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
1139          case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
1140          case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,");
1141          case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
1142          case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
1143          case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
1144          case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
1145          case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
1146          case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
1147
1148          case VK_WINDOWS: return Toolkit.getProperty("AWT.windows", "Windows");
1149          case VK_CONTEXT_MENU: return Toolkit.getProperty("AWT.context", "Context Menu");
1150
1151          case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
1152          case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
1153          case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
1154          case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
1155          case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
1156          case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
1157          case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
1158          case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
1159          case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
1160          case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
1161          case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
1162          case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
1163          case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");
1164          case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");
1165          case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");
1166          case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");
1167          case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");
1168          case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");
1169          case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");
1170          case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");
1171          case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");
1172          case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");
1173          case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");
1174          case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");
1175
1176          case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
1177          case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
1178          case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
1179          case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
1180          case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
1181             
1182          case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");
1183          case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");
1184          case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");
1185          case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
1186
1187          case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
1188          case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
1189          case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex");
1190          case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
1191          case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
1192          case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
1193          case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot");
1194          case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis");
1195          case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring");
1196          case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute");
1197          case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
1198          case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla");
1199          case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
1200          case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
1201          case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound");
1202          case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound");
1203
1204          case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand");
1205          case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk");
1206          case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
1207          case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less");
1208          case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater");
1209          case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
1210          case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace");
1211          case VK_AT: return Toolkit.getProperty("AWT.at", "At");
1212          case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon");
1213          case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex");
1214          case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar");
1215          case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro");
1216          case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark");
1217          case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark");
1218          case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis");
1219          case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign");
1220          case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus");
1221          case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus");
1222          case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis");
1223          case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore");
1224
1225          case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
1226          case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
1227          case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
1228          case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
1229          case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
1230      case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
1231      case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
1232          case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric");
1233      case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana");
1234      case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana");
1235      case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
1236      case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
1237      case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters");
1238      case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates");
1239      case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate");
1240      case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input");
1241      case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana");
1242      case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana");
1243      case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman");
1244      case VK_KANA_LOCK: return Toolkit.getProperty("AWT.kanaLock", "Kana Lock");
1245      case VK_INPUT_METHOD_ON_OFF: return Toolkit.getProperty("AWT.inputMethodOnOff", "Input Method On/Off");
1246
1247          case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again");
1248          case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo");
1249          case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy");
1250          case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste");
1251          case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut");
1252          case VK_FIND: return Toolkit.getProperty("AWT.find", "Find");
1253          case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props");
1254          case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop");
1255        }
1256
1257        if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
1258            String JavaDoc numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
1259        char c = (char)(keyCode - VK_NUMPAD0 + '0');
1260            return numpad + "-" + c;
1261        }
1262
1263        String JavaDoc unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1264        return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1265    }
1266
1267    /**
1268     * Returns a <code>String</code> describing the modifier key(s),
1269     * such as "Shift", or "Ctrl+Shift". These strings can be
1270     * localized by changing the <code>awt.properties</code> file.
1271     * <p>
1272     * Note that <code>InputEvent.ALT_MASK</code> and
1273     * <code>InputEvent.BUTTON2_MASK</code> have the same value,
1274     * so the string "Alt" is returned for both modifiers. Likewise,
1275     * <code>InputEvent.META_MASK</code> and
1276     * <code>InputEvent.BUTTON3_MASK</code> have the same value,
1277     * so the string "Meta" is returned for both modifiers.
1278     *
1279     * @return string a text description of the combination of modifier
1280     * keys that were held down during the event
1281     * @see InputEvent#getModifiersExText(int)
1282     */

1283    public static String JavaDoc getKeyModifiersText(int modifiers) {
1284        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1285        if ((modifiers & InputEvent.META_MASK) != 0) {
1286            buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1287            buf.append("+");
1288        }
1289        if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1290            buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1291            buf.append("+");
1292        }
1293        if ((modifiers & InputEvent.ALT_MASK) != 0) {
1294            buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1295            buf.append("+");
1296        }
1297        if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1298            buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1299            buf.append("+");
1300        }
1301        if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1302            buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
1303            buf.append("+");
1304        }
1305        if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1306            buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
1307            buf.append("+");
1308        }
1309        if (buf.length() > 0) {
1310            buf.setLength(buf.length()-1); // remove trailing '+'
1311
}
1312        return buf.toString();
1313    }
1314
1315
1316    /**
1317     * Returns whether the key in this event is an "action" key.
1318     * Typically an action key does not fire a unicode character and is
1319     * not a modifier key.
1320     *
1321     * @return <code>true</code> if the key is an "action" key,
1322     * <code>false</code> otherwise
1323     */

1324    public boolean isActionKey() {
1325        switch (keyCode) {
1326          case VK_HOME:
1327          case VK_END:
1328          case VK_PAGE_UP:
1329          case VK_PAGE_DOWN:
1330          case VK_UP:
1331          case VK_DOWN:
1332          case VK_LEFT:
1333          case VK_RIGHT:
1334          case VK_BEGIN:
1335
1336      case VK_KP_LEFT:
1337      case VK_KP_UP:
1338      case VK_KP_RIGHT:
1339      case VK_KP_DOWN:
1340
1341          case VK_F1:
1342          case VK_F2:
1343          case VK_F3:
1344          case VK_F4:
1345          case VK_F5:
1346          case VK_F6:
1347          case VK_F7:
1348          case VK_F8:
1349          case VK_F9:
1350          case VK_F10:
1351          case VK_F11:
1352          case VK_F12:
1353          case VK_F13:
1354          case VK_F14:
1355          case VK_F15:
1356          case VK_F16:
1357          case VK_F17:
1358          case VK_F18:
1359          case VK_F19:
1360          case VK_F20:
1361          case VK_F21:
1362          case VK_F22:
1363          case VK_F23:
1364          case VK_F24:
1365          case VK_PRINTSCREEN:
1366          case VK_SCROLL_LOCK:
1367          case VK_CAPS_LOCK:
1368          case VK_NUM_LOCK:
1369          case VK_PAUSE:
1370          case VK_INSERT:
1371
1372          case VK_FINAL:
1373          case VK_CONVERT:
1374          case VK_NONCONVERT:
1375          case VK_ACCEPT:
1376          case VK_MODECHANGE:
1377      case VK_KANA:
1378      case VK_KANJI:
1379          case VK_ALPHANUMERIC:
1380      case VK_KATAKANA:
1381      case VK_HIRAGANA:
1382      case VK_FULL_WIDTH:
1383      case VK_HALF_WIDTH:
1384      case VK_ROMAN_CHARACTERS:
1385      case VK_ALL_CANDIDATES:
1386      case VK_PREVIOUS_CANDIDATE:
1387      case VK_CODE_INPUT:
1388      case VK_JAPANESE_KATAKANA:
1389      case VK_JAPANESE_HIRAGANA:
1390      case VK_JAPANESE_ROMAN:
1391      case VK_KANA_LOCK:
1392      case VK_INPUT_METHOD_ON_OFF:
1393
1394          case VK_AGAIN:
1395          case VK_UNDO:
1396          case VK_COPY:
1397          case VK_PASTE:
1398          case VK_CUT:
1399          case VK_FIND:
1400          case VK_PROPS:
1401          case VK_STOP:
1402
1403          case VK_HELP:
1404          case VK_WINDOWS:
1405          case VK_CONTEXT_MENU:
1406              return true;
1407        }
1408        return false;
1409    }
1410
1411    /**
1412     * Returns a parameter string identifying this event.
1413     * This method is useful for event logging and for debugging.
1414     *
1415     * @return a string identifying the event and its attributes
1416     */

1417    public String JavaDoc paramString() {
1418        StringBuffer JavaDoc str = new StringBuffer JavaDoc(100);
1419
1420        switch (id) {
1421          case KEY_PRESSED:
1422            str.append("KEY_PRESSED");
1423            break;
1424          case KEY_RELEASED:
1425            str.append("KEY_RELEASED");
1426            break;
1427          case KEY_TYPED:
1428            str.append("KEY_TYPED");
1429            break;
1430          default:
1431            str.append("unknown type");
1432            break;
1433        }
1434
1435        str.append(",keyCode=").append(keyCode);
1436        str.append(",keyText=").append(getKeyText(keyCode));
1437
1438        /* Some keychars don't print well, e.g. escape, backspace,
1439         * tab, return, delete, cancel. Get keyText for the keyCode
1440         * instead of the keyChar.
1441         */

1442        str.append(",keyChar=");
1443        switch (keyChar) {
1444          case '\b':
1445            str.append(getKeyText(VK_BACK_SPACE));
1446            break;
1447          case '\t':
1448            str.append(getKeyText(VK_TAB));
1449            break;
1450          case '\n':
1451            str.append(getKeyText(VK_ENTER));
1452            break;
1453          case '\u0018':
1454            str.append(getKeyText(VK_CANCEL));
1455            break;
1456          case '\u001b':
1457            str.append(getKeyText(VK_ESCAPE));
1458            break;
1459          case '\u007f':
1460            str.append(getKeyText(VK_DELETE));
1461            break;
1462          case CHAR_UNDEFINED:
1463            str.append(Toolkit.getProperty("AWT.undefined", "Undefined"));
1464            str.append(" keyChar");
1465            break;
1466          default:
1467            str.append("'").append(keyChar).append("'");
1468            break;
1469        }
1470
1471        if (getModifiers() != 0) {
1472            str.append(",modifiers=").append(getKeyModifiersText(modifiers));
1473        }
1474        if (getModifiersEx() != 0) {
1475            str.append(",extModifiers=").append(getModifiersExText(modifiers));
1476        }
1477
1478        str.append(",keyLocation=");
1479        switch (keyLocation) {
1480          case KEY_LOCATION_UNKNOWN:
1481            str.append("KEY_LOCATION_UNKNOWN");
1482            break;
1483          case KEY_LOCATION_STANDARD:
1484            str.append("KEY_LOCATION_STANDARD");
1485            break;
1486          case KEY_LOCATION_LEFT:
1487            str.append("KEY_LOCATION_LEFT");
1488            break;
1489          case KEY_LOCATION_RIGHT:
1490            str.append("KEY_LOCATION_RIGHT");
1491            break;
1492          case KEY_LOCATION_NUMPAD:
1493            str.append("KEY_LOCATION_NUMPAD");
1494            break;
1495          default:
1496            str.append("KEY_LOCATION_UNKNOWN");
1497            break;
1498        }
1499    str.append(",rawCode=").append(rawCode);
1500
1501        return str.toString();
1502    }
1503
1504    /**
1505     * Sets new modifiers by the old ones. The key modifiers
1506     * override overlaping mouse modifiers.
1507     */

1508    private void setNewModifiers() {
1509    if ((modifiers & SHIFT_MASK) != 0) {
1510        modifiers |= SHIFT_DOWN_MASK;
1511    }
1512    if ((modifiers & ALT_MASK) != 0) {
1513        modifiers |= ALT_DOWN_MASK;
1514    }
1515    if ((modifiers & CTRL_MASK) != 0) {
1516        modifiers |= CTRL_DOWN_MASK;
1517    }
1518    if ((modifiers & META_MASK) != 0) {
1519        modifiers |= META_DOWN_MASK;
1520    }
1521    if ((modifiers & ALT_GRAPH_MASK) != 0) {
1522        modifiers |= ALT_GRAPH_DOWN_MASK;
1523    }
1524    if ((modifiers & BUTTON1_MASK) != 0) {
1525        modifiers |= BUTTON1_DOWN_MASK;
1526    }
1527    }
1528
1529    /**
1530     * Sets old modifiers by the new ones.
1531     */

1532    private void setOldModifiers() {
1533    if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1534        modifiers |= SHIFT_MASK;
1535    }
1536    if ((modifiers & ALT_DOWN_MASK) != 0) {
1537        modifiers |= ALT_MASK;
1538    }
1539    if ((modifiers & CTRL_DOWN_MASK) != 0) {
1540        modifiers |= CTRL_MASK;
1541    }
1542    if ((modifiers & META_DOWN_MASK) != 0) {
1543        modifiers |= META_MASK;
1544    }
1545    if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1546        modifiers |= ALT_GRAPH_MASK;
1547    }
1548    if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1549        modifiers |= BUTTON1_MASK;
1550    }
1551    }
1552
1553    /**
1554     * Sets new modifiers by the old ones. The key modifiers
1555     * override overlaping mouse modifiers.
1556     * @serial
1557     */

1558    private void readObject(ObjectInputStream JavaDoc s)
1559      throws IOException JavaDoc, ClassNotFoundException JavaDoc {
1560    s.defaultReadObject();
1561        if (getModifiers() != 0 && getModifiersEx() == 0) {
1562        setNewModifiers();
1563    }
1564    }
1565}
1566
1567
Popular Tags