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