Merge from the pain train
[official-gcc.git] / libjava / java / awt / event / KeyEvent.java
blobcef05ae62415ae1295e70f58fecee6030256541d
1 /* KeyEvent.java -- event for key presses
2 Copyright (C) 1999, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.event;
41 import gnu.java.awt.EventModifier;
43 import java.awt.Component;
44 import java.io.IOException;
45 import java.io.ObjectInputStream;
47 /**
48 * This event is generated when a key is pressed or released. There are two
49 * categories of key events:
51 * <p><em>"Key typed" events</em> are higher-level, and have already
52 * compensated for modifiers and keyboard layout to generate a single Unicode
53 * character. It may take several key press events to generate one key typed.
54 * The <code>getKeyCode</code> method will return <code>VK_UNDEFINED</code>,
55 * and <code>getKeyChar</code> will return a valid Unicode character or
56 * <code>CHAR_UNDEFINED</code>.
58 * <p><em>"Key pressed" and "key released" events</em> are lower-level, and
59 * are platform and keyboard dependent. They correspond to the actaul motion
60 * on a keyboard, and return a virtual key code which labels the key that was
61 * pressed. The <code>getKeyCode</code> method will return one of the
62 * <code>VK_*</code> constants (except VK_UNDEFINED), and the
63 * <code>getKeyChar</code> method is undefined.
65 * <p>Some keys do not generate key typed events, such as the F1 or HELP keys.
66 * Not all keyboards can generate all virtual keys, and no attempt is made to
67 * simulate the ones that can't be typed. Virtual keys correspond to the
68 * keyboard layout, so for example, VK_Q in English is VK_A in French. Also,
69 * there are some additional virtual keys to ease handling of actions, such
70 * as VK_ALL_CANDIDATES in place of ALT+VK_CONVERT. Do not rely on the value
71 * of the VK_* constants, except for VK_ENTER, VK_BACK_SPACE, and VK_TAB.
73 * @author Aaron M. Renn (arenn@urbanophile.com)
74 * @author Eric Blake (ebb9@email.byu.edu)
75 * @see KeyAdapter
76 * @see KeyListener
77 * @since 1.1
78 * @status updated to 1.4
80 public class KeyEvent extends InputEvent
82 /**
83 * Compatible with JDK 1.1+.
85 private static final long serialVersionUID = -2352130953028126954L;
87 /** This is the first id in the range of event ids used by this class. */
88 public static final int KEY_FIRST = 400;
90 /** This is the last id in the range of event ids used by this class. */
91 public static final int KEY_LAST = 402;
93 /**
94 * This event id indicates a key was typed, which is a key press followed
95 * by a key release to generate an actual Unicode character. It may take
96 * several key presses to generate one key typed event, and some action
97 * keys have no corresponding key typed.
99 public static final int KEY_TYPED = 400;
101 /** This event id indicates a key was pressed. */
102 public static final int KEY_PRESSED = 401;
104 /** This event it indicates a key was released. */
105 public static final int KEY_RELEASED = 402;
107 /** The virtual key Enter, which will always map to '\n'. */
108 public static final int VK_ENTER = '\n';
110 /** The virtual key Backspace, which will always map to '\b'. */
111 public static final int VK_BACK_SPACE = '\b';
113 /** The virtual key Tab, which will always map to '\t'. */
114 public static final int VK_TAB = '\t';
116 /** The virtual key Cancel. */
117 public static final int VK_CANCEL = 3;
119 /** The virtual key VK_CLEAR. */
120 public static final int VK_CLEAR = 12;
122 /** The virtual key VK_SHIFT. */
123 public static final int VK_SHIFT = 16;
125 /** The virtual key VK_CONTROL. */
126 public static final int VK_CONTROL = 17;
128 /** The virtual key VK_ALT. */
129 public static final int VK_ALT = 18;
131 /** The virtual key VK_PAUSE. */
132 public static final int VK_PAUSE = 19;
134 /** The virtual key VK_CAPS_LOCK. */
135 public static final int VK_CAPS_LOCK = 20;
137 /** The virtual key VK_ESCAPE. */
138 public static final int VK_ESCAPE = 27;
140 /** The virtual key VK_SPACE. */
141 public static final int VK_SPACE = ' ';
143 /** The virtual key VK_PAGE_UP. */
144 public static final int VK_PAGE_UP = 33;
146 /** The virtual key VK_PAGE_DOWN. */
147 public static final int VK_PAGE_DOWN = 34;
149 /** The virtual key VK_END. */
150 public static final int VK_END = 35;
152 /** The virtual key VK_HOME. */
153 public static final int VK_HOME = 36;
156 * The virtual key for the non-numpad VK_LEFT.
158 * @see #VK_KP_LEFT
160 public static final int VK_LEFT = 37;
163 * The virtual key for the non-numpad VK_UP.
165 * @see #VK_KP_UP
167 public static final int VK_UP = 38;
170 * The virtual key for the non-numpad VK_RIGHT.
172 * @see #VK_KP_RIGHT
174 public static final int VK_RIGHT = 39;
177 * The virtual key for the non-numpad VK_DOWN.
179 * @see #VK_KP_DOWN
181 public static final int VK_DOWN = 40;
183 /** The virtual key VK_COMMA. */
184 public static final int VK_COMMA = ',';
187 * The virtual key VK_MINUS.
189 * @since 1.2
191 public static final int VK_MINUS = '-';
193 /** The virtual key VK_PERIOD. */
194 public static final int VK_PERIOD = '.';
196 /** The virtual key VK_SLASH. */
197 public static final int VK_SLASH = '/';
199 /** The virtual key VK_0. */
200 public static final int VK_0 = '0';
202 /** The virtual key VK_1. */
203 public static final int VK_1 = '1';
205 /** The virtual key VK_2. */
206 public static final int VK_2 = '2';
208 /** The virtual key VK_3. */
209 public static final int VK_3 = '3';
211 /** The virtual key VK_4. */
212 public static final int VK_4 = '4';
214 /** The virtual key VK_5. */
215 public static final int VK_5 = '5';
217 /** The virtual key VK_6. */
218 public static final int VK_6 = '6';
220 /** The virtual key VK_7. */
221 public static final int VK_7 = '7';
223 /** The virtual key VK_8. */
224 public static final int VK_8 = '8';
226 /** The virtual key VK_9. */
227 public static final int VK_9 = '9';
229 /** The virtual key VK_SEMICOLON. */
230 public static final int VK_SEMICOLON = ';';
232 /** The virtual key VK_EQUALS. */
233 public static final int VK_EQUALS = '=';
235 /** The virtual key VK_A. */
236 public static final int VK_A = 'A';
238 /** The virtual key VK_B. */
239 public static final int VK_B = 'B';
241 /** The virtual key VK_C. */
242 public static final int VK_C = 'C';
244 /** The virtual key VK_D. */
245 public static final int VK_D = 'D';
247 /** The virtual key VK_E. */
248 public static final int VK_E = 'E';
250 /** The virtual key VK_F. */
251 public static final int VK_F = 'F';
253 /** The virtual key VK_G. */
254 public static final int VK_G = 'G';
256 /** The virtual key VK_H. */
257 public static final int VK_H = 'H';
259 /** The virtual key VK_I. */
260 public static final int VK_I = 'I';
262 /** The virtual key VK_J. */
263 public static final int VK_J = 'J';
265 /** The virtual key VK_K. */
266 public static final int VK_K = 'K';
268 /** The virtual key VK_L. */
269 public static final int VK_L = 'L';
271 /** The virtual key VK_M. */
272 public static final int VK_M = 'M';
274 /** The virtual key VK_N. */
275 public static final int VK_N = 'N';
277 /** The virtual key VK_O. */
278 public static final int VK_O = 'O';
280 /** The virtual key VK_P. */
281 public static final int VK_P = 'P';
283 /** The virtual key VK_Q. */
284 public static final int VK_Q = 'Q';
286 /** The virtual key VK_R. */
287 public static final int VK_R = 'R';
289 /** The virtual key VK_S. */
290 public static final int VK_S = 'S';
292 /** The virtual key VK_T. */
293 public static final int VK_T = 'T';
295 /** The virtual key VK_U. */
296 public static final int VK_U = 'U';
298 /** The virtual key VK_V. */
299 public static final int VK_V = 'V';
301 /** The virtual key VK_W. */
302 public static final int VK_W = 'W';
304 /** The virtual key VK_X. */
305 public static final int VK_X = 'X';
307 /** The virtual key VK_Y. */
308 public static final int VK_Y = 'Y';
310 /** The virtual key VK_Z. */
311 public static final int VK_Z = 'Z';
313 /** The virtual key VK_OPEN_BRACKET. */
314 public static final int VK_OPEN_BRACKET = '[';
316 /** The virtual key VK_BACK_SLASH. */
317 public static final int VK_BACK_SLASH = '\\';
319 /** The virtual key VK_CLOSE_BRACKET. */
320 public static final int VK_CLOSE_BRACKET = ']';
322 /** The virtual key VK_NUMPAD0. */
323 public static final int VK_NUMPAD0 = 96;
325 /** The virtual key VK_NUMPAD1. */
326 public static final int VK_NUMPAD1 = 97;
328 /** The virtual key VK_NUMPAD2. */
329 public static final int VK_NUMPAD2 = 98;
331 /** The virtual key VK_NUMPAD3. */
332 public static final int VK_NUMPAD3 = 99;
334 /** The virtual key VK_NUMPAD4. */
335 public static final int VK_NUMPAD4 = 100;
337 /** The virtual key VK_NUMPAD5. */
338 public static final int VK_NUMPAD5 = 101;
340 /** The virtual key VK_NUMPAD6. */
341 public static final int VK_NUMPAD6 = 102;
343 /** The virtual key VK_NUMPAD7. */
344 public static final int VK_NUMPAD7 = 103;
346 /** The virtual key VK_NUMPAD8. */
347 public static final int VK_NUMPAD8 = 104;
349 /** The virtual key VK_NUMPAD9. */
350 public static final int VK_NUMPAD9 = 105;
352 /** The virtual key VK_MULTIPLY. */
353 public static final int VK_MULTIPLY = 106;
355 /** The virtual key VK_ADD. */
356 public static final int VK_ADD = 107;
359 * The virtual key VK_SEPARATOR, handily mispelled for those who can't
360 * figure it out.
362 * @deprecated use {@link #VK_SEPARATOR}
364 public static final int VK_SEPARATER = 108;
367 * The virtual key VK_SEPARATOR.
369 * @since 1.4
371 public static final int VK_SEPARATOR = 108;
373 /** The virtual key VK_SUBTRACT. */
374 public static final int VK_SUBTRACT = 109;
376 /** The virtual key VK_DECIMAL. */
377 public static final int VK_DECIMAL = 110;
379 /** The virtual key VK_DIVIDE. */
380 public static final int VK_DIVIDE = 111;
382 /** The virtual key VK_DELETE. */
383 public static final int VK_DELETE = 127;
385 /** The virtual key VK_NUM_LOCK. */
386 public static final int VK_NUM_LOCK = 144;
388 /** The virtual key VK_SCROLL_LOCK. */
389 public static final int VK_SCROLL_LOCK = 145;
391 /** The virtual key VK_F1. */
392 public static final int VK_F1 = 112;
394 /** The virtual key VK_F2. */
395 public static final int VK_F2 = 113;
397 /** The virtual key VK_F3. */
398 public static final int VK_F3 = 114;
400 /** The virtual key VK_F4. */
401 public static final int VK_F4 = 115;
403 /** The virtual key VK_F5. */
404 public static final int VK_F5 = 116;
406 /** The virtual key VK_F6. */
407 public static final int VK_F6 = 117;
409 /** The virtual key VK_F7. */
410 public static final int VK_F7 = 118;
412 /** The virtual key VK_F8. */
413 public static final int VK_F8 = 119;
415 /** The virtual key VK_F9. */
416 public static final int VK_F9 = 120;
418 /** The virtual key VK_F10. */
419 public static final int VK_F10 = 121;
421 /** The virtual key VK_F11. */
422 public static final int VK_F11 = 122;
424 /** The virtual key VK_F12. */
425 public static final int VK_F12 = 123;
428 * The virtual key VK_F13.
430 * @since 1.2
432 public static final int VK_F13 = 61440;
435 * The virtual key VK_F14.
437 * @since 1.2
439 public static final int VK_F14 = 61441;
442 * The virtual key VK_F15.
444 * @since 1.2
446 public static final int VK_F15 = 61442;
449 * The virtual key VK_F16.
451 * @since 1.2
453 public static final int VK_F16 = 61443;
456 * The virtual key VK_F17.
458 * @since 1.2
460 public static final int VK_F17 = 61444;
463 * The virtual key VK_F18.
465 * @since 1.2
467 public static final int VK_F18 = 61445;
470 * The virtual key VK_F19.
472 * @since 1.2
474 public static final int VK_F19 = 61446;
477 * The virtual key VK_F20.
479 * @since 1.2
481 public static final int VK_F20 = 61447;
484 * The virtual key VK_F21.
486 * @since 1.2
488 public static final int VK_F21 = 61448;
491 * The virtual key VK_F22.
493 * @since 1.2
495 public static final int VK_F22 = 61449;
498 * The virtual key VK_F23.
500 * @since 1.2
502 public static final int VK_F23 = 61450;
505 * The virtual key VK_F24.
507 * @since 1.2
509 public static final int VK_F24 = 61451;
511 /** The virtual key VK_PRINTSCREEN. */
512 public static final int VK_PRINTSCREEN = 154;
514 /** The virtual key VK_INSERT. */
515 public static final int VK_INSERT = 155;
517 /** The virtual key VK_HELP. */
518 public static final int VK_HELP = 156;
520 /** The virtual key VK_META. */
521 public static final int VK_META = 157;
523 /** The virtual key VK_BACK_QUOTE. */
524 public static final int VK_BACK_QUOTE = 192;
526 /** The virtual key VK_QUOTE. */
527 public static final int VK_QUOTE = 222;
530 * The virtual key for the numpad VK_KP_UP.
532 * @see #VK_UP
533 * @since 1.2
535 public static final int VK_KP_UP = 224;
538 * The virtual key for the numpad VK_KP_DOWN.
540 * @see #VK_DOWN
541 * @since 1.2
543 public static final int VK_KP_DOWN = 225;
546 * The virtual key for the numpad VK_KP_LEFT.
548 * @see #VK_LEFT
549 * @since 1.2
551 public static final int VK_KP_LEFT = 226;
554 * The virtual key for the numpad VK_KP_RIGHT.
556 * @see #VK_RIGHT
557 * @since 1.2
559 public static final int VK_KP_RIGHT = 227;
562 * The virtual key VK_DEAD_GRAVE.
564 * @since 1.2
566 public static final int VK_DEAD_GRAVE = 128;
569 * The virtual key VK_DEAD_ACUTE.
571 * @since 1.2
573 public static final int VK_DEAD_ACUTE = 129;
576 * The virtual key VK_DEAD_CIRCUMFLEX.
578 * @since 1.2
580 public static final int VK_DEAD_CIRCUMFLEX = 130;
583 * The virtual key VK_DEAD_TILDE.
585 * @since 1.2
587 public static final int VK_DEAD_TILDE = 131;
590 * The virtual key VK_DEAD_MACRON.
592 * @since 1.2
594 public static final int VK_DEAD_MACRON = 132;
597 * The virtual key VK_DEAD_BREVE.
599 * @since 1.2
601 public static final int VK_DEAD_BREVE = 133;
604 * The virtual key VK_DEAD_ABOVEDOT.
606 * @since 1.2
608 public static final int VK_DEAD_ABOVEDOT = 134;
611 * The virtual key VK_DEAD_DIAERESIS.
613 * @since 1.2
615 public static final int VK_DEAD_DIAERESIS = 135;
618 * The virtual key VK_DEAD_ABOVERING.
620 * @since 1.2
622 public static final int VK_DEAD_ABOVERING = 136;
625 * The virtual key VK_DEAD_DOUBLEACUTE.
627 * @since 1.2
629 public static final int VK_DEAD_DOUBLEACUTE = 137;
632 * The virtual key VK_DEAD_CARON.
634 * @since 1.2
636 public static final int VK_DEAD_CARON = 138;
639 * The virtual key VK_DEAD_CEDILLA.
641 * @since 1.2
643 public static final int VK_DEAD_CEDILLA = 139;
646 * The virtual key VK_DEAD_OGONEK.
648 * @since 1.2
650 public static final int VK_DEAD_OGONEK = 140;
653 * The virtual key VK_DEAD_IOTA.
655 * @since 1.2
657 public static final int VK_DEAD_IOTA = 141;
660 * The virtual key VK_DEAD_VOICED_SOUND.
662 * @since 1.2
664 public static final int VK_DEAD_VOICED_SOUND = 142;
667 * The virtual key VK_DEAD_SEMIVOICED_SOUND.
669 * @since 1.2
671 public static final int VK_DEAD_SEMIVOICED_SOUND = 143;
674 * The virtual key VK_AMPERSAND.
676 * @since 1.2
678 public static final int VK_AMPERSAND = 150;
681 * The virtual key VK_ASTERISK.
683 * @since 1.2
685 public static final int VK_ASTERISK = 151;
688 * The virtual key VK_QUOTEDBL.
690 * @since 1.2
692 public static final int VK_QUOTEDBL = 152;
695 * The virtual key VK_LESS.
697 * @since 1.2
699 public static final int VK_LESS = 153;
702 * The virtual key VK_GREATER.
704 * @since 1.2
706 public static final int VK_GREATER = 160;
709 * The virtual key VK_BRACELEFT.
711 * @since 1.2
713 public static final int VK_BRACELEFT = 161;
716 * The virtual key VK_BRACERIGHT.
718 * @since 1.2
720 public static final int VK_BRACERIGHT = 162;
723 * The virtual key VK_AT.
725 * @since 1.2
727 public static final int VK_AT = 512;
730 * The virtual key VK_COLON.
732 * @since 1.2
734 public static final int VK_COLON = 513;
737 * The virtual key VK_CIRCUMFLEX.
739 * @since 1.2
741 public static final int VK_CIRCUMFLEX = 514;
744 * The virtual key VK_DOLLAR.
746 * @since 1.2
748 public static final int VK_DOLLAR = 515;
751 * The virtual key VK_EURO_SIGN.
753 * @since 1.2
755 public static final int VK_EURO_SIGN = 516;
758 * The virtual key VK_EXCLAMATION_MARK.
760 * @since 1.2
762 public static final int VK_EXCLAMATION_MARK = 517;
765 * The virtual key VK_INVERTED_EXCLAMATION_MARK.
767 * @since 1.2
769 public static final int VK_INVERTED_EXCLAMATION_MARK = 518;
772 * The virtual key VK_LEFT_PARENTHESIS.
774 * @since 1.2
776 public static final int VK_LEFT_PARENTHESIS = 519;
779 * The virtual key VK_NUMBER_SIGN.
781 * @since 1.2
783 public static final int VK_NUMBER_SIGN = 520;
786 * The virtual key VK_PLUS.
788 * @since 1.2
790 public static final int VK_PLUS = 521;
793 * The virtual key VK_RIGHT_PARENTHESIS.
795 * @since 1.2
797 public static final int VK_RIGHT_PARENTHESIS = 522;
800 * The virtual key VK_UNDERSCORE.
802 * @since 1.2
804 public static final int VK_UNDERSCORE = 523;
806 /** The virtual key VK_FINAL. */
807 public static final int VK_FINAL = 24;
809 /** The virtual key VK_CONVERT. */
810 public static final int VK_CONVERT = 28;
812 /** The virtual key VK_NONCONVERT. */
813 public static final int VK_NONCONVERT = 29;
815 /** The virtual key VK_ACCEPT. */
816 public static final int VK_ACCEPT = 30;
818 /** The virtual key VK_MODECHANGE. */
819 public static final int VK_MODECHANGE = 31;
821 /** The virtual key VK_KANA. */
822 public static final int VK_KANA = 21;
824 /** The virtual key VK_KANJI. */
825 public static final int VK_KANJI = 25;
828 * The virtual key VK_ALPHANUMERIC.
830 * @since 1.2
832 public static final int VK_ALPHANUMERIC = 240;
835 * The virtual key VK_KATAKANA.
837 * @since 1.2
839 public static final int VK_KATAKANA = 241;
842 * The virtual key VK_HIRAGANA.
844 * @since 1.2
846 public static final int VK_HIRAGANA = 242;
849 * The virtual key VK_FULL_WIDTH.
851 * @since 1.2
853 public static final int VK_FULL_WIDTH = 243;
856 * The virtual key VK_HALF_WIDTH.
858 * @since 1.2
860 public static final int VK_HALF_WIDTH = 244;
863 * The virtual key VK_ROMAN_CHARACTERS.
865 * @since 1.2
867 public static final int VK_ROMAN_CHARACTERS = 245;
870 * The virtual key VK_ALL_CANDIDATES.
872 * @since 1.2
874 public static final int VK_ALL_CANDIDATES = 256;
877 * The virtual key VK_PREVIOUS_CANDIDATE.
879 * @since 1.2
881 public static final int VK_PREVIOUS_CANDIDATE = 257;
884 * The virtual key VK_CODE_INPUT.
886 * @since 1.2
888 public static final int VK_CODE_INPUT = 258;
891 * The virtual key VK_JAPANESE_KATAKANA.
893 * @since 1.2
895 public static final int VK_JAPANESE_KATAKANA = 259;
898 * The virtual key VK_JAPANESE_HIRAGANA.
900 * @since 1.2
902 public static final int VK_JAPANESE_HIRAGANA = 260;
905 * The virtual key VK_JAPANESE_ROMAN.
907 * @since 1.2
909 public static final int VK_JAPANESE_ROMAN = 261;
912 * The virtual key VK_KANA_LOCK.
914 * @since 1.3
916 public static final int VK_KANA_LOCK = 262;
919 * The virtual key VK_INPUT_METHOD_ON_OFF.
921 * @since 1.3
923 public static final int VK_INPUT_METHOD_ON_OFF = 263;
926 * The virtual key VK_CUT.
928 * @since 1.2
930 public static final int VK_CUT = 65489;
933 * The virtual key VK_COPY.
935 * @since 1.2
937 public static final int VK_COPY = 65485;
940 * The virtual key VK_PASTE.
942 * @since 1.2
944 public static final int VK_PASTE = 65487;
947 * The virtual key VK_UNDO.
949 * @since 1.2
951 public static final int VK_UNDO = 65483;
954 * The virtual key VK_AGAIN.
956 * @since 1.2
958 public static final int VK_AGAIN = 65481;
961 * The virtual key VK_FIND.
963 * @since 1.2
965 public static final int VK_FIND = 65488;
968 * The virtual key VK_PROPS.
970 * @since 1.2
972 public static final int VK_PROPS = 65482;
975 * The virtual key VK_STOP.
977 * @since 1.2
979 public static final int VK_STOP = 65480;
982 * The virtual key VK_COMPOSE.
984 * @since 1.2
986 public static final int VK_COMPOSE = 65312;
989 * The virtual key VK_ALT_GRAPH.
991 * @since 1.2
993 public static final int VK_ALT_GRAPH = 65406;
996 * The virtual key VK_UNDEFINED. This is used for key typed events, which
997 * do not have a virtual key.
999 public static final int VK_UNDEFINED = 0;
1002 * The only char with no valid Unicode interpretation. This is used for
1003 * key pressed and key released events which do not have a valid keyChar.
1005 public static final char CHAR_UNDEFINED = '\uffff';
1008 * Indicates unknown or irrelavent key location. This is also used for
1009 * key typed events, which do not need a location.
1011 * @since 1.4
1013 public static final int KEY_LOCATION_UNKNOWN = 0;
1016 * Indicates a standard key location, with no left/right variants and not
1017 * on the numeric pad.
1019 * @since 1.4
1021 public static final int KEY_LOCATION_STANDARD = 1;
1024 * Indicates the key is on the left side of the keyboard, such as the left
1025 * shift.
1027 * @since 1.4
1029 public static final int KEY_LOCATION_LEFT = 2;
1032 * Indicates the key is on the right side of the keyboard, such as the right
1033 * shift.
1035 * @since 1.4
1037 public static final int KEY_LOCATION_RIGHT = 3;
1040 * Indicates the key is on the numeric pad, such as the numpad 0.
1042 * @since 1.4
1044 public static final int KEY_LOCATION_NUMPAD = 4;
1047 * The code assigned to the physical keyboard location (as adjusted by the
1048 * keyboard layout). Use the symbolic VK_* names instead of numbers.
1050 * @see #getKeyCode()
1051 * @serial the VK_ code for this key
1053 private int keyCode;
1056 * The Unicode character produced by the key type event. This has no meaning
1057 * for key pressed and key released events.
1059 * @see #getKeyChar()
1060 * @serial the Unicode value for this key
1062 private char keyChar;
1065 * The keyboard location of the key. One of {@link #KEY_LOCATION_UNKNOWN},
1066 * {@link #KEY_LOCATION_STANDARD}, {@link #KEY_LOCATION_LEFT},
1067 * {@link #KEY_LOCATION_RIGHT}, or {@link #KEY_LOCATION_NUMPAD}.
1069 * @see #getKeyLocation()
1070 * @serial the key location
1071 * @since 1.4
1073 private final int keyLocation;
1076 * Stores the state of the native event dispatching system, to correctly
1077 * dispatch in Component#dispatchEventImpl when a proxy is active.
1079 * XXX Does this matter in Classpath?
1081 * @serial whether the proxy is active
1083 private boolean isProxyActive;
1087 * Initializes a new instance of <code>KeyEvent</code> with the specified
1088 * information. Note that an invalid id leads to unspecified results.
1090 * @param source the component that generated this event
1091 * @param id the event id
1092 * @param when the timestamp when the even occurred
1093 * @param modifiers the modifier keys during the event, in old or new style
1094 * @param keyCode the integer constant for the virtual key type
1095 * @param keyChar the Unicode value of the key
1096 * @param keyLocation the location of the key
1097 * @throws IllegalArgumentException if source is null, if keyLocation is
1098 * invalid, or if (id == KEY_TYPED && (keyCode != VK_UNDEFINED
1099 * || keyChar == CHAR_UNDEFINED))
1101 public KeyEvent(Component source, int id, long when, int modifiers,
1102 int keyCode, char keyChar, int keyLocation)
1104 super(source, id, when, modifiers);
1105 this.keyCode = keyCode;
1106 this.keyChar = keyChar;
1107 this.keyLocation = keyLocation;
1108 if ((id == KEY_TYPED && (keyCode != VK_UNDEFINED
1109 || keyChar == CHAR_UNDEFINED))
1110 || keyLocation < KEY_LOCATION_UNKNOWN
1111 || keyLocation > KEY_LOCATION_NUMPAD)
1112 throw new IllegalArgumentException();
1116 * Initializes a new instance of <code>KeyEvent</code> with the specified
1117 * information. Note that an invalid id leads to unspecified results.
1119 * @param source the component that generated this event
1120 * @param id the event id
1121 * @param when the timestamp when the even occurred
1122 * @param modifiers the modifier keys during the event, in old or new style
1123 * @param keyCode the integer constant for the virtual key type
1124 * @param keyChar the Unicode value of the key
1125 * @throws IllegalArgumentException if source is null, or if
1126 * (id == KEY_TYPED && (keyCode != VK_UNDEFINED
1127 * || keyChar == CHAR_UNDEFINED))
1129 public KeyEvent(Component source, int id, long when, int modifiers,
1130 int keyCode, char keyChar)
1132 this(source, id, when, modifiers, keyCode, keyChar, KEY_LOCATION_UNKNOWN);
1136 * Initializes a new instance of <code>KeyEvent</code> with the specified
1137 * information. Note that an invalid id leads to unspecified results.
1139 * @param source the component that generated this event
1140 * @param id the event id
1141 * @param when the timestamp when the even occurred
1142 * @param modifiers the modifier keys during the event, in old or new style
1143 * @param keyCode the integer constant for the virtual key type
1144 * @throws IllegalArgumentException if source is null, or if
1145 * id == KEY_TYPED but keyCode != VK_UNDEFINED
1147 * @deprecated
1149 public KeyEvent(Component source, int id, long when, int modifiers,
1150 int keyCode)
1152 this(source, id, when, modifiers, keyCode, '\0', KEY_LOCATION_UNKNOWN);
1156 * Returns the key code for the event key. This will be one of the
1157 * <code>VK_*</code> constants defined in this class. If the event type is
1158 * KEY_TYPED, the result will be VK_UNDEFINED.
1160 * @return the key code for this event
1162 public int getKeyCode()
1164 return keyCode;
1168 * Sets the key code for this event. This must be one of the
1169 * <code>VK_*</code> constants defined in this class.
1171 * @param keyCode the new key code for this event
1173 public void setKeyCode(int keyCode)
1175 this.keyCode = keyCode;
1179 * Returns the Unicode value for the event key. This will be
1180 * <code>CHAR_UNDEFINED</code> if there is no Unicode equivalent for
1181 * this key, usually when this is a KEY_PRESSED or KEY_RELEASED event.
1183 * @return the Unicode character for this event
1185 public char getKeyChar()
1187 return keyChar;
1191 * Sets the Unicode character for this event to the specified value.
1193 * @param keyChar the new Unicode character for this event
1195 public void setKeyChar(char keyChar)
1197 this.keyChar = keyChar;
1201 * Sets the modifier keys to the specified value. This should be a union
1202 * of the bit mask constants from <code>InputEvent</code>. The use of this
1203 * method is not recommended, particularly for KEY_TYPED events, which do
1204 * not check if the modifiers were changed.
1206 * @param modifiers the new modifier value, in either old or new style
1207 * @see InputEvent
1209 * @deprecated
1211 public void setModifiers(int modifiers)
1213 this.modifiers = EventModifier.extend(modifiers);
1217 * Returns the keyboard location of the key that generated this event. This
1218 * provides a way to distinguish between keys like left and right shift
1219 * which share a common key code. The result will be one of
1220 * {@link #KEY_LOCATION_UNKNOWN}, {@link #KEY_LOCATION_STANDARD},
1221 * {@link #KEY_LOCATION_LEFT}, {@link #KEY_LOCATION_RIGHT}, or
1222 * {@link #KEY_LOCATION_NUMPAD}.
1224 * @return the key location
1225 * @since 1.4
1227 public int getKeyLocation()
1229 return keyLocation;
1233 * Returns the text name of key code, such as "HOME", "F1", or "A".
1235 * XXX Sun claims this can be localized via the awt.properties file - how
1236 * do we implement that?
1238 * @return the text name of the key code
1240 public static String getKeyText(int keyCode)
1242 switch (keyCode)
1244 case VK_CANCEL:
1245 return "Cancel";
1246 case VK_BACK_SPACE:
1247 return "Backspace";
1248 case VK_TAB:
1249 return "Tab";
1250 case VK_ENTER:
1251 return "Enter";
1252 case VK_CLEAR:
1253 return "Clear";
1254 case VK_SHIFT:
1255 return "Shift";
1256 case VK_CONTROL:
1257 return "Ctrl";
1258 case VK_ALT:
1259 return "Alt";
1260 case VK_PAUSE:
1261 return "Pause";
1262 case VK_CAPS_LOCK:
1263 return "Caps Lock";
1264 case VK_KANA:
1265 return "Kana";
1266 case VK_FINAL:
1267 return "Final";
1268 case VK_KANJI:
1269 return "Kanji";
1270 case VK_ESCAPE:
1271 return "Escape";
1272 case VK_CONVERT:
1273 return "Convert";
1274 case VK_NONCONVERT:
1275 return "No Convert";
1276 case VK_ACCEPT:
1277 return "Accept";
1278 case VK_MODECHANGE:
1279 return "Mode Change";
1280 case VK_SPACE:
1281 return "Space";
1282 case VK_PAGE_UP:
1283 return "Page Up";
1284 case VK_PAGE_DOWN:
1285 return "Page Down";
1286 case VK_END:
1287 return "End";
1288 case VK_HOME:
1289 return "Home";
1290 case VK_LEFT:
1291 case VK_KP_LEFT:
1292 return "Left";
1293 case VK_UP:
1294 case VK_KP_UP:
1295 return "Up";
1296 case VK_RIGHT:
1297 case VK_KP_RIGHT:
1298 return "Right";
1299 case VK_DOWN:
1300 case VK_KP_DOWN:
1301 return "Down";
1302 case VK_MINUS:
1303 return "Minus";
1304 case VK_MULTIPLY:
1305 return "NumPad *";
1306 case VK_ADD:
1307 return "NumPad +";
1308 case VK_SEPARATOR:
1309 return "NumPad ,";
1310 case VK_SUBTRACT:
1311 return "NumPad -";
1312 case VK_DECIMAL:
1313 return "NumPad .";
1314 case VK_DIVIDE:
1315 return "NumPad /";
1316 case VK_DELETE:
1317 return "Delete";
1318 case VK_DEAD_GRAVE:
1319 return "Dead Grave";
1320 case VK_DEAD_ACUTE:
1321 return "Dead Acute";
1322 case VK_DEAD_CIRCUMFLEX:
1323 return "Dead Circumflex";
1324 case VK_DEAD_TILDE:
1325 return "Dead Tilde";
1326 case VK_DEAD_MACRON:
1327 return "Dead Macron";
1328 case VK_DEAD_BREVE:
1329 return "Dead Breve";
1330 case VK_DEAD_ABOVEDOT:
1331 return "Dead Above Dot";
1332 case VK_DEAD_DIAERESIS:
1333 return "Dead Diaeresis";
1334 case VK_DEAD_ABOVERING:
1335 return "Dead Above Ring";
1336 case VK_DEAD_DOUBLEACUTE:
1337 return "Dead Double Acute";
1338 case VK_DEAD_CARON:
1339 return "Dead Caron";
1340 case VK_DEAD_CEDILLA:
1341 return "Dead Cedilla";
1342 case VK_DEAD_OGONEK:
1343 return "Dead Ogonek";
1344 case VK_DEAD_IOTA:
1345 return "Dead Iota";
1346 case VK_DEAD_VOICED_SOUND:
1347 return "Dead Voiced Sound";
1348 case VK_DEAD_SEMIVOICED_SOUND:
1349 return "Dead Semivoiced Sound";
1350 case VK_NUM_LOCK:
1351 return "Num Lock";
1352 case VK_SCROLL_LOCK:
1353 return "Scroll Lock";
1354 case VK_AMPERSAND:
1355 return "Ampersand";
1356 case VK_ASTERISK:
1357 return "Asterisk";
1358 case VK_QUOTEDBL:
1359 return "Double Quote";
1360 case VK_LESS:
1361 return "Less";
1362 case VK_PRINTSCREEN:
1363 return "Print Screen";
1364 case VK_INSERT:
1365 return "Insert";
1366 case VK_HELP:
1367 return "Help";
1368 case VK_META:
1369 return "Meta";
1370 case VK_GREATER:
1371 return "Greater";
1372 case VK_BRACELEFT:
1373 return "Left Brace";
1374 case VK_BRACERIGHT:
1375 return "Right Brace";
1376 case VK_BACK_QUOTE:
1377 return "Back Quote";
1378 case VK_QUOTE:
1379 return "Quote";
1380 case VK_ALPHANUMERIC:
1381 return "Alphanumeric";
1382 case VK_KATAKANA:
1383 return "Katakana";
1384 case VK_HIRAGANA:
1385 return "Hiragana";
1386 case VK_FULL_WIDTH:
1387 return "Full-Width";
1388 case VK_HALF_WIDTH:
1389 return "Half-Width";
1390 case VK_ROMAN_CHARACTERS:
1391 return "Roman Characters";
1392 case VK_ALL_CANDIDATES:
1393 return "All Candidates";
1394 case VK_PREVIOUS_CANDIDATE:
1395 return "Previous Candidate";
1396 case VK_CODE_INPUT:
1397 return "Code Input";
1398 case VK_JAPANESE_KATAKANA:
1399 return "Japanese Katakana";
1400 case VK_JAPANESE_HIRAGANA:
1401 return "Japanese Hiragana";
1402 case VK_JAPANESE_ROMAN:
1403 return "Japanese Roman";
1404 case VK_KANA_LOCK:
1405 return "Kana Lock";
1406 case VK_INPUT_METHOD_ON_OFF:
1407 return "Input Method On/Off";
1408 case VK_AT:
1409 return "At";
1410 case VK_COLON:
1411 return "Colon";
1412 case VK_CIRCUMFLEX:
1413 return "Circumflex";
1414 case VK_DOLLAR:
1415 return "Dollar";
1416 case VK_EURO_SIGN:
1417 return "Euro";
1418 case VK_EXCLAMATION_MARK:
1419 return "Exclamation Mark";
1420 case VK_INVERTED_EXCLAMATION_MARK:
1421 return "Inverted Exclamation Mark";
1422 case VK_LEFT_PARENTHESIS:
1423 return "Left Parenthesis";
1424 case VK_NUMBER_SIGN:
1425 return "Number Sign";
1426 case VK_PLUS:
1427 return "Plus";
1428 case VK_RIGHT_PARENTHESIS:
1429 return "Right Parenthesis";
1430 case VK_UNDERSCORE:
1431 return "Underscore";
1432 case VK_COMPOSE:
1433 return "Compose";
1434 case VK_ALT_GRAPH:
1435 return "Alt Graph";
1436 case VK_STOP:
1437 return "Stop";
1438 case VK_AGAIN:
1439 return "Again";
1440 case VK_PROPS:
1441 return "Props";
1442 case VK_UNDO:
1443 return "Undo";
1444 case VK_COPY:
1445 return "Copy";
1446 case VK_PASTE:
1447 return "Paste";
1448 case VK_FIND:
1449 return "Find";
1450 case VK_CUT:
1451 return "Cut";
1452 case VK_COMMA:
1453 case VK_PERIOD:
1454 case VK_SLASH:
1455 case VK_0:
1456 case VK_1:
1457 case VK_2:
1458 case VK_3:
1459 case VK_4:
1460 case VK_5:
1461 case VK_6:
1462 case VK_7:
1463 case VK_8:
1464 case VK_9:
1465 case VK_SEMICOLON:
1466 case VK_EQUALS:
1467 case VK_A:
1468 case VK_B:
1469 case VK_C:
1470 case VK_D:
1471 case VK_E:
1472 case VK_F:
1473 case VK_G:
1474 case VK_H:
1475 case VK_I:
1476 case VK_J:
1477 case VK_K:
1478 case VK_L:
1479 case VK_M:
1480 case VK_N:
1481 case VK_O:
1482 case VK_P:
1483 case VK_Q:
1484 case VK_R:
1485 case VK_S:
1486 case VK_T:
1487 case VK_U:
1488 case VK_V:
1489 case VK_W:
1490 case VK_X:
1491 case VK_Y:
1492 case VK_Z:
1493 case VK_OPEN_BRACKET:
1494 case VK_BACK_SLASH:
1495 case VK_CLOSE_BRACKET:
1496 return "" + (char) keyCode;
1497 case VK_NUMPAD0:
1498 case VK_NUMPAD1:
1499 case VK_NUMPAD2:
1500 case VK_NUMPAD3:
1501 case VK_NUMPAD4:
1502 case VK_NUMPAD5:
1503 case VK_NUMPAD6:
1504 case VK_NUMPAD7:
1505 case VK_NUMPAD8:
1506 case VK_NUMPAD9:
1507 return "NumPad-" + (keyCode - VK_NUMPAD0);
1508 case VK_F1:
1509 case VK_F2:
1510 case VK_F3:
1511 case VK_F4:
1512 case VK_F5:
1513 case VK_F6:
1514 case VK_F7:
1515 case VK_F8:
1516 case VK_F9:
1517 case VK_F10:
1518 case VK_F11:
1519 case VK_F12:
1520 return "F" + (keyCode - (VK_F1 - 1));
1521 case VK_F13:
1522 case VK_F14:
1523 case VK_F15:
1524 case VK_F16:
1525 case VK_F17:
1526 case VK_F18:
1527 case VK_F19:
1528 case VK_F20:
1529 case VK_F21:
1530 case VK_F22:
1531 case VK_F23:
1532 case VK_F24:
1533 return "F" + (keyCode - (VK_F13 - 13));
1534 default:
1535 // This is funky on negative numbers, but that's Sun's fault.
1536 return "Unknown keyCode: 0x" + (keyCode < 0 ? "-" : "")
1537 + Integer.toHexString(Math.abs(keyCode));
1542 * Returns a string describing the modifiers, such as "Shift" or
1543 * "Ctrl+Button1".
1545 * XXX Sun claims this can be localized via the awt.properties file - how
1546 * do we implement that?
1548 * @param modifiers the old-style modifiers to convert to text
1549 * @return a string representation of the modifiers in this bitmask
1551 public static String getKeyModifiersText(int modifiers)
1553 return getModifiersExText(EventModifier.extend(modifiers
1554 & EventModifier.OLD_MASK));
1558 * Tests whether or not this key is an action key. An action key typically
1559 * does not fire a KEY_TYPED event, and is not a modifier.
1561 * @return true if this is an action key
1563 public boolean isActionKey()
1565 switch (keyCode)
1567 case VK_PAUSE:
1568 case VK_CAPS_LOCK:
1569 case VK_KANA:
1570 case VK_FINAL:
1571 case VK_KANJI:
1572 case VK_CONVERT:
1573 case VK_NONCONVERT:
1574 case VK_ACCEPT:
1575 case VK_MODECHANGE:
1576 case VK_PAGE_UP:
1577 case VK_PAGE_DOWN:
1578 case VK_END:
1579 case VK_HOME:
1580 case VK_LEFT:
1581 case VK_UP:
1582 case VK_RIGHT:
1583 case VK_DOWN:
1584 case VK_F1:
1585 case VK_F2:
1586 case VK_F3:
1587 case VK_F4:
1588 case VK_F5:
1589 case VK_F6:
1590 case VK_F7:
1591 case VK_F8:
1592 case VK_F9:
1593 case VK_F10:
1594 case VK_F11:
1595 case VK_F12:
1596 case VK_NUM_LOCK:
1597 case VK_SCROLL_LOCK:
1598 case VK_PRINTSCREEN:
1599 case VK_INSERT:
1600 case VK_HELP:
1601 case VK_KP_UP:
1602 case VK_KP_DOWN:
1603 case VK_KP_LEFT:
1604 case VK_KP_RIGHT:
1605 case VK_ALPHANUMERIC:
1606 case VK_KATAKANA:
1607 case VK_HIRAGANA:
1608 case VK_FULL_WIDTH:
1609 case VK_HALF_WIDTH:
1610 case VK_ROMAN_CHARACTERS:
1611 case VK_ALL_CANDIDATES:
1612 case VK_PREVIOUS_CANDIDATE:
1613 case VK_CODE_INPUT:
1614 case VK_JAPANESE_KATAKANA:
1615 case VK_JAPANESE_HIRAGANA:
1616 case VK_JAPANESE_ROMAN:
1617 case VK_KANA_LOCK:
1618 case VK_INPUT_METHOD_ON_OFF:
1619 case VK_F13:
1620 case VK_F14:
1621 case VK_F15:
1622 case VK_F16:
1623 case VK_F17:
1624 case VK_F18:
1625 case VK_F19:
1626 case VK_F20:
1627 case VK_F21:
1628 case VK_F22:
1629 case VK_F23:
1630 case VK_F24:
1631 case VK_STOP:
1632 case VK_AGAIN:
1633 case VK_PROPS:
1634 case VK_UNDO:
1635 case VK_COPY:
1636 case VK_PASTE:
1637 case VK_FIND:
1638 case VK_CUT:
1639 return true;
1640 default:
1641 return false;
1646 * Returns a string identifying the event. This is formatted as the
1647 * field name of the id type, followed by the keyCode, then the
1648 * keyChar, modifiers (if any), extModifiers (if any), and
1649 * keyLocation.
1651 * @return a string identifying the event
1653 public String paramString()
1655 StringBuffer s = new StringBuffer();
1657 switch (id)
1659 case KEY_PRESSED:
1660 s.append("KEY_PRESSED");
1661 break;
1662 case KEY_RELEASED:
1663 s.append("KEY_RELEASED");
1664 break;
1665 case KEY_TYPED:
1666 s.append("KEY_TYPED");
1667 break;
1668 default:
1669 s.append("unknown type");
1672 s.append(",keyCode=").append(keyCode);
1674 s.append(",keyText=").append(getKeyText(keyCode));
1676 s.append(",keyChar=");
1677 if (isActionKey()
1678 || keyCode == VK_SHIFT
1679 || keyCode == VK_CONTROL
1680 || keyCode == VK_ALT)
1681 s.append("Undefined keyChar");
1682 else
1684 /* This output string must be selected by examining keyChar
1685 * rather than keyCode, because key code information is not
1686 * included in KEY_TYPED events.
1688 if (keyChar == VK_BACK_SPACE
1689 || keyChar == VK_TAB
1690 || keyChar == VK_ENTER
1691 || keyChar == VK_ESCAPE
1692 || keyChar == VK_DELETE)
1693 s.append(getKeyText(keyChar));
1694 else
1695 s.append("'").append(keyChar).append("'");
1698 if ((modifiers & CONVERT_MASK) != 0)
1699 s.append(",modifiers=").append(getModifiersExText(modifiers
1700 & CONVERT_MASK));
1701 if (modifiers != 0)
1702 s.append(",extModifiers=").append(getModifiersExText(modifiers));
1704 s.append(",keyLocation=KEY_LOCATION_");
1705 switch (keyLocation)
1707 case KEY_LOCATION_UNKNOWN:
1708 s.append("UNKNOWN");
1709 break;
1710 case KEY_LOCATION_STANDARD:
1711 s.append("STANDARD");
1712 break;
1713 case KEY_LOCATION_LEFT:
1714 s.append("LEFT");
1715 break;
1716 case KEY_LOCATION_RIGHT:
1717 s.append("RIGHT");
1718 break;
1719 case KEY_LOCATION_NUMPAD:
1720 s.append("NUMPAD");
1723 return s.toString();
1727 * Reads in the object from a serial stream.
1729 * @param s the stream to read from
1730 * @throws IOException if deserialization fails
1731 * @throws ClassNotFoundException if deserialization fails
1732 * @serialData default, except that the modifiers are converted to new style
1734 private void readObject(ObjectInputStream s)
1735 throws IOException, ClassNotFoundException
1737 s.defaultReadObject();
1738 modifiers = EventModifier.extend(modifiers);
1740 } // class KeyEvent