1. /*
  2. * @(#)KeyEvent.java 1.65 03/01/23
  3. *
  4. * Copyright 2003 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.GraphicsEnvironment;
  11. import java.awt.Toolkit;
  12. import java.io.IOException;
  13. import java.io.ObjectInputStream;
  14. /**
  15. * An event which indicates that a keystroke occurred in a component.
  16. * <p>
  17. * This low-level event is generated by a component object (such as a text
  18. * field) when a key is pressed, released, or typed.
  19. * The event is passed to every <code>KeyListener</code>
  20. * or <code>KeyAdapter</code> object which registered to receive such
  21. * events using the component's <code>addKeyListener</code> method.
  22. * (<code>KeyAdapter</code> objects implement the
  23. * <code>KeyListener</code> interface.) Each such listener object
  24. * gets this <code>KeyEvent</code> when the event occurs.
  25. *
  26. * <p>
  27. * <em>"Key typed" events</em> are higher-level and generally do not depend on
  28. * the platform or keyboard layout. They are generated when a Unicode character
  29. * is entered, and are the preferred way to find out about character input.
  30. * In the simplest case, a key typed event is produced by a single key press
  31. * (e.g., 'a'). Often, however, characters are produced by series of key
  32. * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to
  33. * key typed events may be many-to-one or many-to-many. Key releases are not
  34. * usually necessary to generate a key typed event, but there are some cases
  35. * where the key typed event is not generated until a key is released (e.g.,
  36. * entering ASCII sequences via the Alt-Numpad method in Windows).
  37. * No key typed events are generated for keys that don't generate Unicode
  38. * characters (e.g., action keys, modifier keys, etc.).
  39. * The getKeyChar method always returns a valid Unicode character or
  40. * CHAR_UNDEFINED.
  41. * For key pressed and key released events, the getKeyCode method returns
  42. * the event's keyCode. For key typed events, the getKeyCode method
  43. * always returns VK_UNDEFINED.
  44. *
  45. * <p>
  46. * <em>"Key pressed" and "key released" events</em> are lower-level and depend
  47. * on the platform and keyboard layout. They are generated whenever a key is
  48. * pressed or released, and are the only way to find out about keys that don't
  49. * generate character input (e.g., action keys, modifier keys, etc.). The key
  50. * being pressed or released is indicated by the getKeyCode method, which returns
  51. * a virtual key code.
  52. *
  53. * <p>
  54. * <em>Virtual key codes</em> are used to report which keyboard key has
  55. * been pressed, rather than a character generated by the combination
  56. * of one or more keystrokes (such as "A", which comes from shift and "a").
  57. *
  58. * <p>
  59. * For example, pressing the Shift key will cause a KEY_PRESSED event
  60. * with a VK_SHIFT keyCode, while pressing the 'a' key will result in
  61. * a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event
  62. * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
  63. * value of 'A' is generated.
  64. *
  65. * <p>
  66. * Notes:
  67. * <ul>
  68. * <li>Key combinations which do not result in Unicode characters, such as action
  69. * keys like F1 and the HELP key, do not generate KEY_TYPED events.
  70. * <li>Not all keyboards or systems are capable of generating all
  71. * virtual key codes. No attempt is made in Java to generate these keys
  72. * artificially.
  73. * <li>Virtual key codes do not identify a physical key: they depend on the
  74. * platform and keyboard layout. For example, the key that generates VK_Q
  75. * when using a U.S. keyboard layout will generate VK_A when using a French
  76. * keyboard layout.
  77. * <li>Not all characters have a keycode associated with them. For example,
  78. * there is no keycode for the question mark because there is no keyboard
  79. * for which it appears on the primary layer.
  80. * <li>In order to support the platform-independent handling of action keys,
  81. * the Java platform uses a few additional virtual key constants for functions
  82. * that would otherwise have to be recognized by interpreting virtual key codes
  83. * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
  84. * is returned instead of VK_CONVERT with the ALT modifier.
  85. * </ul>
  86. *
  87. * <p>
  88. * WARNING: Aside from those keys that are defined by the Java language
  89. * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
  90. * constants. Sun reserves the right to change these values as needed
  91. * to accomodate a wider range of keyboards in the future.
  92. *
  93. * @author Carl Quinn
  94. * @author Amy Fowler
  95. * @author Norbert Lindenberg
  96. * @version 1.65 01/23/03
  97. *
  98. * @see KeyAdapter
  99. * @see KeyListener
  100. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/keylistener.html">Tutorial: Writing a Key Listener</a>
  101. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  102. *
  103. * @since 1.1
  104. */
  105. public class KeyEvent extends InputEvent {
  106. /**
  107. * Stores the state of native event dispatching system
  108. * - true, if when the event was created event proxying
  109. * mechanism was active
  110. * - false, if it was inactive
  111. * Used in Component.dispatchEventImpl to correctly dispatch
  112. * events when proxy is active
  113. */
  114. private boolean isProxyActive = false;
  115. /**
  116. * The first number in the range of ids used for key events.
  117. */
  118. public static final int KEY_FIRST = 400;
  119. /**
  120. * The last number in the range of ids used for key events.
  121. */
  122. public static final int KEY_LAST = 402;
  123. /**
  124. * The "key typed" event. This event is generated when a character is
  125. * entered. In the simplest case, it is produced by a single key press.
  126. * Often, however, characters are produced by series of key presses, and
  127. * the mapping from key pressed events to key typed events may be
  128. * many-to-one or many-to-many.
  129. */
  130. public static final int KEY_TYPED = KEY_FIRST;
  131. /**
  132. * The "key pressed" event. This event is generated when a key
  133. * is pushed down.
  134. */
  135. public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
  136. /**
  137. * The "key released" event. This event is generated when a key
  138. * is let up.
  139. */
  140. public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
  141. /* Virtual key codes. */
  142. public static final int VK_ENTER = '\n';
  143. public static final int VK_BACK_SPACE = '\b';
  144. public static final int VK_TAB = '\t';
  145. public static final int VK_CANCEL = 0x03;
  146. public static final int VK_CLEAR = 0x0C;
  147. public static final int VK_SHIFT = 0x10;
  148. public static final int VK_CONTROL = 0x11;
  149. public static final int VK_ALT = 0x12;
  150. public static final int VK_PAUSE = 0x13;
  151. public static final int VK_CAPS_LOCK = 0x14;
  152. public static final int VK_ESCAPE = 0x1B;
  153. public static final int VK_SPACE = 0x20;
  154. public static final int VK_PAGE_UP = 0x21;
  155. public static final int VK_PAGE_DOWN = 0x22;
  156. public static final int VK_END = 0x23;
  157. public static final int VK_HOME = 0x24;
  158. /**
  159. * Constant for the non-numpad <b>left</b> arrow key.
  160. * @see #VK_KP_LEFT
  161. */
  162. public static final int VK_LEFT = 0x25;
  163. /**
  164. * Constant for the non-numpad <b>up</b> arrow key.
  165. * @see #VK_KP_UP
  166. */
  167. public static final int VK_UP = 0x26;
  168. /**
  169. * Constant for the non-numpad <b>right</b> arrow key.
  170. * @see #VK_KP_RIGHT
  171. */
  172. public static final int VK_RIGHT = 0x27;
  173. /**
  174. * Constant for the non-numpad <b>down</b> arrow key.
  175. * @see #VK_KP_DOWN
  176. */
  177. public static final int VK_DOWN = 0x28;
  178. public static final int VK_COMMA = 0x2C;
  179. /**
  180. * Constant for the "-" key.
  181. * @since 1.2
  182. */
  183. public static final int VK_MINUS = 0x2D;
  184. public static final int VK_PERIOD = 0x2E;
  185. public static final int VK_SLASH = 0x2F;
  186. /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
  187. public static final int VK_0 = 0x30;
  188. public static final int VK_1 = 0x31;
  189. public static final int VK_2 = 0x32;
  190. public static final int VK_3 = 0x33;
  191. public static final int VK_4 = 0x34;
  192. public static final int VK_5 = 0x35;
  193. public static final int VK_6 = 0x36;
  194. public static final int VK_7 = 0x37;
  195. public static final int VK_8 = 0x38;
  196. public static final int VK_9 = 0x39;
  197. public static final int VK_SEMICOLON = 0x3B;
  198. public static final int VK_EQUALS = 0x3D;
  199. /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
  200. public static final int VK_A = 0x41;
  201. public static final int VK_B = 0x42;
  202. public static final int VK_C = 0x43;
  203. public static final int VK_D = 0x44;
  204. public static final int VK_E = 0x45;
  205. public static final int VK_F = 0x46;
  206. public static final int VK_G = 0x47;
  207. public static final int VK_H = 0x48;
  208. public static final int VK_I = 0x49;
  209. public static final int VK_J = 0x4A;
  210. public static final int VK_K = 0x4B;
  211. public static final int VK_L = 0x4C;
  212. public static final int VK_M = 0x4D;
  213. public static final int VK_N = 0x4E;
  214. public static final int VK_O = 0x4F;
  215. public static final int VK_P = 0x50;
  216. public static final int VK_Q = 0x51;
  217. public static final int VK_R = 0x52;
  218. public static final int VK_S = 0x53;
  219. public static final int VK_T = 0x54;
  220. public static final int VK_U = 0x55;
  221. public static final int VK_V = 0x56;
  222. public static final int VK_W = 0x57;
  223. public static final int VK_X = 0x58;
  224. public static final int VK_Y = 0x59;
  225. public static final int VK_Z = 0x5A;
  226. public static final int VK_OPEN_BRACKET = 0x5B;
  227. public static final int VK_BACK_SLASH = 0x5C;
  228. public static final int VK_CLOSE_BRACKET = 0x5D;
  229. public static final int VK_NUMPAD0 = 0x60;
  230. public static final int VK_NUMPAD1 = 0x61;
  231. public static final int VK_NUMPAD2 = 0x62;
  232. public static final int VK_NUMPAD3 = 0x63;
  233. public static final int VK_NUMPAD4 = 0x64;
  234. public static final int VK_NUMPAD5 = 0x65;
  235. public static final int VK_NUMPAD6 = 0x66;
  236. public static final int VK_NUMPAD7 = 0x67;
  237. public static final int VK_NUMPAD8 = 0x68;
  238. public static final int VK_NUMPAD9 = 0x69;
  239. public static final int VK_MULTIPLY = 0x6A;
  240. public static final int VK_ADD = 0x6B;
  241. /**
  242. * This constant is obsolete, and is included only for backwards
  243. * compatibility.
  244. * @see #VK_SEPARATOR
  245. */
  246. public static final int VK_SEPARATER = 0x6C;
  247. /**
  248. * Constant for the Numpad Separator key.
  249. * @since 1.4
  250. */
  251. public static final int VK_SEPARATOR = VK_SEPARATER;
  252. public static final int VK_SUBTRACT = 0x6D;
  253. public static final int VK_DECIMAL = 0x6E;
  254. public static final int VK_DIVIDE = 0x6F;
  255. public static final int VK_DELETE = 0x7F; /* ASCII DEL */
  256. public static final int VK_NUM_LOCK = 0x90;
  257. public static final int VK_SCROLL_LOCK = 0x91;
  258. /** Constant for the F1 function key. */
  259. public static final int VK_F1 = 0x70;
  260. /** Constant for the F2 function key. */
  261. public static final int VK_F2 = 0x71;
  262. /** Constant for the F3 function key. */
  263. public static final int VK_F3 = 0x72;
  264. /** Constant for the F4 function key. */
  265. public static final int VK_F4 = 0x73;
  266. /** Constant for the F5 function key. */
  267. public static final int VK_F5 = 0x74;
  268. /** Constant for the F6 function key. */
  269. public static final int VK_F6 = 0x75;
  270. /** Constant for the F7 function key. */
  271. public static final int VK_F7 = 0x76;
  272. /** Constant for the F8 function key. */
  273. public static final int VK_F8 = 0x77;
  274. /** Constant for the F9 function key. */
  275. public static final int VK_F9 = 0x78;
  276. /** Constant for the F10 function key. */
  277. public static final int VK_F10 = 0x79;
  278. /** Constant for the F11 function key. */
  279. public static final int VK_F11 = 0x7A;
  280. /** Constant for the F12 function key. */
  281. public static final int VK_F12 = 0x7B;
  282. /**
  283. * Constant for the F13 function key.
  284. * @since 1.2
  285. */
  286. /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
  287. public static final int VK_F13 = 0xF000;
  288. /**
  289. * Constant for the F14 function key.
  290. * @since 1.2
  291. */
  292. public static final int VK_F14 = 0xF001;
  293. /**
  294. * Constant for the F15 function key.
  295. * @since 1.2
  296. */
  297. public static final int VK_F15 = 0xF002;
  298. /**
  299. * Constant for the F16 function key.
  300. * @since 1.2
  301. */
  302. public static final int VK_F16 = 0xF003;
  303. /**
  304. * Constant for the F17 function key.
  305. * @since 1.2
  306. */
  307. public static final int VK_F17 = 0xF004;
  308. /**
  309. * Constant for the F18 function key.
  310. * @since 1.2
  311. */
  312. public static final int VK_F18 = 0xF005;
  313. /**
  314. * Constant for the F19 function key.
  315. * @since 1.2
  316. */
  317. public static final int VK_F19 = 0xF006;
  318. /**
  319. * Constant for the F20 function key.
  320. * @since 1.2
  321. */
  322. public static final int VK_F20 = 0xF007;
  323. /**
  324. * Constant for the F21 function key.
  325. * @since 1.2
  326. */
  327. public static final int VK_F21 = 0xF008;
  328. /**
  329. * Constant for the F22 function key.
  330. * @since 1.2
  331. */
  332. public static final int VK_F22 = 0xF009;
  333. /**
  334. * Constant for the F23 function key.
  335. * @since 1.2
  336. */
  337. public static final int VK_F23 = 0xF00A;
  338. /**
  339. * Constant for the F24 function key.
  340. * @since 1.2
  341. */
  342. public static final int VK_F24 = 0xF00B;
  343. public static final int VK_PRINTSCREEN = 0x9A;
  344. public static final int VK_INSERT = 0x9B;
  345. public static final int VK_HELP = 0x9C;
  346. public static final int VK_META = 0x9D;
  347. public static final int VK_BACK_QUOTE = 0xC0;
  348. public static final int VK_QUOTE = 0xDE;
  349. /**
  350. * Constant for the numeric keypad <b>up</b> arrow key.
  351. * @see #VK_UP
  352. * @since 1.2
  353. */
  354. public static final int VK_KP_UP = 0xE0;
  355. /**
  356. * Constant for the numeric keypad <b>down</b> arrow key.
  357. * @see #VK_DOWN
  358. * @since 1.2
  359. */
  360. public static final int VK_KP_DOWN = 0xE1;
  361. /**
  362. * Constant for the numeric keypad <b>left</b> arrow key.
  363. * @see #VK_LEFT
  364. * @since 1.2
  365. */
  366. public static final int VK_KP_LEFT = 0xE2;
  367. /**
  368. * Constant for the numeric keypad <b>right</b> arrow key.
  369. * @see #VK_RIGHT
  370. * @since 1.2
  371. */
  372. public static final int VK_KP_RIGHT = 0xE3;
  373. /* For European keyboards */
  374. /** @since 1.2 */
  375. public static final int VK_DEAD_GRAVE = 0x80;
  376. /** @since 1.2 */
  377. public static final int VK_DEAD_ACUTE = 0x81;
  378. /** @since 1.2 */
  379. public static final int VK_DEAD_CIRCUMFLEX = 0x82;
  380. /** @since 1.2 */
  381. public static final int VK_DEAD_TILDE = 0x83;
  382. /** @since 1.2 */
  383. public static final int VK_DEAD_MACRON = 0x84;
  384. /** @since 1.2 */
  385. public static final int VK_DEAD_BREVE = 0x85;
  386. /** @since 1.2 */
  387. public static final int VK_DEAD_ABOVEDOT = 0x86;
  388. /** @since 1.2 */
  389. public static final int VK_DEAD_DIAERESIS = 0x87;
  390. /** @since 1.2 */
  391. public static final int VK_DEAD_ABOVERING = 0x88;
  392. /** @since 1.2 */
  393. public static final int VK_DEAD_DOUBLEACUTE = 0x89;
  394. /** @since 1.2 */
  395. public static final int VK_DEAD_CARON = 0x8a;
  396. /** @since 1.2 */
  397. public static final int VK_DEAD_CEDILLA = 0x8b;
  398. /** @since 1.2 */
  399. public static final int VK_DEAD_OGONEK = 0x8c;
  400. /** @since 1.2 */
  401. public static final int VK_DEAD_IOTA = 0x8d;
  402. /** @since 1.2 */
  403. public static final int VK_DEAD_VOICED_SOUND = 0x8e;
  404. /** @since 1.2 */
  405. public static final int VK_DEAD_SEMIVOICED_SOUND = 0x8f;
  406. /** @since 1.2 */
  407. public static final int VK_AMPERSAND = 0x96;
  408. /** @since 1.2 */
  409. public static final int VK_ASTERISK = 0x97;
  410. /** @since 1.2 */
  411. public static final int VK_QUOTEDBL = 0x98;
  412. /** @since 1.2 */
  413. public static final int VK_LESS = 0x99;
  414. /** @since 1.2 */
  415. public static final int VK_GREATER = 0xa0;
  416. /** @since 1.2 */
  417. public static final int VK_BRACELEFT = 0xa1;
  418. /** @since 1.2 */
  419. public static final int VK_BRACERIGHT = 0xa2;
  420. /**
  421. * Constant for the "@" key.
  422. * @since 1.2
  423. */
  424. public static final int VK_AT = 0x0200;
  425. /**
  426. * Constant for the ":" key.
  427. * @since 1.2
  428. */
  429. public static final int VK_COLON = 0x0201;
  430. /**
  431. * Constant for the "^" key.
  432. * @since 1.2
  433. */
  434. public static final int VK_CIRCUMFLEX = 0x0202;
  435. /**
  436. * Constant for the "$" key.
  437. * @since 1.2
  438. */
  439. public static final int VK_DOLLAR = 0x0203;
  440. /**
  441. * Constant for the Euro currency sign key.
  442. * @since 1.2
  443. */
  444. public static final int VK_EURO_SIGN = 0x0204;
  445. /**
  446. * Constant for the "!" key.
  447. * @since 1.2
  448. */
  449. public static final int VK_EXCLAMATION_MARK = 0x0205;
  450. /**
  451. * Constant for the inverted exclamation mark key.
  452. * @since 1.2
  453. */
  454. public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
  455. /**
  456. * Constant for the "(" key.
  457. * @since 1.2
  458. */
  459. public static final int VK_LEFT_PARENTHESIS = 0x0207;
  460. /**
  461. * Constant for the "#" key.
  462. * @since 1.2
  463. */
  464. public static final int VK_NUMBER_SIGN = 0x0208;
  465. /**
  466. * Constant for the "+" key.
  467. * @since 1.2
  468. */
  469. public static final int VK_PLUS = 0x0209;
  470. /**
  471. * Constant for the ")" key.
  472. * @since 1.2
  473. */
  474. public static final int VK_RIGHT_PARENTHESIS = 0x020A;
  475. /**
  476. * Constant for the "_" key.
  477. * @since 1.2
  478. */
  479. public static final int VK_UNDERSCORE = 0x020B;
  480. /* for input method support on Asian Keyboards */
  481. /* not clear what this means - listed in Microsoft Windows API */
  482. public static final int VK_FINAL = 0x0018;
  483. /** Constant for the Convert function key. */
  484. /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
  485. public static final int VK_CONVERT = 0x001C;
  486. /** Constant for the Don't Convert function key. */
  487. /* Japanese PC 106 keyboard: muhenkan */
  488. public static final int VK_NONCONVERT = 0x001D;
  489. /** Constant for the Accept or Commit function key. */
  490. /* Japanese Solaris keyboard: kakutei */
  491. public static final int VK_ACCEPT = 0x001E;
  492. /* not clear what this means - listed in Microsoft Windows API */
  493. public static final int VK_MODECHANGE = 0x001F;
  494. /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
  495. might still be used on other platforms */
  496. public static final int VK_KANA = 0x0015;
  497. /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
  498. might still be used for other platforms */
  499. public static final int VK_KANJI = 0x0019;
  500. /**
  501. * Constant for the Alphanumeric function key.
  502. * @since 1.2
  503. */
  504. /* Japanese PC 106 keyboard: eisuu */
  505. public static final int VK_ALPHANUMERIC = 0x00F0;
  506. /**
  507. * Constant for the Katakana function key.
  508. * @since 1.2
  509. */
  510. /* Japanese PC 106 keyboard: katakana */
  511. public static final int VK_KATAKANA = 0x00F1;
  512. /**
  513. * Constant for the Hiragana function key.
  514. * @since 1.2
  515. */
  516. /* Japanese PC 106 keyboard: hiragana */
  517. public static final int VK_HIRAGANA = 0x00F2;
  518. /**
  519. * Constant for the Full-Width Characters function key.
  520. * @since 1.2
  521. */
  522. /* Japanese PC 106 keyboard: zenkaku */
  523. public static final int VK_FULL_WIDTH = 0x00F3;
  524. /**
  525. * Constant for the Half-Width Characters function key.
  526. * @since 1.2
  527. */
  528. /* Japanese PC 106 keyboard: hankaku */
  529. public static final int VK_HALF_WIDTH = 0x00F4;
  530. /**
  531. * Constant for the Roman Characters function key.
  532. * @since 1.2
  533. */
  534. /* Japanese PC 106 keyboard: roumaji */
  535. public static final int VK_ROMAN_CHARACTERS = 0x00F5;
  536. /**
  537. * Constant for the All Candidates function key.
  538. * @since 1.2
  539. */
  540. /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */
  541. public static final int VK_ALL_CANDIDATES = 0x0100;
  542. /**
  543. * Constant for the Previous Candidate function key.
  544. * @since 1.2
  545. */
  546. /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */
  547. public static final int VK_PREVIOUS_CANDIDATE = 0x0101;
  548. /**
  549. * Constant for the Code Input function key.
  550. * @since 1.2
  551. */
  552. /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */
  553. public static final int VK_CODE_INPUT = 0x0102;
  554. /**
  555. * Constant for the Japanese-Katakana function key.
  556. * This key switches to a Japanese input method and selects its Katakana input mode.
  557. * @since 1.2
  558. */
  559. /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
  560. public static final int VK_JAPANESE_KATAKANA = 0x0103;
  561. /**
  562. * Constant for the Japanese-Hiragana function key.
  563. * This key switches to a Japanese input method and selects its Hiragana input mode.
  564. * @since 1.2
  565. */
  566. /* Japanese Macintosh keyboard */
  567. public static final int VK_JAPANESE_HIRAGANA = 0x0104;
  568. /**
  569. * Constant for the Japanese-Roman function key.
  570. * This key switches to a Japanese input method and selects its Roman-Direct input mode.
  571. * @since 1.2
  572. */
  573. /* Japanese Macintosh keyboard */
  574. public static final int VK_JAPANESE_ROMAN = 0x0105;
  575. /**
  576. * Constant for the locking Kana function key.
  577. * This key locks the keyboard into a Kana layout.
  578. * @since 1.3
  579. */
  580. /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */
  581. public static final int VK_KANA_LOCK = 0x0106;
  582. /**
  583. * Constant for the input method on/off key.
  584. * @since 1.3
  585. */
  586. /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */
  587. public static final int VK_INPUT_METHOD_ON_OFF = 0x0107;
  588. /* for Sun keyboards */
  589. /** @since 1.2 */
  590. public static final int VK_CUT = 0xFFD1;
  591. /** @since 1.2 */
  592. public static final int VK_COPY = 0xFFCD;
  593. /** @since 1.2 */
  594. public static final int VK_PASTE = 0xFFCF;
  595. /** @since 1.2 */
  596. public static final int VK_UNDO = 0xFFCB;
  597. /** @since 1.2 */
  598. public static final int VK_AGAIN = 0xFFC9;
  599. /** @since 1.2 */
  600. public static final int VK_FIND = 0xFFD0;
  601. /** @since 1.2 */
  602. public static final int VK_PROPS = 0xFFCA;
  603. /** @since 1.2 */
  604. public static final int VK_STOP = 0xFFC8;
  605. /**
  606. * Constant for the Compose function key.
  607. * @since 1.2
  608. */
  609. public static final int VK_COMPOSE = 0xFF20;
  610. /**
  611. * Constant for the AltGraph function key.
  612. * @since 1.2
  613. */
  614. public static final int VK_ALT_GRAPH = 0xFF7E;
  615. /**
  616. * This value is used to indicate that the keyCode is unknown.
  617. * KEY_TYPED events do not have a keyCode value; this value
  618. * is used instead.
  619. */
  620. public static final int VK_UNDEFINED = 0x0;
  621. /**
  622. * KEY_PRESSED and KEY_RELEASED events which do not map to a
  623. * valid Unicode character use this for the keyChar value.
  624. */
  625. public static final char CHAR_UNDEFINED = 0xFFFF;
  626. /**
  627. * A constant indicating that the keyLocation is indeterminate
  628. * or not relevant.
  629. * KEY_TYPED events do not have a keyLocation; this value
  630. * is used instead.
  631. * @since 1.4
  632. */
  633. public static final int KEY_LOCATION_UNKNOWN = 0;
  634. /**
  635. * A constant indicating that the key pressed or released
  636. * is not distinguished as the left or right version of a key,
  637. * and did not originate on the numeric keypad (or did not
  638. * originate with a virtual key corresponding to the numeric
  639. * keypad).
  640. * @since 1.4
  641. */
  642. public static final int KEY_LOCATION_STANDARD = 1;
  643. /**
  644. * A constant indicating that the key pressed or released is in
  645. * the left key location (there is more than one possible location
  646. * for this key). Example: the left shift key.
  647. * @since 1.4
  648. */
  649. public static final int KEY_LOCATION_LEFT = 2;
  650. /**
  651. * A constant indicating that the key pressed or released is in
  652. * the right key location (there is more than one possible location
  653. * for this key). Example: the right shift key.
  654. * @since 1.4
  655. */
  656. public static final int KEY_LOCATION_RIGHT = 3;
  657. /**
  658. * A constant indicating that the key event originated on the
  659. * numeric keypad or with a virtual key corresponding to the
  660. * numeric keypad.
  661. * @since 1.4
  662. */
  663. public static final int KEY_LOCATION_NUMPAD = 4;
  664. /**
  665. * The unique value assigned to each of the keys on the
  666. * keyboard. There is a common set of key codes that
  667. * can be fired by most keyboards.
  668. * The symbolic name for a key code should be used rather
  669. * than the code value itself.
  670. *
  671. * @serial
  672. * @see #getKeyCode()
  673. * @see #setKeyCode(int)
  674. */
  675. int keyCode;
  676. /**
  677. * <code>keyChar</code> is a valid unicode character
  678. * that is fired by a key or a key combination on
  679. * a keyboard.
  680. *
  681. * @serial
  682. * @see #getKeyChar()
  683. * @see #setKeyChar(char)
  684. */
  685. char keyChar;
  686. /**
  687. * The location of the key on the keyboard.
  688. *
  689. * Some keys occur more than once on a keyboard, e.g. the left and
  690. * right shift keys. Additionally, some keys occur on the numeric
  691. * keypad. This variable is used to distinguish such keys.
  692. *
  693. * The only legal values are <code>KEY_LOCATION_UNKNOWN</code>,
  694. * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
  695. * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
  696. *
  697. * @serial
  698. * @see #getKeyLocation()
  699. */
  700. int keyLocation;
  701. /*
  702. * JDK 1.1 serialVersionUID
  703. */
  704. private static final long serialVersionUID = -2352130953028126954L;
  705. static {
  706. /* ensure that the necessary native libraries are loaded */
  707. NativeLibLoader.loadLibraries();
  708. if (!GraphicsEnvironment.isHeadless()) {
  709. initIDs();
  710. }
  711. }
  712. /**
  713. * Initialize JNI field and method IDs for fields that may be
  714. * accessed from C.
  715. */
  716. private static native void initIDs();
  717. /**
  718. * Constructs a <code>KeyEvent</code> object.
  719. * <p>Note that passing in an invalid <code>id</code> results in
  720. * unspecified behavior.
  721. *
  722. * @param source the <code>Component</code> that originated the event
  723. * @param id an integer identifying the type of event
  724. * @param when a long integer that specifies the time the event
  725. * occurred
  726. * @param modifiers the modifier keys down during event (shift, ctrl,
  727. * alt, meta)
  728. * Either extended _DOWN_MASK or old _MASK modifiers
  729. * should be used, but both models should not be mixed
  730. * in one event. Use of the extended modifiers is
  731. * preferred.
  732. * @param keyCode the integer code for an actual key, or VK_UNDEFINED
  733. * (for a key-typed event)
  734. * @param keyChar the Unicode character generated by this event, or
  735. * CHAR_UNDEFINED (for key-pressed and key-released
  736. * events which do not map to a valid Unicode character)
  737. * @param keyLocation identifies the key location. The only legal
  738. * values are <code>KEY_LOCATION_UNKNOWN</code>,
  739. * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
  740. * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
  741. * @exception IllegalArgumentException
  742. * if <code>id</code> is <code>KEY_TYPED</code> and
  743. * <code>keyChar</code> is <code>CHAR_UNDEFINED</code>
  744. * or if <code>id</code> is <code>KEY_TYPED</code> and
  745. * <code>keyCode</code> is not <code>VK_UNDEFINED</code>
  746. * or if <code>id</code> is <code>KEY_TYPED</code> and
  747. * <code>keyLocation</code> is not <code>KEY_LOCATION_UNKNOWN</code>
  748. * or if <code>keyLocation</code> is not one of the legal
  749. * values enumerated above.
  750. * @since 1.4
  751. */
  752. private KeyEvent(Component source, int id, long when, int modifiers,
  753. int keyCode, char keyChar, int keyLocation, boolean isProxyActive) {
  754. this(source, id, when, modifiers, keyCode, keyChar, keyLocation);
  755. this.isProxyActive = isProxyActive;
  756. }
  757. public KeyEvent(Component source, int id, long when, int modifiers,
  758. int keyCode, char keyChar, int keyLocation) {
  759. super(source, id, when, modifiers);
  760. if (id == KEY_TYPED) {
  761. if (keyChar == CHAR_UNDEFINED) {
  762. throw new IllegalArgumentException("invalid keyChar");
  763. }
  764. if (keyCode != VK_UNDEFINED) {
  765. throw new IllegalArgumentException("invalid keyCode");
  766. }
  767. if (keyLocation != KEY_LOCATION_UNKNOWN) {
  768. throw new IllegalArgumentException("invalid keyLocation");
  769. }
  770. }
  771. this.keyCode = keyCode;
  772. this.keyChar = keyChar;
  773. if ((keyLocation < KEY_LOCATION_UNKNOWN) ||
  774. (keyLocation > KEY_LOCATION_NUMPAD)) {
  775. throw new IllegalArgumentException("invalid keyLocation");
  776. }
  777. this.keyLocation = keyLocation;
  778. if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
  779. setNewModifiers();
  780. } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
  781. setOldModifiers();
  782. }
  783. }
  784. /**
  785. * Constructs a <code>KeyEvent</code> object.
  786. * <p>Note that passing in an invalid <code>id</code> results in
  787. * unspecified behavior.
  788. *
  789. * @param source the <code>Component</code> that originated the event
  790. * @param id an integer identifying the type of event
  791. * @param when a long integer that specifies the time the event
  792. * occurred
  793. * @param modifiers the modifier keys down during event (shift, ctrl,
  794. * alt, meta)
  795. * Either extended _DOWN_MASK or old _MASK modifiers
  796. * should be used, but both models should not be mixed
  797. * in one event. Use of the extended modifiers is
  798. * preferred.
  799. * @param keyCode the integer code for an actual key, or VK_UNDEFINED
  800. * (for a key-typed event)
  801. * @param keyChar the Unicode character generated by this event, or
  802. * CHAR_UNDEFINED (for key-pressed and key-released
  803. * events which do not map to a valid Unicode character)
  804. * @exception IllegalArgumentException if <code>id</code> is
  805. * <code>KEY_TYPED</code> and <code>keyChar</code> is
  806. * <code>CHAR_UNDEFINED</code> or if <code>id</code> is
  807. * <code>KEY_TYPED</code> and <code>keyCode</code> is not
  808. * <code>VK_UNDEFINED</code>
  809. */
  810. public KeyEvent(Component source, int id, long when, int modifiers,
  811. int keyCode, char keyChar) {
  812. this(source, id, when, modifiers, keyCode, keyChar,
  813. KEY_LOCATION_UNKNOWN);
  814. }
  815. /**
  816. * @deprecated as of JDK1.1
  817. */
  818. public KeyEvent(Component source, int id, long when, int modifiers,
  819. int keyCode) {
  820. this(source, id, when, modifiers, keyCode, (char)keyCode);
  821. }
  822. /**
  823. * Returns the integer keyCode associated with the key in this event.
  824. *
  825. * @return the integer code for an actual key on the keyboard.
  826. * (For <code>KEY_TYPED</code> events, the keyCode is
  827. * <code>VK_UNDEFINED</code>.)
  828. */
  829. public int getKeyCode() {
  830. return keyCode;
  831. }
  832. /**
  833. * Set the keyCode value to indicate a physical key.
  834. *
  835. * @param keyCode an integer corresponding to an actual key on the keyboard.
  836. */
  837. public void setKeyCode(int keyCode) {
  838. this.keyCode = keyCode;
  839. }
  840. /**
  841. * Returns the character associated with the key in this event.
  842. * For example, the key-typed event for shift + "a" returns the
  843. * value for "A".
  844. *
  845. * @return the Unicode character defined for this key event.
  846. * If no valid Unicode character exists for this key event,
  847. * keyChar is <code>CHAR_UNDEFINED</code>.
  848. */
  849. public char getKeyChar() {
  850. return keyChar;
  851. }
  852. /**
  853. * Set the keyChar value to indicate a logical character.
  854. *
  855. * @param keyChar a char corresponding to to the combination of keystrokes
  856. * that make up this event.
  857. */
  858. public void setKeyChar(char keyChar) {
  859. this.keyChar = keyChar;
  860. }
  861. /**
  862. * Set the modifiers to indicate additional keys that were held down
  863. * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
  864. * <p>
  865. * NOTE: use of this method is not recommended, because many AWT
  866. * implementations do not recognize modifier changes. This is
  867. * especially true for <code>KEY_TYPED</code> events where the shift
  868. * modifier is changed.
  869. *
  870. * @param modifiers an integer combination of the modifier constants.
  871. * @see InputEvent
  872. * @deprecated as of JDK1.1.4
  873. */
  874. public void setModifiers(int modifiers) {
  875. this.modifiers = modifiers;
  876. if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
  877. setNewModifiers();
  878. } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
  879. setOldModifiers();
  880. }
  881. }
  882. /**
  883. * Returns the location of the key that originated this key event.
  884. *
  885. * Some keys occur more than once on a keyboard, e.g. the left and
  886. * right shift keys. Additionally, some keys occur on the numeric
  887. * keypad. This provides a way of distinguishing such keys.
  888. *
  889. * @return the location of the key that was pressed or released.
  890. * Always returns <code>KEY_LOCATION_UNKNOWN</code> for
  891. * <code>KEY_TYPED</code> events.
  892. * @since 1.4
  893. */
  894. public int getKeyLocation() {
  895. return keyLocation;
  896. }
  897. /**
  898. * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
  899. * These strings can be localized by changing the awt.properties file.
  900. *
  901. * @return a string containing a text description for a physical key,
  902. * identified by its keyCode
  903. */
  904. public static String getKeyText(int keyCode) {
  905. if (keyCode >= VK_0 && keyCode <= VK_9 ||
  906. keyCode >= VK_A && keyCode <= VK_Z) {
  907. return String.valueOf((char)keyCode);
  908. }
  909. // Check for other ASCII keyCodes.
  910. int index = ",./;=[\\]".indexOf(keyCode);
  911. if (index >= 0) {
  912. return String.valueOf((char)keyCode);
  913. }
  914. switch(keyCode) {
  915. case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter");
  916. case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace");
  917. case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab");
  918. case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel");
  919. case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear");
  920. case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift");
  921. case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control");
  922. case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt");
  923. case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause");
  924. case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
  925. case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape");
  926. case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space");
  927. case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up");
  928. case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down");
  929. case VK_END: return Toolkit.getProperty("AWT.end", "End");
  930. case VK_HOME: return Toolkit.getProperty("AWT.home", "Home");
  931. case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left");
  932. case VK_UP: return Toolkit.getProperty("AWT.up", "Up");
  933. case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
  934. case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down");
  935. // handled by ASCII value above:
  936. // comma, period, slash, 0-9, semicolon, equals, A-Z,
  937. // open_bracket, back_slash, close_bracket
  938. // numpad numeric keys handled below
  939. case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *");
  940. case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +");
  941. case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,");
  942. case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -");
  943. case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad .");
  944. case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /");
  945. case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete");
  946. case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock");
  947. case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
  948. case VK_F1: return Toolkit.getProperty("AWT.f1", "F1");
  949. case VK_F2: return Toolkit.getProperty("AWT.f2", "F2");
  950. case VK_F3: return Toolkit.getProperty("AWT.f3", "F3");
  951. case VK_F4: return Toolkit.getProperty("AWT.f4", "F4");
  952. case VK_F5: return Toolkit.getProperty("AWT.f5", "F5");
  953. case VK_F6: return Toolkit.getProperty("AWT.f6", "F6");
  954. case VK_F7: return Toolkit.getProperty("AWT.f7", "F7");
  955. case VK_F8: return Toolkit.getProperty("AWT.f8", "F8");
  956. case VK_F9: return Toolkit.getProperty("AWT.f9", "F9");
  957. case VK_F10: return Toolkit.getProperty("AWT.f10", "F10");
  958. case VK_F11: return Toolkit.getProperty("AWT.f11", "F11");
  959. case VK_F12: return Toolkit.getProperty("AWT.f12", "F12");
  960. case VK_F13: return Toolkit.getProperty("AWT.f13", "F13");
  961. case VK_F14: return Toolkit.getProperty("AWT.f14", "F14");
  962. case VK_F15: return Toolkit.getProperty("AWT.f15", "F15");
  963. case VK_F16: return Toolkit.getProperty("AWT.f16", "F16");
  964. case VK_F17: return Toolkit.getProperty("AWT.f17", "F17");
  965. case VK_F18: return Toolkit.getProperty("AWT.f18", "F18");
  966. case VK_F19: return Toolkit.getProperty("AWT.f19", "F19");
  967. case VK_F20: return Toolkit.getProperty("AWT.f20", "F20");
  968. case VK_F21: return Toolkit.getProperty("AWT.f21", "F21");
  969. case VK_F22: return Toolkit.getProperty("AWT.f22", "F22");
  970. case VK_F23: return Toolkit.getProperty("AWT.f23", "F23");
  971. case VK_F24: return Toolkit.getProperty("AWT.f24", "F24");
  972. case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen");
  973. case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert");
  974. case VK_HELP: return Toolkit.getProperty("AWT.help", "Help");
  975. case VK_META: return Toolkit.getProperty("AWT.meta", "Meta");
  976. case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote");
  977. case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote");
  978. case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up");
  979. case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down");
  980. case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left");
  981. case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right");
  982. case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
  983. case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
  984. case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex");
  985. case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
  986. case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
  987. case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
  988. case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot");
  989. case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis");
  990. case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring");
  991. case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute");
  992. case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
  993. case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla");
  994. case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
  995. case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
  996. case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound");
  997. case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound");
  998. case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand");
  999. case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk");
  1000. case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
  1001. case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less");
  1002. case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater");
  1003. case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
  1004. case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace");
  1005. case VK_AT: return Toolkit.getProperty("AWT.at", "At");
  1006. case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon");
  1007. case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex");
  1008. case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar");
  1009. case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro");
  1010. case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark");
  1011. case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark");
  1012. case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis");
  1013. case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign");
  1014. case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus");
  1015. case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus");
  1016. case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis");
  1017. case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore");
  1018. case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final");
  1019. case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert");
  1020. case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert");
  1021. case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept");
  1022. case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change");
  1023. case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana");
  1024. case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji");
  1025. case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric");
  1026. case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana");
  1027. case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana");
  1028. case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
  1029. case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
  1030. case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters");
  1031. case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates");
  1032. case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate");
  1033. case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input");
  1034. case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana");
  1035. case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana");
  1036. case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman");
  1037. case VK_KANA_LOCK: return Toolkit.getProperty("AWT.kanaLock", "Kana Lock");
  1038. case VK_INPUT_METHOD_ON_OFF: return Toolkit.getProperty("AWT.inputMethodOnOff", "Input Method On/Off");
  1039. case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again");
  1040. case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo");
  1041. case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy");
  1042. case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste");
  1043. case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut");
  1044. case VK_FIND: return Toolkit.getProperty("AWT.find", "Find");
  1045. case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props");
  1046. case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop");
  1047. case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose");
  1048. case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
  1049. }
  1050. if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
  1051. String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
  1052. char c = (char)(keyCode - VK_NUMPAD0 + '0');
  1053. return numpad + "-" + c;
  1054. }
  1055. String unknown = Toolkit.getProperty("AWT.unknown", "Unknown keyCode");
  1056. return unknown + ": 0x" + Integer.toString(keyCode, 16);
  1057. }
  1058. /**
  1059. * Returns a String describing the modifier key(s), such as "Shift",
  1060. * or "Ctrl+Shift". These strings can be localized by changing the
  1061. * awt.properties file.
  1062. *
  1063. * @return string a text description of the combination of modifier
  1064. * keys that were held down during the event
  1065. */
  1066. public static String getKeyModifiersText(int modifiers) {
  1067. StringBuffer buf = new StringBuffer();
  1068. if ((modifiers & InputEvent.META_MASK) != 0) {
  1069. buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
  1070. buf.append("+");
  1071. }
  1072. if ((modifiers & InputEvent.CTRL_MASK) != 0) {
  1073. buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
  1074. buf.append("+");
  1075. }
  1076. if ((modifiers & InputEvent.ALT_MASK) != 0) {
  1077. buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
  1078. buf.append("+");
  1079. }
  1080. if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
  1081. buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
  1082. buf.append("+");
  1083. }
  1084. if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
  1085. buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
  1086. buf.append("+");
  1087. }
  1088. if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
  1089. buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
  1090. buf.append("+");
  1091. }
  1092. if (buf.length() > 0) {
  1093. buf.setLength(buf.length()-1); // remove trailing '+'
  1094. }
  1095. return buf.toString();
  1096. }
  1097. /**
  1098. * Returns whether the key in this event is an "action" key.
  1099. * Typically an action key does not fire a unicode character and is
  1100. * not a modifier key.
  1101. *
  1102. * @return <code>true</code> if the key is an "action" key,
  1103. * <code>false</code> otherwise
  1104. */
  1105. public boolean isActionKey() {
  1106. switch (keyCode) {
  1107. case VK_HOME:
  1108. case VK_END:
  1109. case VK_PAGE_UP:
  1110. case VK_PAGE_DOWN:
  1111. case VK_UP:
  1112. case VK_DOWN:
  1113. case VK_LEFT:
  1114. case VK_RIGHT:
  1115. case VK_KP_LEFT:
  1116. case VK_KP_UP:
  1117. case VK_KP_RIGHT:
  1118. case VK_KP_DOWN:
  1119. case VK_F1:
  1120. case VK_F2:
  1121. case VK_F3:
  1122. case VK_F4:
  1123. case VK_F5:
  1124. case VK_F6:
  1125. case VK_F7:
  1126. case VK_F8:
  1127. case VK_F9:
  1128. case VK_F10:
  1129. case VK_F11:
  1130. case VK_F12:
  1131. case VK_F13:
  1132. case VK_F14:
  1133. case VK_F15:
  1134. case VK_F16:
  1135. case VK_F17:
  1136. case VK_F18:
  1137. case VK_F19:
  1138. case VK_F20:
  1139. case VK_F21:
  1140. case VK_F22:
  1141. case VK_F23:
  1142. case VK_F24:
  1143. case VK_PRINTSCREEN:
  1144. case VK_SCROLL_LOCK:
  1145. case VK_CAPS_LOCK:
  1146. case VK_NUM_LOCK:
  1147. case VK_PAUSE:
  1148. case VK_INSERT:
  1149. case VK_FINAL:
  1150. case VK_CONVERT:
  1151. case VK_NONCONVERT:
  1152. case VK_ACCEPT:
  1153. case VK_MODECHANGE:
  1154. case VK_KANA:
  1155. case VK_KANJI:
  1156. case VK_ALPHANUMERIC:
  1157. case VK_KATAKANA:
  1158. case VK_HIRAGANA:
  1159. case VK_FULL_WIDTH:
  1160. case VK_HALF_WIDTH:
  1161. case VK_ROMAN_CHARACTERS:
  1162. case VK_ALL_CANDIDATES:
  1163. case VK_PREVIOUS_CANDIDATE:
  1164. case VK_CODE_INPUT:
  1165. case VK_JAPANESE_KATAKANA:
  1166. case VK_JAPANESE_HIRAGANA:
  1167. case VK_JAPANESE_ROMAN:
  1168. case VK_KANA_LOCK:
  1169. case VK_INPUT_METHOD_ON_OFF:
  1170. case VK_AGAIN:
  1171. case VK_UNDO:
  1172. case VK_COPY:
  1173. case VK_PASTE:
  1174. case VK_CUT:
  1175. case VK_FIND:
  1176. case VK_PROPS:
  1177. case VK_STOP:
  1178. case VK_HELP:
  1179. return true;
  1180. }
  1181. return false;
  1182. }
  1183. /**
  1184. * Returns a parameter string identifying this event.
  1185. * This method is useful for event logging and for debugging.
  1186. *
  1187. * @return a string identifying the event and its attributes
  1188. */
  1189. public String paramString() {
  1190. StringBuffer str = new StringBuffer(100);
  1191. switch (id) {
  1192. case KEY_PRESSED:
  1193. str.append("KEY_PRESSED");
  1194. break;
  1195. case KEY_RELEASED:
  1196. str.append("KEY_RELEASED");
  1197. break;
  1198. case KEY_TYPED:
  1199. str.append("KEY_TYPED");
  1200. break;
  1201. default:
  1202. str.append("unknown type");
  1203. break;
  1204. }
  1205. str.append(",keyCode=").append(keyCode);
  1206. str.append(",keyText=").append(getKeyText(keyCode));
  1207. /* Some keychars don't print well, e.g. escape, backspace,
  1208. * tab, return, delete, cancel. Get keyText for the keyCode
  1209. * instead of the keyChar.
  1210. */
  1211. str.append(",keyChar=");
  1212. switch (keyChar) {
  1213. case '\b':
  1214. str.append(getKeyText(VK_BACK_SPACE));
  1215. break;
  1216. case '\t':
  1217. str.append(getKeyText(VK_TAB));
  1218. break;
  1219. case '\n':
  1220. str.append(getKeyText(VK_ENTER));
  1221. break;
  1222. case '\u0018':
  1223. str.append(getKeyText(VK_CANCEL));
  1224. break;
  1225. case '\u001b':
  1226. str.append(getKeyText(VK_ESCAPE));
  1227. break;
  1228. case '\u007f':
  1229. str.append(getKeyText(VK_DELETE));
  1230. break;
  1231. case CHAR_UNDEFINED:
  1232. str.append(Toolkit.getProperty("AWT.undefinedKeyChar", "Undefined keyChar"));
  1233. break;
  1234. default:
  1235. str.append("'").append(keyChar).append("'");
  1236. break;
  1237. }
  1238. if (getModifiers() != 0) {
  1239. str.append(",modifiers=").append(getKeyModifiersText(modifiers));
  1240. }
  1241. if (getModifiersEx() != 0) {
  1242. str.append(",extModifiers=").append(getModifiersExText(modifiers));
  1243. }
  1244. str.append(",keyLocation=");
  1245. switch (keyLocation) {
  1246. case KEY_LOCATION_UNKNOWN:
  1247. str.append("KEY_LOCATION_UNKNOWN");
  1248. break;
  1249. case KEY_LOCATION_STANDARD:
  1250. str.append("KEY_LOCATION_STANDARD");
  1251. break;
  1252. case KEY_LOCATION_LEFT:
  1253. str.append("KEY_LOCATION_LEFT");
  1254. break;
  1255. case KEY_LOCATION_RIGHT:
  1256. str.append("KEY_LOCATION_RIGHT");
  1257. break;
  1258. case KEY_LOCATION_NUMPAD:
  1259. str.append("KEY_LOCATION_NUMPAD");
  1260. break;
  1261. default:
  1262. str.append("KEY_LOCATION_UNKNOWN");
  1263. break;
  1264. }
  1265. return str.toString();
  1266. }
  1267. /**
  1268. * Sets new modifiers by the old ones. The key modifiers
  1269. * override overlaping mouse modifiers.
  1270. */
  1271. private void setNewModifiers() {
  1272. if ((modifiers & SHIFT_MASK) != 0) {
  1273. modifiers |= SHIFT_DOWN_MASK;
  1274. }
  1275. if ((modifiers & ALT_MASK) != 0) {
  1276. modifiers |= ALT_DOWN_MASK;
  1277. }
  1278. if ((modifiers & CTRL_MASK) != 0) {
  1279. modifiers |= CTRL_DOWN_MASK;
  1280. }
  1281. if ((modifiers & META_MASK) != 0) {
  1282. modifiers |= META_DOWN_MASK;
  1283. }
  1284. if ((modifiers & ALT_GRAPH_MASK) != 0) {
  1285. modifiers |= ALT_GRAPH_DOWN_MASK;
  1286. }
  1287. if ((modifiers & BUTTON1_MASK) != 0) {
  1288. modifiers |= BUTTON1_DOWN_MASK;
  1289. }
  1290. }
  1291. /**
  1292. * Sets old modifiers by the new ones.
  1293. */
  1294. private void setOldModifiers() {
  1295. if ((modifiers & SHIFT_DOWN_MASK) != 0) {
  1296. modifiers |= SHIFT_MASK;
  1297. }
  1298. if ((modifiers & ALT_DOWN_MASK) != 0) {
  1299. modifiers |= ALT_MASK;
  1300. }
  1301. if ((modifiers & CTRL_DOWN_MASK) != 0) {
  1302. modifiers |= CTRL_MASK;
  1303. }
  1304. if ((modifiers & META_DOWN_MASK) != 0) {
  1305. modifiers |= META_MASK;
  1306. }
  1307. if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
  1308. modifiers |= ALT_GRAPH_MASK;
  1309. }
  1310. if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
  1311. modifiers |= BUTTON1_MASK;
  1312. }
  1313. }
  1314. /**
  1315. * Sets new modifiers by the old ones. The key modifiers
  1316. * override overlaping mouse modifiers.
  1317. * @serial
  1318. */
  1319. private void readObject(ObjectInputStream s)
  1320. throws IOException, ClassNotFoundException {
  1321. s.defaultReadObject();
  1322. if (getModifiers() != 0 && getModifiersEx() == 0) {
  1323. setNewModifiers();
  1324. }
  1325. }
  1326. }