1. /*
  2. * @(#)KeyEvent.java 1.39 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.event;
  8. import java.awt.Event;
  9. import java.awt.Component;
  10. import java.awt.Toolkit;
  11. /**
  12. * An event which indicates that a keystroke occurred in a component.
  13. * <P>
  14. * This low-level event is generated by a component object (such as a text
  15. * field) when a key is pressed, released, or typed.
  16. * The event is passed to every <code>KeyListener</code>
  17. * or <code>KeyAdapter</code> object which registered to receive such
  18. * events using the component's <code>addKeyListener</code> method.
  19. * (<code>KeyAdapter</code> objects implement the
  20. * <code>KeyListener</code> interface.) Each such listener object
  21. * gets this <code>KeyEvent</code> when the event occurs.
  22. *
  23. * <p>
  24. * <em>"Key typed" events</em> are higher-level and generally do not depend on
  25. * the platform or keyboard layout. They are generated when a character is
  26. * entered, and are the preferred way to find out about character input.
  27. * In the simplest case, a key typed event is produced by a single key press
  28. * (e.g., 'a'). Often, however, characters are produced by series of key
  29. * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to
  30. * key typed events may be many-to-one or many-to-many. Key releases are not
  31. * usually necessary to generate a key typed event, but there are some cases
  32. * where the key typed event is not generated until a key is released (e.g.,
  33. * entering ASCII sequences via the Alt-Numpad method in Windows).
  34. * No key typed events are generated for keys that don't generate characters
  35. * (e.g., action keys, modifier keys, etc.).
  36. * The getKeyChar method always returns a valid Unicode character or
  37. * CHAR_UNDEFINED.
  38. * For key pressed and key released events, the getKeyCode method returns
  39. * the event's keyCode. For key typed events, the getKeyCode method
  40. * always returns VK_UNDEFINED.
  41. *
  42. * <p>
  43. * <em>"Key pressed" and "key released" events</em> are lower-level and depend
  44. * on the platform and keyboard layout. They are generated whenever a key is
  45. * pressed or released, and are the only way to find out about keys that don't
  46. * generate character input (e.g., action keys, modifier keys, etc.). The key
  47. * being pressed or released is indicated by the getKeyCode method, which returns
  48. * a virtual key code.
  49. *
  50. * <p>
  51. * <em>Virtual key codes</em> are used to report which keyboard key has
  52. * been pressed, rather than a character generated by the combination
  53. * of one or more keystrokes (like "A", which comes from shift and "a").
  54. *
  55. * <P>
  56. * For example, pressing the Shift key will cause a KEY_PRESSED event
  57. * with a VK_SHIFT keyCode, while pressing the 'a' key will result in
  58. * a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event
  59. * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
  60. * value of 'A' is generated.
  61. *
  62. * <P>
  63. * Notes:
  64. * <ul>
  65. * <li>Key combinations which do not result in characters, such as action keys
  66. * like F1 and the HELP key, do not generate KEY_TYPED events.
  67. * <li>Not all keyboards or systems are capable of generating all
  68. * virtual key codes. No attempt is made in Java to artificially
  69. * generate these keys.
  70. * <li>Virtual key codes do not identify a physical key, they depend on the
  71. * platform and keyboard layout. For
  72. * example, the key that on a Windows U.S. keyboard layout generates VK_Q,
  73. * generates VK_A on a Windows French keyboard layout.
  74. * <li>In order to support the platform-independent handling of action keys,
  75. * the Java platform uses a few additional virtual key constants for functions
  76. * that would otherwise have to be recognized by interpreting virtual key codes
  77. * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
  78. * is returned instead of VK_CONVERT with the ALT modifer.
  79. * </ul>
  80. *
  81. * <P>
  82. * WARNING: Aside from those keys that are defined by the Java language
  83. * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
  84. * constants. Sun reserves the right to change these values as needed
  85. * to accomodate a wider range of keyboards in the future.
  86. *
  87. * @see KeyAdapter
  88. * @see KeyListener
  89. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/keylistener.html">Tutorial: Writing a Key Listener</a>
  90. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  91. *
  92. * @version 1.39 11/29/01
  93. * @author Carl Quinn
  94. * @author Amy Fowler
  95. * @author Norbert Lindenberg
  96. */
  97. public class KeyEvent extends InputEvent {
  98. /**
  99. * The first number in the range of ids used for key events.
  100. */
  101. public static final int KEY_FIRST = 400;
  102. /**
  103. * The last number in the range of ids used for key events.
  104. */
  105. public static final int KEY_LAST = 402;
  106. /**
  107. * The "key typed" event. This event is generated when a character is
  108. * entered. In the simplest case, it is produced by a single key press.
  109. * Often, however, characters are produced by series of key presses, and
  110. * the mapping from key pressed events to key typed events may be
  111. * many-to-one or many-to-many.
  112. */
  113. public static final int KEY_TYPED = KEY_FIRST;
  114. /**
  115. * The "key pressed" event. This event is generated when a key
  116. * is pushed down.
  117. */
  118. public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
  119. /**
  120. * The "key released" event. This event is generated when a key
  121. * is let up.
  122. */
  123. public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
  124. /* Virtual key codes. */
  125. public static final int VK_ENTER = '\n';
  126. public static final int VK_BACK_SPACE = '\b';
  127. public static final int VK_TAB = '\t';
  128. public static final int VK_CANCEL = 0x03;
  129. public static final int VK_CLEAR = 0x0C;
  130. public static final int VK_SHIFT = 0x10;
  131. public static final int VK_CONTROL = 0x11;
  132. public static final int VK_ALT = 0x12;
  133. public static final int VK_PAUSE = 0x13;
  134. public static final int VK_CAPS_LOCK = 0x14;
  135. public static final int VK_ESCAPE = 0x1B;
  136. public static final int VK_SPACE = 0x20;
  137. public static final int VK_PAGE_UP = 0x21;
  138. public static final int VK_PAGE_DOWN = 0x22;
  139. public static final int VK_END = 0x23;
  140. public static final int VK_HOME = 0x24;
  141. public static final int VK_LEFT = 0x25;
  142. public static final int VK_UP = 0x26;
  143. public static final int VK_RIGHT = 0x27;
  144. public static final int VK_DOWN = 0x28;
  145. public static final int VK_COMMA = 0x2C;
  146. public static final int VK_MINUS = 0x2D;
  147. public static final int VK_PERIOD = 0x2E;
  148. public static final int VK_SLASH = 0x2F;
  149. /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
  150. public static final int VK_0 = 0x30;
  151. public static final int VK_1 = 0x31;
  152. public static final int VK_2 = 0x32;
  153. public static final int VK_3 = 0x33;
  154. public static final int VK_4 = 0x34;
  155. public static final int VK_5 = 0x35;
  156. public static final int VK_6 = 0x36;
  157. public static final int VK_7 = 0x37;
  158. public static final int VK_8 = 0x38;
  159. public static final int VK_9 = 0x39;
  160. public static final int VK_SEMICOLON = 0x3B;
  161. public static final int VK_EQUALS = 0x3D;
  162. /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
  163. public static final int VK_A = 0x41;
  164. public static final int VK_B = 0x42;
  165. public static final int VK_C = 0x43;
  166. public static final int VK_D = 0x44;
  167. public static final int VK_E = 0x45;
  168. public static final int VK_F = 0x46;
  169. public static final int VK_G = 0x47;
  170. public static final int VK_H = 0x48;
  171. public static final int VK_I = 0x49;
  172. public static final int VK_J = 0x4A;
  173. public static final int VK_K = 0x4B;
  174. public static final int VK_L = 0x4C;
  175. public static final int VK_M = 0x4D;
  176. public static final int VK_N = 0x4E;
  177. public static final int VK_O = 0x4F;
  178. public static final int VK_P = 0x50;
  179. public static final int VK_Q = 0x51;
  180. public static final int VK_R = 0x52;
  181. public static final int VK_S = 0x53;
  182. public static final int VK_T = 0x54;
  183. public static final int VK_U = 0x55;
  184. public static final int VK_V = 0x56;
  185. public static final int VK_W = 0x57;
  186. public static final int VK_X = 0x58;
  187. public static final int VK_Y = 0x59;
  188. public static final int VK_Z = 0x5A;
  189. public static final int VK_OPEN_BRACKET = 0x5B;
  190. public static final int VK_BACK_SLASH = 0x5C;
  191. public static final int VK_CLOSE_BRACKET = 0x5D;
  192. public static final int VK_NUMPAD0 = 0x60;
  193. public static final int VK_NUMPAD1 = 0x61;
  194. public static final int VK_NUMPAD2 = 0x62;
  195. public static final int VK_NUMPAD3 = 0x63;
  196. public static final int VK_NUMPAD4 = 0x64;
  197. public static final int VK_NUMPAD5 = 0x65;
  198. public static final int VK_NUMPAD6 = 0x66;
  199. public static final int VK_NUMPAD7 = 0x67;
  200. public static final int VK_NUMPAD8 = 0x68;
  201. public static final int VK_NUMPAD9 = 0x69;
  202. public static final int VK_MULTIPLY = 0x6A;
  203. public static final int VK_ADD = 0x6B;
  204. public static final int VK_SEPARATER = 0x6C;
  205. public static final int VK_SUBTRACT = 0x6D;
  206. public static final int VK_DECIMAL = 0x6E;
  207. public static final int VK_DIVIDE = 0x6F;
  208. public static final int VK_DELETE = 0x7F; /* ASCII DEL */
  209. public static final int VK_NUM_LOCK = 0x90;
  210. public static final int VK_SCROLL_LOCK = 0x91;
  211. /** Constant for the F1 function key. */
  212. public static final int VK_F1 = 0x70;
  213. /** Constant for the F2 function key. */
  214. public static final int VK_F2 = 0x71;
  215. /** Constant for the F3 function key. */
  216. public static final int VK_F3 = 0x72;
  217. /** Constant for the F4 function key. */
  218. public static final int VK_F4 = 0x73;
  219. /** Constant for the F5 function key. */
  220. public static final int VK_F5 = 0x74;
  221. /** Constant for the F6 function key. */
  222. public static final int VK_F6 = 0x75;
  223. /** Constant for the F7 function key. */
  224. public static final int VK_F7 = 0x76;
  225. /** Constant for the F8 function key. */
  226. public static final int VK_F8 = 0x77;
  227. /** Constant for the F9 function key. */
  228. public static final int VK_F9 = 0x78;
  229. /** Constant for the F10 function key. */
  230. public static final int VK_F10 = 0x79;
  231. /** Constant for the F11 function key. */
  232. public static final int VK_F11 = 0x7A;
  233. /** Constant for the F12 function key. */
  234. public static final int VK_F12 = 0x7B;
  235. /** Constant for the F13 function key. */
  236. /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
  237. public static final int VK_F13 = 0xF000;
  238. /** Constant for the F14 function key. */
  239. public static final int VK_F14 = 0xF001;
  240. /** Constant for the F15 function key. */
  241. public static final int VK_F15 = 0xF002;
  242. /** Constant for the F16 function key. */
  243. public static final int VK_F16 = 0xF003;
  244. /** Constant for the F17 function key. */
  245. public static final int VK_F17 = 0xF004;
  246. /** Constant for the F18 function key. */
  247. public static final int VK_F18 = 0xF005;
  248. /** Constant for the F19 function key. */
  249. public static final int VK_F19 = 0xF006;
  250. /** Constant for the F20 function key. */
  251. public static final int VK_F20 = 0xF007;
  252. /** Constant for the F21 function key. */
  253. public static final int VK_F21 = 0xF008;
  254. /** Constant for the F22 function key. */
  255. public static final int VK_F22 = 0xF009;
  256. /** Constant for the F23 function key. */
  257. public static final int VK_F23 = 0xF00A;
  258. /** Constant for the F24 function key. */
  259. public static final int VK_F24 = 0xF00B;
  260. public static final int VK_PRINTSCREEN = 0x9A;
  261. public static final int VK_INSERT = 0x9B;
  262. public static final int VK_HELP = 0x9C;
  263. public static final int VK_META = 0x9D;
  264. public static final int VK_BACK_QUOTE = 0xC0;
  265. public static final int VK_QUOTE = 0xDE;
  266. /** for KeyPad cursor arrow keys */
  267. public static final int VK_KP_UP = 0xE0;
  268. public static final int VK_KP_DOWN = 0xE1;
  269. public static final int VK_KP_LEFT = 0xE2;
  270. public static final int VK_KP_RIGHT = 0xE3;
  271. /* For European keyboards */
  272. public static final int VK_DEAD_GRAVE = 0x80;
  273. public static final int VK_DEAD_ACUTE = 0x81;
  274. public static final int VK_DEAD_CIRCUMFLEX = 0x82;
  275. public static final int VK_DEAD_TILDE = 0x83;
  276. public static final int VK_DEAD_MACRON = 0x84;
  277. public static final int VK_DEAD_BREVE = 0x85;
  278. public static final int VK_DEAD_ABOVEDOT = 0x86;
  279. public static final int VK_DEAD_DIAERESIS = 0x87;
  280. public static final int VK_DEAD_ABOVERING = 0x88;
  281. public static final int VK_DEAD_DOUBLEACUTE = 0x89;
  282. public static final int VK_DEAD_CARON = 0x8a;
  283. public static final int VK_DEAD_CEDILLA = 0x8b;
  284. public static final int VK_DEAD_OGONEK = 0x8c;
  285. public static final int VK_DEAD_IOTA = 0x8d;
  286. public static final int VK_DEAD_VOICED_SOUND = 0x8e;
  287. public static final int VK_DEAD_SEMIVOICED_SOUND = 0x8f;
  288. public static final int VK_AMPERSAND = 0x96;
  289. public static final int VK_ASTERISK = 0x97;
  290. public static final int VK_QUOTEDBL = 0x98;
  291. public static final int VK_LESS = 0x99;
  292. public static final int VK_GREATER = 0xa0;
  293. public static final int VK_BRACELEFT = 0xa1;
  294. public static final int VK_BRACERIGHT = 0xa2;
  295. /** Constant for the "@" key. */
  296. public static final int VK_AT = 0x0200;
  297. /** Constant for the ":" key. */
  298. public static final int VK_COLON = 0x0201;
  299. /** Constant for the "^" key. */
  300. public static final int VK_CIRCUMFLEX = 0x0202;
  301. /** Constant for the "$" key. */
  302. public static final int VK_DOLLAR = 0x0203;
  303. /** Constant for the Euro currency sign key. */
  304. public static final int VK_EURO_SIGN = 0x0204;
  305. /** Constant for the "!" key. */
  306. public static final int VK_EXCLAMATION_MARK = 0x0205;
  307. /** Constant for the inverted exclamation mark key. */
  308. public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
  309. /** Constant for the "(" key. */
  310. public static final int VK_LEFT_PARENTHESIS = 0x0207;
  311. /** Constant for the "#" key. */
  312. public static final int VK_NUMBER_SIGN = 0x0208;
  313. /** Constant for the "+" key. */
  314. public static final int VK_PLUS = 0x0209;
  315. /** Constant for the ")" key. */
  316. public static final int VK_RIGHT_PARENTHESIS = 0x020A;
  317. /** Constant for the "_" key. */
  318. public static final int VK_UNDERSCORE = 0x020B;
  319. /* for input method support on Asian Keyboards */
  320. public static final int VK_FINAL = 0x0018;
  321. public static final int VK_CONVERT = 0x001C;
  322. public static final int VK_NONCONVERT = 0x001D;
  323. public static final int VK_ACCEPT = 0x001E;
  324. public static final int VK_MODECHANGE = 0x001F;
  325. public static final int VK_KANA = 0x0015;
  326. public static final int VK_KANJI = 0x0019;
  327. /** Constant for the Alphanumeric function key. */
  328. /* Japanese PC 106 keyboard */
  329. public static final int VK_ALPHANUMERIC = 0x00F0;
  330. /** Constant for the Katakana function key. */
  331. /* Japanese PC 106 keyboard */
  332. public static final int VK_KATAKANA = 0x00F1;
  333. /** Constant for the Hiragana function key. */
  334. /* Japanese PC 106 keyboard */
  335. public static final int VK_HIRAGANA = 0x00F2;
  336. /** Constant for the Full-Width Characters function key. */
  337. /* Japanese PC 106 keyboard */
  338. public static final int VK_FULL_WIDTH = 0x00F3;
  339. /** Constant for the Half-Width Characters function key. */
  340. /* Japanese PC 106 keyboard */
  341. public static final int VK_HALF_WIDTH = 0x00F4;
  342. /** Constant for the Roman Characters function key. */
  343. /* Japanese PC 106 keyboard */
  344. public static final int VK_ROMAN_CHARACTERS = 0x00F5;
  345. /** Constant for the All Candidates function key. */
  346. /* Japanese PC 106 keyboard - VK_CONVERT + ALT */
  347. public static final int VK_ALL_CANDIDATES = 0x0100;
  348. /** Constant for the Previous Candidate function key. */
  349. /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT */
  350. public static final int VK_PREVIOUS_CANDIDATE = 0x0101;
  351. /** Constant for the Code Input function key. */
  352. /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT */
  353. public static final int VK_CODE_INPUT = 0x0102;
  354. /** Constant for the Japanese-Katakana function key.
  355. * This key switches to a Japanese input method and selects its Katakana input mode. */
  356. /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
  357. public static final int VK_JAPANESE_KATAKANA = 0x0103;
  358. /** Constant for the Japanese-Hiragana function key.
  359. * This key switches to a Japanese input method and selects its Hiragana input mode. */
  360. /* Japanese Macintosh keyboard */
  361. public static final int VK_JAPANESE_HIRAGANA = 0x0104;
  362. /** Constant for the Japanese-Roman function key.
  363. * This key switches to a Japanese input method and selects its Roman-Direct input mode. */
  364. /* Japanese Macintosh keyboard */
  365. public static final int VK_JAPANESE_ROMAN = 0x0105;
  366. /* for Sun keyboards */
  367. public static final int VK_CUT = 0xFFD1;
  368. public static final int VK_COPY = 0xFFCD;
  369. public static final int VK_PASTE = 0xFFCF;
  370. public static final int VK_UNDO = 0xFFCB;
  371. public static final int VK_AGAIN = 0xFFC9;
  372. public static final int VK_FIND = 0xFFD0;
  373. public static final int VK_PROPS = 0xFFCA;
  374. public static final int VK_STOP = 0xFFC8;
  375. /** Constant for the Compose function key. */
  376. public static final int VK_COMPOSE = 0xFF20;
  377. /** Constant for the AltGraph modifier key. */
  378. public static final int VK_ALT_GRAPH = 0xFF7E;
  379. /**
  380. * KEY_TYPED events do not have a keyCode value.
  381. * This value is used, instead.
  382. */
  383. public static final int VK_UNDEFINED = 0x0;
  384. /**
  385. * KEY_PRESSED and KEY_RELEASED events which do not map to a
  386. * valid Unicode character use this for the keyChar value.
  387. */
  388. public static final char CHAR_UNDEFINED = 0x0ffff;
  389. /**
  390. * The unique value assigned to each of the keys on the
  391. * keyboard. There is a common set of key codes that
  392. * can be fired by most keyboards.
  393. * The symbolic name for a key code should be used rather
  394. * than the code value itself.
  395. *
  396. * @serial
  397. * @see getKeyCode()
  398. * @see setKeyCode()
  399. */
  400. int keyCode;
  401. /**
  402. * <code>keyChar</code> is a valid unicode character
  403. * that is fired by a key or a key combination on
  404. * a keyboard.
  405. *
  406. * @serial
  407. * @see getKeyChar()
  408. * @see setKeyChar()
  409. */
  410. char keyChar;
  411. /*
  412. * JDK 1.1 serialVersionUID
  413. */
  414. private static final long serialVersionUID = -2352130953028126954L;
  415. static {
  416. /* ensure that the necessary native libraries are loaded */
  417. NativeLibLoader.loadLibraries();
  418. initIDs();
  419. }
  420. /**
  421. * Initialize JNI field and method IDs for fields that may be
  422. accessed from C.
  423. */
  424. private static native void initIDs();
  425. /**
  426. * Constructs a KeyEvent object.
  427. *
  428. * @param source the Component that originated the event
  429. * @param id an integer identifying the type of event
  430. * @param when a long integer that specifys the time the event occurred
  431. * @param modifiers the modifier keys down during event
  432. * (shift, ctrl, alt, meta)
  433. * @param keyCode the integer code for an actual key, or VK_UNDEFINED
  434. * (for a key-typed event)
  435. * @param keyChar the Unicode character generated by this event, or
  436. * CHAR_UNDEFINED (for key-pressed and key-released
  437. * events which do not map to a valid Unicode character)
  438. */
  439. public KeyEvent(Component source, int id, long when, int modifiers,
  440. int keyCode, char keyChar) {
  441. super(source, id, when, modifiers);
  442. if (id == KEY_TYPED && keyChar == CHAR_UNDEFINED) {
  443. throw new IllegalArgumentException("invalid keyChar");
  444. }
  445. if (id == KEY_TYPED && keyCode != VK_UNDEFINED) {
  446. throw new IllegalArgumentException("invalid keyCode");
  447. }
  448. this.keyCode = keyCode;
  449. this.keyChar = keyChar;
  450. }
  451. /*
  452. * @deprecated, as of JDK1.1 - Do NOT USE; will be removed in 1.1.1.
  453. */
  454. public KeyEvent(Component source, int id, long when, int modifiers,
  455. int keyCode) {
  456. this(source, id, when, modifiers, keyCode, (char)keyCode);
  457. }
  458. /**
  459. * Returns the integer key-code associated with the key in this event.
  460. *
  461. * @return the integer code for an actual key on the keyboard.
  462. * (For KEY_TYPED events, keyCode is VK_UNDEFINED.)
  463. */
  464. public int getKeyCode() {
  465. return keyCode;
  466. }
  467. /**
  468. * Set the keyCode value to indicate a physical key.
  469. *
  470. * @param keyCode an integer corresponding to an actual key on the keyboard.
  471. */
  472. public void setKeyCode(int keyCode) {
  473. this.keyCode = keyCode;
  474. }
  475. /**
  476. * Set the keyChar value to indicate a logical character.
  477. *
  478. * @param keyChar a char corresponding to to the combination of keystrokes
  479. * that make up this event.
  480. */
  481. public void setKeyChar(char keyChar) {
  482. this.keyChar = keyChar;
  483. }
  484. /**
  485. * Set the modifiers to indicate additional keys that were held down
  486. * (shift, ctrl, alt, meta) defined as part of InputEvent.
  487. * <p>
  488. * NOTE: use of this method is not recommended, because many AWT
  489. * implementations do not recognize modifier changes. This is
  490. * especially true for KEY_TYPED events where the shift modifier
  491. * is changed.
  492. *
  493. * @param modifiers an integer combination of the modifier constants.
  494. *
  495. * @see InputEvent
  496. *
  497. * @deprecated, as of JDK1.1.4
  498. */
  499. public void setModifiers(int modifiers) {
  500. this.modifiers = modifiers;
  501. }
  502. /**
  503. * Returns the character associated with the key in this event.
  504. * For example, the key-typed event for shift + "a" returns the
  505. * value for "A".
  506. *
  507. * @return the Unicode character defined for this key event.
  508. * If no valid Unicode character exists for this key event,
  509. * keyChar is CHAR_UNDEFINED.
  510. */
  511. public char getKeyChar() {
  512. return keyChar;
  513. }
  514. /**
  515. * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
  516. * These strings can be localized by changing the awt.properties file.
  517. *
  518. * @return string a text description for a physical key, identified by
  519. * its keyCode
  520. */
  521. public static String getKeyText(int keyCode) {
  522. if (keyCode >= VK_0 && keyCode <= VK_9 ||
  523. keyCode >= VK_A && keyCode <= VK_Z) {
  524. return String.valueOf((char)keyCode);
  525. }
  526. // Check for other ASCII keyCodes.
  527. int index = ",./;=[\\]".indexOf(keyCode);
  528. if (index >= 0) {
  529. return String.valueOf((char)keyCode);
  530. }
  531. switch(keyCode) {
  532. case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
  533. case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
  534. case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
  535. case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
  536. case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
  537. case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
  538. case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
  539. case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
  540. case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
  541. case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
  542. case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
  543. case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
  544. case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
  545. case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
  546. case VK_END: return Toolkit.getProperty("AWT.end", "End");
  547. case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
  548. case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
  549. case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
  550. case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
  551. case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
  552. // handled by ASCII value above:
  553. // comma, period, slash, 0-9, semicolon, equals, A-Z,
  554. // open_bracket, back_slash, close_bracket
  555. // numpad numeric keys handled below
  556. case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
  557. case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
  558. case VK_SEPARATER: return Toolkit.getProperty("AWT.separater", "NumPad ,");
  559. case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
  560. case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
  561. case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
  562. case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
  563. case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
  564. case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
  565. case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
  566. case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
  567. case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
  568. case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
  569. case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
  570. case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
  571. case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
  572. case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
  573. case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
  574. case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
  575. case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
  576. case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
  577. case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");
  578. case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");
  579. case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");
  580. case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");
  581. case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");
  582. case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");
  583. case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");
  584. case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");
  585. case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");
  586. case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");
  587. case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");
  588. case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");
  589. case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
  590. case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
  591. case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
  592. case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
  593. case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
  594. case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
  595. case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");
  596. case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");
  597. case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");
  598. case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
  599. case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
  600. case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
  601. case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex");
  602. case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
  603. case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
  604. case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
  605. case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot");
  606. case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis");
  607. case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring");
  608. case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute");
  609. case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
  610. case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla");
  611. case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
  612. case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
  613. case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound");
  614. case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound");
  615. case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand");
  616. case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk");
  617. case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
  618. case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less");
  619. case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater");
  620. case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
  621. case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace");
  622. case VK_AT: return Toolkit.getProperty("AWT.at", "At");
  623. case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon");
  624. case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex");
  625. case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar");
  626. case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro");
  627. case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark");
  628. case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark");
  629. case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis");
  630. case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign");
  631. case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus");
  632. case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus");
  633. case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis");
  634. case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore");
  635. case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
  636. case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
  637. case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
  638. case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
  639. case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
  640. case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
  641. case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
  642. case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric");
  643. case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana");
  644. case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana");
  645. case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
  646. case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
  647. case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters");
  648. case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates");
  649. case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate");
  650. case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input");
  651. case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana");
  652. case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana");
  653. case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman");
  654. case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again");
  655. case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo");
  656. case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy");
  657. case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste");
  658. case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut");
  659. case VK_FIND: return Toolkit.getProperty("AWT.find", "Find");
  660. case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props");
  661. case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop");
  662. case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
  663. case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
  664. }
  665. if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
  666. String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
  667. char c = (char)(keyCode - VK_NUMPAD0 + '0');
  668. return numpad + "-" + c;
  669. }
  670. String unknown = Toolkit.getProperty("AWT.unknown", "Unknown keyCode");
  671. return unknown + ": 0x" + Integer.toString(keyCode, 16);
  672. }
  673. /**
  674. * Returns a String describing the modifier key(s), such as "Shift",
  675. * or "Ctrl+Shift". These strings can be localized by changing the
  676. * awt.properties file.
  677. *
  678. * @return string a text description of the combination of modifier
  679. * keys that were held down during the event
  680. */
  681. public static String getKeyModifiersText(int modifiers) {
  682. StringBuffer buf = new StringBuffer();
  683. if ((modifiers & InputEvent.META_MASK) != 0) {
  684. buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  685. buf.append("+");
  686. }
  687. if ((modifiers & InputEvent.CTRL_MASK) != 0) {
  688. buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  689. buf.append("+");
  690. }
  691. if ((modifiers & InputEvent.ALT_MASK) != 0) {
  692. buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  693. buf.append("+");
  694. }
  695. if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
  696. buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  697. buf.append("+");
  698. }
  699. if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
  700. buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
  701. buf.append("+");
  702. }
  703. if (buf.length() > 0) {
  704. buf.setLength(buf.length()-1); // remove trailing '+'
  705. }
  706. return buf.toString();
  707. }
  708. /** Returns whether or not the key in this event is an "action" key,
  709. * as defined in Event.java.
  710. *
  711. * @return boolean value, true if the key is an "action" key
  712. *
  713. * @see Event
  714. */
  715. public boolean isActionKey() {
  716. switch (keyCode) {
  717. case VK_HOME:
  718. case VK_END:
  719. case VK_PAGE_UP:
  720. case VK_PAGE_DOWN:
  721. case VK_UP:
  722. case VK_DOWN:
  723. case VK_LEFT:
  724. case VK_RIGHT:
  725. case VK_KP_LEFT:
  726. case VK_KP_UP:
  727. case VK_KP_RIGHT:
  728. case VK_KP_DOWN:
  729. case VK_F1:
  730. case VK_F2:
  731. case VK_F3:
  732. case VK_F4:
  733. case VK_F5:
  734. case VK_F6:
  735. case VK_F7:
  736. case VK_F8:
  737. case VK_F9:
  738. case VK_F10:
  739. case VK_F11:
  740. case VK_F12:
  741. case VK_F13:
  742. case VK_F14:
  743. case VK_F15:
  744. case VK_F16:
  745. case VK_F17:
  746. case VK_F18:
  747. case VK_F19:
  748. case VK_F20:
  749. case VK_F21:
  750. case VK_F22:
  751. case VK_F23:
  752. case VK_F24:
  753. case VK_PRINTSCREEN:
  754. case VK_SCROLL_LOCK:
  755. case VK_CAPS_LOCK:
  756. case VK_NUM_LOCK:
  757. case VK_PAUSE:
  758. case VK_INSERT:
  759. case VK_FINAL:
  760. case VK_CONVERT:
  761. case VK_NONCONVERT:
  762. case VK_ACCEPT:
  763. case VK_MODECHANGE:
  764. case VK_KANA:
  765. case VK_KANJI:
  766. case VK_ALPHANUMERIC:
  767. case VK_KATAKANA:
  768. case VK_HIRAGANA:
  769. case VK_FULL_WIDTH:
  770. case VK_HALF_WIDTH:
  771. case VK_ROMAN_CHARACTERS:
  772. case VK_ALL_CANDIDATES:
  773. case VK_PREVIOUS_CANDIDATE:
  774. case VK_CODE_INPUT:
  775. case VK_JAPANESE_KATAKANA:
  776. case VK_JAPANESE_HIRAGANA:
  777. case VK_JAPANESE_ROMAN:
  778. case VK_AGAIN:
  779. case VK_UNDO:
  780. case VK_COPY:
  781. case VK_PASTE:
  782. case VK_CUT:
  783. case VK_FIND:
  784. case VK_PROPS:
  785. case VK_STOP:
  786. case VK_HELP:
  787. return true;
  788. }
  789. return false;
  790. }
  791. /**
  792. * Returns a parameter string identifying this event.
  793. * This method is useful for event-logging and for debugging.
  794. *
  795. * @return a string identifying the event and its attributes
  796. */
  797. public String paramString() {
  798. String typeStr;
  799. switch(id) {
  800. case KEY_PRESSED:
  801. typeStr = "KEY_PRESSED";
  802. break;
  803. case KEY_RELEASED:
  804. typeStr = "KEY_RELEASED";
  805. break;
  806. case KEY_TYPED:
  807. typeStr = "KEY_TYPED";
  808. break;
  809. default:
  810. typeStr = "unknown type";
  811. }
  812. String str = typeStr + ",keyCode=" + keyCode;
  813. if (isActionKey() || keyCode == VK_ENTER || keyCode == VK_BACK_SPACE ||
  814. keyCode == VK_TAB || keyCode == VK_ESCAPE || keyCode == VK_DELETE ||
  815. (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) ||
  816. keyCode == VK_COMPOSE || keyCode == VK_ALT_GRAPH) {
  817. str += "," + getKeyText(keyCode);
  818. } else if (keyChar == '\n' || keyChar == '\b' ||
  819. keyChar == '\t' || keyChar == VK_ESCAPE || keyChar == VK_DELETE) {
  820. str += "," + getKeyText(keyChar);
  821. } else {
  822. str += ",keyChar='" + keyChar + "'";
  823. }
  824. if (modifiers > 0) {
  825. str += ",modifiers=" + getKeyModifiersText(modifiers);
  826. }
  827. return str;
  828. }
  829. }