1 /* 2 * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt.event; 27 28 import java.awt.Component; 29 import java.awt.GraphicsEnvironment; 30 import java.awt.Toolkit; 31 import java.io.IOException; 32 import java.io.ObjectInputStream; 33 34 import sun.awt.AWTAccessor; 35 36 /** 37 * An event which indicates that a keystroke occurred in a component. 38 * <p> 39 * This low-level event is generated by a component object (such as a text 40 * field) when a key is pressed, released, or typed. 41 * The event is passed to every {@code KeyListener} 42 * or {@code KeyAdapter} object which registered to receive such 43 * events using the component's {@code addKeyListener} method. 44 * ({@code KeyAdapter} objects implement the 45 * {@code KeyListener} interface.) Each such listener object 46 * gets this {@code KeyEvent} when the event occurs. 47 * <p> 48 * <em>"Key typed" events</em> are higher-level and generally do not depend on 49 * the platform or keyboard layout. They are generated when a Unicode character 50 * is entered, and are the preferred way to find out about character input. 51 * In the simplest case, a key typed event is produced by a single key press 52 * (e.g., 'a'). Often, however, characters are produced by series of key 53 * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to 54 * key typed events may be many-to-one or many-to-many. Key releases are not 55 * usually necessary to generate a key typed event, but there are some cases 56 * where the key typed event is not generated until a key is released (e.g., 57 * entering ASCII sequences via the Alt-Numpad method in Windows). 58 * No key typed events are generated for keys that don't generate Unicode 59 * characters (e.g., action keys, modifier keys, etc.). 60 * <p> 61 * The getKeyChar method always returns a valid Unicode character or 62 * CHAR_UNDEFINED. Character input is reported by KEY_TYPED events: 63 * KEY_PRESSED and KEY_RELEASED events are not necessarily associated 64 * with character input. Therefore, the result of the getKeyChar method 65 * is guaranteed to be meaningful only for KEY_TYPED events. 66 * <p> 67 * For key pressed and key released events, the getKeyCode method returns 68 * the event's keyCode. For key typed events, the getKeyCode method 69 * always returns {@code VK_UNDEFINED}. The {@code getExtendedKeyCode} method 70 * may also be used with many international keyboard layouts. 71 * 72 * <p> 73 * <em>"Key pressed" and "key released" events</em> are lower-level and depend 74 * on the platform and keyboard layout. They are generated whenever a key is 75 * pressed or released, and are the only way to find out about keys that don't 76 * generate character input (e.g., action keys, modifier keys, etc.). The key 77 * being pressed or released is indicated by the {@code getKeyCode} and {@code getExtendedKeyCode} 78 * methods, which return a virtual key code. 79 * 80 * <p> 81 * <em>Virtual key codes</em> are used to report which keyboard key has 82 * been pressed, rather than a character generated by the combination 83 * of one or more keystrokes (such as "A", which comes from shift and "a"). 84 * 85 * <p> 86 * For example, pressing the Shift key will cause a KEY_PRESSED event 87 * with a VK_SHIFT keyCode, while pressing the 'a' key will result in 88 * a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event 89 * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar 90 * value of 'A' is generated. 91 * 92 * <p> 93 * Pressing and releasing a key on the keyboard results in the generating 94 * the following key events (in order): 95 * <PRE> 96 * {@code KEY_PRESSED} 97 * {@code KEY_TYPED} (is only generated if a valid Unicode character could be generated.) 98 * {@code KEY_RELEASED} 99 * </PRE> 100 * 101 * But in some cases (e.g. auto-repeat or input method is activated) the order 102 * could be different (and platform dependent). 103 * 104 * <p> 105 * Notes: 106 * <ul> 107 * <li>Key combinations which do not result in Unicode characters, such as action 108 * keys like F1 and the HELP key, do not generate KEY_TYPED events. 109 * <li>Not all keyboards or systems are capable of generating all 110 * virtual key codes. No attempt is made in Java to generate these keys 111 * artificially. 112 * <li>Virtual key codes do not identify a physical key: they depend on the 113 * platform and keyboard layout. For example, the key that generates VK_Q 114 * when using a U.S. keyboard layout will generate VK_A when using a French 115 * keyboard layout. 116 * <li>The key that generates {@code VK_Q} when using a U.S. keyboard layout also 117 * generates a unique code for Russian or Hebrew layout. There is no a 118 * {@code VK_} constant for these and many other codes in various layouts. These codes 119 * may be obtained by using {@code getExtendedKeyCode} and are used whenever 120 * a {@code VK_} constant is used. 121 * <li>Not all characters have a keycode associated with them. For example, 122 * there is no keycode for the question mark because there is no keyboard 123 * for which it appears on the primary layer. 124 * <li>In order to support the platform-independent handling of action keys, 125 * the Java platform uses a few additional virtual key constants for functions 126 * that would otherwise have to be recognized by interpreting virtual key codes 127 * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES 128 * is returned instead of VK_CONVERT with the ALT modifier. 129 * <li>As specified in <a href="../doc-files/FocusSpec.html">Focus Specification</a> 130 * key events are dispatched to the focus owner by default. 131 * </ul> 132 * 133 * <p> 134 * WARNING: Aside from those keys that are defined by the Java language 135 * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ 136 * constants. The platform steward reserves the right to change these values as needed 137 * to accommodate a wider range of keyboards in the future. 138 * <p> 139 * An unspecified behavior will be caused if the {@code id} parameter 140 * of any particular {@code KeyEvent} instance is not 141 * in the range from {@code KEY_FIRST} to {@code KEY_LAST}. 142 * 143 * @author Carl Quinn 144 * @author Amy Fowler 145 * @author Norbert Lindenberg 146 * 147 * @see KeyAdapter 148 * @see KeyListener 149 * @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html">Tutorial: Writing a Key Listener</a> 150 * 151 * @since 1.1 152 */ 153 public class KeyEvent extends InputEvent { 154 155 /** 156 * Stores the state of native event dispatching system 157 * - true, if when the event was created event proxying 158 * mechanism was active 159 * - false, if it was inactive 160 * Used in Component.dispatchEventImpl to correctly dispatch 161 * events when proxy is active 162 */ 163 private boolean isProxyActive = false; 164 165 /** 166 * The first number in the range of ids used for key events. 167 */ 168 public static final int KEY_FIRST = 400; 169 170 /** 171 * The last number in the range of ids used for key events. 172 */ 173 public static final int KEY_LAST = 402; 174 175 /** 176 * The "key typed" event. This event is generated when a character is 177 * entered. In the simplest case, it is produced by a single key press. 178 * Often, however, characters are produced by series of key presses, and 179 * the mapping from key pressed events to key typed events may be 180 * many-to-one or many-to-many. 181 */ 182 public static final int KEY_TYPED = KEY_FIRST; 183 184 /** 185 * The "key pressed" event. This event is generated when a key 186 * is pushed down. 187 */ 188 public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS 189 190 /** 191 * The "key released" event. This event is generated when a key 192 * is let up. 193 */ 194 public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE 195 196 /* Virtual key codes. */ 197 198 /** Constant for the ENTER virtual key. */ 199 public static final int VK_ENTER = '\n'; 200 201 /** Constant for the BACK_SPACE virtual key. */ 202 public static final int VK_BACK_SPACE = '\b'; 203 204 /** Constant for the TAB virtual key. */ 205 public static final int VK_TAB = '\t'; 206 207 /** Constant for the CANCEL virtual key. */ 208 public static final int VK_CANCEL = 0x03; 209 210 /** Constant for the CLEAR virtual key. */ 211 public static final int VK_CLEAR = 0x0C; 212 213 /** Constant for the SHIFT virtual key. */ 214 public static final int VK_SHIFT = 0x10; 215 216 /** Constant for the CONTROL virtual key. */ 217 public static final int VK_CONTROL = 0x11; 218 219 /** Constant for the ALT virtual key. */ 220 public static final int VK_ALT = 0x12; 221 222 /** Constant for the PAUSE virtual key. */ 223 public static final int VK_PAUSE = 0x13; 224 225 /** Constant for the CAPS_LOCK virtual key. */ 226 public static final int VK_CAPS_LOCK = 0x14; 227 228 /** Constant for the ESCAPE virtual key. */ 229 public static final int VK_ESCAPE = 0x1B; 230 231 /** Constant for the SPACE virtual key. */ 232 public static final int VK_SPACE = 0x20; 233 234 /** Constant for the PAGE_UP virtual key. */ 235 public static final int VK_PAGE_UP = 0x21; 236 237 /** Constant for the PAGE_DOWN virtual key. */ 238 public static final int VK_PAGE_DOWN = 0x22; 239 240 /** Constant for the END virtual key. */ 241 public static final int VK_END = 0x23; 242 243 /** Constant for the HOME virtual key. */ 244 public static final int VK_HOME = 0x24; 245 246 /** 247 * Constant for the non-numpad <b>left</b> arrow key. 248 * @see #VK_KP_LEFT 249 */ 250 public static final int VK_LEFT = 0x25; 251 252 /** 253 * Constant for the non-numpad <b>up</b> arrow key. 254 * @see #VK_KP_UP 255 */ 256 public static final int VK_UP = 0x26; 257 258 /** 259 * Constant for the non-numpad <b>right</b> arrow key. 260 * @see #VK_KP_RIGHT 261 */ 262 public static final int VK_RIGHT = 0x27; 263 264 /** 265 * Constant for the non-numpad <b>down</b> arrow key. 266 * @see #VK_KP_DOWN 267 */ 268 public static final int VK_DOWN = 0x28; 269 270 /** 271 * Constant for the comma key, "," 272 */ 273 public static final int VK_COMMA = 0x2C; 274 275 /** 276 * Constant for the minus key, "-" 277 * @since 1.2 278 */ 279 public static final int VK_MINUS = 0x2D; 280 281 /** 282 * Constant for the period key, "." 283 */ 284 public static final int VK_PERIOD = 0x2E; 285 286 /** 287 * Constant for the forward slash key, "/" 288 */ 289 public static final int VK_SLASH = 0x2F; 290 291 /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */ 292 293 /** Constant for the "0" key. */ 294 public static final int VK_0 = 0x30; 295 296 /** Constant for the "1" key. */ 297 public static final int VK_1 = 0x31; 298 299 /** Constant for the "2" key. */ 300 public static final int VK_2 = 0x32; 301 302 /** Constant for the "3" key. */ 303 public static final int VK_3 = 0x33; 304 305 /** Constant for the "4" key. */ 306 public static final int VK_4 = 0x34; 307 308 /** Constant for the "5" key. */ 309 public static final int VK_5 = 0x35; 310 311 /** Constant for the "6" key. */ 312 public static final int VK_6 = 0x36; 313 314 /** Constant for the "7" key. */ 315 public static final int VK_7 = 0x37; 316 317 /** Constant for the "8" key. */ 318 public static final int VK_8 = 0x38; 319 320 /** Constant for the "9" key. */ 321 public static final int VK_9 = 0x39; 322 323 /** 324 * Constant for the semicolon key, ";" 325 */ 326 public static final int VK_SEMICOLON = 0x3B; 327 328 /** 329 * Constant for the equals key, "=" 330 */ 331 public static final int VK_EQUALS = 0x3D; 332 333 /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */ 334 335 /** Constant for the "A" key. */ 336 public static final int VK_A = 0x41; 337 338 /** Constant for the "B" key. */ 339 public static final int VK_B = 0x42; 340 341 /** Constant for the "C" key. */ 342 public static final int VK_C = 0x43; 343 344 /** Constant for the "D" key. */ 345 public static final int VK_D = 0x44; 346 347 /** Constant for the "E" key. */ 348 public static final int VK_E = 0x45; 349 350 /** Constant for the "F" key. */ 351 public static final int VK_F = 0x46; 352 353 /** Constant for the "G" key. */ 354 public static final int VK_G = 0x47; 355 356 /** Constant for the "H" key. */ 357 public static final int VK_H = 0x48; 358 359 /** Constant for the "I" key. */ 360 public static final int VK_I = 0x49; 361 362 /** Constant for the "J" key. */ 363 public static final int VK_J = 0x4A; 364 365 /** Constant for the "K" key. */ 366 public static final int VK_K = 0x4B; 367 368 /** Constant for the "L" key. */ 369 public static final int VK_L = 0x4C; 370 371 /** Constant for the "M" key. */ 372 public static final int VK_M = 0x4D; 373 374 /** Constant for the "N" key. */ 375 public static final int VK_N = 0x4E; 376 377 /** Constant for the "O" key. */ 378 public static final int VK_O = 0x4F; 379 380 /** Constant for the "P" key. */ 381 public static final int VK_P = 0x50; 382 383 /** Constant for the "Q" key. */ 384 public static final int VK_Q = 0x51; 385 386 /** Constant for the "R" key. */ 387 public static final int VK_R = 0x52; 388 389 /** Constant for the "S" key. */ 390 public static final int VK_S = 0x53; 391 392 /** Constant for the "T" key. */ 393 public static final int VK_T = 0x54; 394 395 /** Constant for the "U" key. */ 396 public static final int VK_U = 0x55; 397 398 /** Constant for the "V" key. */ 399 public static final int VK_V = 0x56; 400 401 /** Constant for the "W" key. */ 402 public static final int VK_W = 0x57; 403 404 /** Constant for the "X" key. */ 405 public static final int VK_X = 0x58; 406 407 /** Constant for the "Y" key. */ 408 public static final int VK_Y = 0x59; 409 410 /** Constant for the "Z" key. */ 411 public static final int VK_Z = 0x5A; 412 413 /** 414 * Constant for the open bracket key, "[" 415 */ 416 public static final int VK_OPEN_BRACKET = 0x5B; 417 418 /** 419 * Constant for the back slash key, "\" 420 */ 421 public static final int VK_BACK_SLASH = 0x5C; 422 423 /** 424 * Constant for the close bracket key, "]" 425 */ 426 public static final int VK_CLOSE_BRACKET = 0x5D; 427 428 /** Constant for the number pad "0" key. */ 429 public static final int VK_NUMPAD0 = 0x60; 430 431 /** Constant for the number pad "1" key. */ 432 public static final int VK_NUMPAD1 = 0x61; 433 434 /** Constant for the number pad "2" key. */ 435 public static final int VK_NUMPAD2 = 0x62; 436 437 /** Constant for the number pad "3" key. */ 438 public static final int VK_NUMPAD3 = 0x63; 439 440 /** Constant for the number pad "4" key. */ 441 public static final int VK_NUMPAD4 = 0x64; 442 443 /** Constant for the number pad "5" key. */ 444 public static final int VK_NUMPAD5 = 0x65; 445 446 /** Constant for the number pad "6" key. */ 447 public static final int VK_NUMPAD6 = 0x66; 448 449 /** Constant for the number pad "7" key. */ 450 public static final int VK_NUMPAD7 = 0x67; 451 452 /** Constant for the number pad "8" key. */ 453 public static final int VK_NUMPAD8 = 0x68; 454 455 /** Constant for the number pad "9" key. */ 456 public static final int VK_NUMPAD9 = 0x69; 457 458 /** Constant for the number pad multiply key. */ 459 public static final int VK_MULTIPLY = 0x6A; 460 461 /** Constant for the number pad add key. */ 462 public static final int VK_ADD = 0x6B; 463 464 /** 465 * This constant is obsolete, and is included only for backwards 466 * compatibility. 467 * @see #VK_SEPARATOR 468 */ 469 public static final int VK_SEPARATER = 0x6C; 470 471 /** 472 * Constant for the Numpad Separator key. 473 * @since 1.4 474 */ 475 public static final int VK_SEPARATOR = VK_SEPARATER; 476 477 /** Constant for the number pad subtract key. */ 478 public static final int VK_SUBTRACT = 0x6D; 479 480 /** Constant for the number pad decimal point key. */ 481 public static final int VK_DECIMAL = 0x6E; 482 483 /** Constant for the number pad divide key. */ 484 public static final int VK_DIVIDE = 0x6F; 485 486 /** Constant for the delete key. */ 487 public static final int VK_DELETE = 0x7F; /* ASCII DEL */ 488 489 /** Constant for the NUM_LOCK key. */ 490 public static final int VK_NUM_LOCK = 0x90; 491 492 /** Constant for the SCROLL_LOCK key. */ 493 public static final int VK_SCROLL_LOCK = 0x91; 494 495 /** Constant for the F1 function key. */ 496 public static final int VK_F1 = 0x70; 497 498 /** Constant for the F2 function key. */ 499 public static final int VK_F2 = 0x71; 500 501 /** Constant for the F3 function key. */ 502 public static final int VK_F3 = 0x72; 503 504 /** Constant for the F4 function key. */ 505 public static final int VK_F4 = 0x73; 506 507 /** Constant for the F5 function key. */ 508 public static final int VK_F5 = 0x74; 509 510 /** Constant for the F6 function key. */ 511 public static final int VK_F6 = 0x75; 512 513 /** Constant for the F7 function key. */ 514 public static final int VK_F7 = 0x76; 515 516 /** Constant for the F8 function key. */ 517 public static final int VK_F8 = 0x77; 518 519 /** Constant for the F9 function key. */ 520 public static final int VK_F9 = 0x78; 521 522 /** Constant for the F10 function key. */ 523 public static final int VK_F10 = 0x79; 524 525 /** Constant for the F11 function key. */ 526 public static final int VK_F11 = 0x7A; 527 528 /** Constant for the F12 function key. */ 529 public static final int VK_F12 = 0x7B; 530 531 /** 532 * Constant for the F13 function key. 533 * @since 1.2 534 */ 535 /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */ 536 public static final int VK_F13 = 0xF000; 537 538 /** 539 * Constant for the F14 function key. 540 * @since 1.2 541 */ 542 public static final int VK_F14 = 0xF001; 543 544 /** 545 * Constant for the F15 function key. 546 * @since 1.2 547 */ 548 public static final int VK_F15 = 0xF002; 549 550 /** 551 * Constant for the F16 function key. 552 * @since 1.2 553 */ 554 public static final int VK_F16 = 0xF003; 555 556 /** 557 * Constant for the F17 function key. 558 * @since 1.2 559 */ 560 public static final int VK_F17 = 0xF004; 561 562 /** 563 * Constant for the F18 function key. 564 * @since 1.2 565 */ 566 public static final int VK_F18 = 0xF005; 567 568 /** 569 * Constant for the F19 function key. 570 * @since 1.2 571 */ 572 public static final int VK_F19 = 0xF006; 573 574 /** 575 * Constant for the F20 function key. 576 * @since 1.2 577 */ 578 public static final int VK_F20 = 0xF007; 579 580 /** 581 * Constant for the F21 function key. 582 * @since 1.2 583 */ 584 public static final int VK_F21 = 0xF008; 585 586 /** 587 * Constant for the F22 function key. 588 * @since 1.2 589 */ 590 public static final int VK_F22 = 0xF009; 591 592 /** 593 * Constant for the F23 function key. 594 * @since 1.2 595 */ 596 public static final int VK_F23 = 0xF00A; 597 598 /** 599 * Constant for the F24 function key. 600 * @since 1.2 601 */ 602 public static final int VK_F24 = 0xF00B; 603 604 /** Constant for the PRINTSCREEN key. */ 605 public static final int VK_PRINTSCREEN = 0x9A; 606 607 /** Constant for the INSERT key. */ 608 public static final int VK_INSERT = 0x9B; 609 610 /** Constant for the HELP key. */ 611 public static final int VK_HELP = 0x9C; 612 613 /** Constant for the META key. */ 614 public static final int VK_META = 0x9D; 615 616 /** Constant for the BACK_QUOTE key. */ 617 public static final int VK_BACK_QUOTE = 0xC0; 618 619 /** Constant for the QUOTE key. */ 620 public static final int VK_QUOTE = 0xDE; 621 622 /** 623 * Constant for the numeric keypad <b>up</b> arrow key. 624 * @see #VK_UP 625 * @since 1.2 626 */ 627 public static final int VK_KP_UP = 0xE0; 628 629 /** 630 * Constant for the numeric keypad <b>down</b> arrow key. 631 * @see #VK_DOWN 632 * @since 1.2 633 */ 634 public static final int VK_KP_DOWN = 0xE1; 635 636 /** 637 * Constant for the numeric keypad <b>left</b> arrow key. 638 * @see #VK_LEFT 639 * @since 1.2 640 */ 641 public static final int VK_KP_LEFT = 0xE2; 642 643 /** 644 * Constant for the numeric keypad <b>right</b> arrow key. 645 * @see #VK_RIGHT 646 * @since 1.2 647 */ 648 public static final int VK_KP_RIGHT = 0xE3; 649 650 /* For European keyboards */ 651 /** @since 1.2 */ 652 public static final int VK_DEAD_GRAVE = 0x80; 653 /** @since 1.2 */ 654 public static final int VK_DEAD_ACUTE = 0x81; 655 /** @since 1.2 */ 656 public static final int VK_DEAD_CIRCUMFLEX = 0x82; 657 /** @since 1.2 */ 658 public static final int VK_DEAD_TILDE = 0x83; 659 /** @since 1.2 */ 660 public static final int VK_DEAD_MACRON = 0x84; 661 /** @since 1.2 */ 662 public static final int VK_DEAD_BREVE = 0x85; 663 /** @since 1.2 */ 664 public static final int VK_DEAD_ABOVEDOT = 0x86; 665 /** @since 1.2 */ 666 public static final int VK_DEAD_DIAERESIS = 0x87; 667 /** @since 1.2 */ 668 public static final int VK_DEAD_ABOVERING = 0x88; 669 /** @since 1.2 */ 670 public static final int VK_DEAD_DOUBLEACUTE = 0x89; 671 /** @since 1.2 */ 672 public static final int VK_DEAD_CARON = 0x8a; 673 /** @since 1.2 */ 674 public static final int VK_DEAD_CEDILLA = 0x8b; 675 /** @since 1.2 */ 676 public static final int VK_DEAD_OGONEK = 0x8c; 677 /** @since 1.2 */ 678 public static final int VK_DEAD_IOTA = 0x8d; 679 /** @since 1.2 */ 680 public static final int VK_DEAD_VOICED_SOUND = 0x8e; 681 /** @since 1.2 */ 682 public static final int VK_DEAD_SEMIVOICED_SOUND = 0x8f; 683 684 /** @since 1.2 */ 685 public static final int VK_AMPERSAND = 0x96; 686 /** @since 1.2 */ 687 public static final int VK_ASTERISK = 0x97; 688 /** @since 1.2 */ 689 public static final int VK_QUOTEDBL = 0x98; 690 /** @since 1.2 */ 691 public static final int VK_LESS = 0x99; 692 693 /** @since 1.2 */ 694 public static final int VK_GREATER = 0xa0; 695 /** @since 1.2 */ 696 public static final int VK_BRACELEFT = 0xa1; 697 /** @since 1.2 */ 698 public static final int VK_BRACERIGHT = 0xa2; 699 700 /** 701 * Constant for the "@" key. 702 * @since 1.2 703 */ 704 public static final int VK_AT = 0x0200; 705 706 /** 707 * Constant for the ":" key. 708 * @since 1.2 709 */ 710 public static final int VK_COLON = 0x0201; 711 712 /** 713 * Constant for the "^" key. 714 * @since 1.2 715 */ 716 public static final int VK_CIRCUMFLEX = 0x0202; 717 718 /** 719 * Constant for the "$" key. 720 * @since 1.2 721 */ 722 public static final int VK_DOLLAR = 0x0203; 723 724 /** 725 * Constant for the Euro currency sign key. 726 * @since 1.2 727 */ 728 public static final int VK_EURO_SIGN = 0x0204; 729 730 /** 731 * Constant for the "!" key. 732 * @since 1.2 733 */ 734 public static final int VK_EXCLAMATION_MARK = 0x0205; 735 736 /** 737 * Constant for the inverted exclamation mark key. 738 * @since 1.2 739 */ 740 public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206; 741 742 /** 743 * Constant for the "(" key. 744 * @since 1.2 745 */ 746 public static final int VK_LEFT_PARENTHESIS = 0x0207; 747 748 /** 749 * Constant for the "#" key. 750 * @since 1.2 751 */ 752 public static final int VK_NUMBER_SIGN = 0x0208; 753 754 /** 755 * Constant for the "+" key. 756 * @since 1.2 757 */ 758 public static final int VK_PLUS = 0x0209; 759 760 /** 761 * Constant for the ")" key. 762 * @since 1.2 763 */ 764 public static final int VK_RIGHT_PARENTHESIS = 0x020A; 765 766 /** 767 * Constant for the "_" key. 768 * @since 1.2 769 */ 770 public static final int VK_UNDERSCORE = 0x020B; 771 772 /** 773 * Constant for the Microsoft Windows "Windows" key. 774 * It is used for both the left and right version of the key. 775 * @see #getKeyLocation() 776 * @since 1.5 777 */ 778 public static final int VK_WINDOWS = 0x020C; 779 780 /** 781 * Constant for the Microsoft Windows Context Menu key. 782 * @since 1.5 783 */ 784 public static final int VK_CONTEXT_MENU = 0x020D; 785 786 /* for input method support on Asian Keyboards */ 787 788 /* not clear what this means - listed in Microsoft Windows API */ 789 /** Constant for the FINAL key. */ 790 public static final int VK_FINAL = 0x0018; 791 792 /** Constant for the Convert function key. */ 793 /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */ 794 public static final int VK_CONVERT = 0x001C; 795 796 /** Constant for the Don't Convert function key. */ 797 /* Japanese PC 106 keyboard: muhenkan */ 798 public static final int VK_NONCONVERT = 0x001D; 799 800 /** Constant for the Accept or Commit function key. */ 801 /* Japanese Solaris keyboard: kakutei */ 802 public static final int VK_ACCEPT = 0x001E; 803 804 /* not clear what this means - listed in Microsoft Windows API */ 805 /** Constant for the MODECHANGE key. */ 806 public static final int VK_MODECHANGE = 0x001F; 807 808 /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris; 809 might still be used on other platforms */ 810 /** 811 * Constant for the KANA lock key. 812 * @see #VK_KANA_LOCK 813 **/ 814 public static final int VK_KANA = 0x0015; 815 816 /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris; 817 might still be used for other platforms */ 818 /** 819 * Constant for KANJI. 820 * @see #VK_INPUT_METHOD_ON_OFF 821 */ 822 public static final int VK_KANJI = 0x0019; 823 824 /** 825 * Constant for the Alphanumeric function key. 826 * @since 1.2 827 */ 828 /* Japanese PC 106 keyboard: eisuu */ 829 public static final int VK_ALPHANUMERIC = 0x00F0; 830 831 /** 832 * Constant for the Katakana function key. 833 * @since 1.2 834 */ 835 /* Japanese PC 106 keyboard: katakana */ 836 public static final int VK_KATAKANA = 0x00F1; 837 838 /** 839 * Constant for the Hiragana function key. 840 * @since 1.2 841 */ 842 /* Japanese PC 106 keyboard: hiragana */ 843 public static final int VK_HIRAGANA = 0x00F2; 844 845 /** 846 * Constant for the Full-Width Characters function key. 847 * @since 1.2 848 */ 849 /* Japanese PC 106 keyboard: zenkaku */ 850 public static final int VK_FULL_WIDTH = 0x00F3; 851 852 /** 853 * Constant for the Half-Width Characters function key. 854 * @since 1.2 855 */ 856 /* Japanese PC 106 keyboard: hankaku */ 857 public static final int VK_HALF_WIDTH = 0x00F4; 858 859 /** 860 * Constant for the Roman Characters function key. 861 * @since 1.2 862 */ 863 /* Japanese PC 106 keyboard: roumaji */ 864 public static final int VK_ROMAN_CHARACTERS = 0x00F5; 865 866 /** 867 * Constant for the All Candidates function key. 868 * @since 1.2 869 */ 870 /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */ 871 public static final int VK_ALL_CANDIDATES = 0x0100; 872 873 /** 874 * Constant for the Previous Candidate function key. 875 * @since 1.2 876 */ 877 /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */ 878 public static final int VK_PREVIOUS_CANDIDATE = 0x0101; 879 880 /** 881 * Constant for the Code Input function key. 882 * @since 1.2 883 */ 884 /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */ 885 public static final int VK_CODE_INPUT = 0x0102; 886 887 /** 888 * Constant for the Japanese-Katakana function key. 889 * This key switches to a Japanese input method and selects its Katakana input mode. 890 * @since 1.2 891 */ 892 /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */ 893 public static final int VK_JAPANESE_KATAKANA = 0x0103; 894 895 /** 896 * Constant for the Japanese-Hiragana function key. 897 * This key switches to a Japanese input method and selects its Hiragana input mode. 898 * @since 1.2 899 */ 900 /* Japanese Macintosh keyboard */ 901 public static final int VK_JAPANESE_HIRAGANA = 0x0104; 902 903 /** 904 * Constant for the Japanese-Roman function key. 905 * This key switches to a Japanese input method and selects its Roman-Direct input mode. 906 * @since 1.2 907 */ 908 /* Japanese Macintosh keyboard */ 909 public static final int VK_JAPANESE_ROMAN = 0x0105; 910 911 /** 912 * Constant for the locking Kana function key. 913 * This key locks the keyboard into a Kana layout. 914 * @since 1.3 915 */ 916 /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */ 917 public static final int VK_KANA_LOCK = 0x0106; 918 919 /** 920 * Constant for the input method on/off key. 921 * @since 1.3 922 */ 923 /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */ 924 public static final int VK_INPUT_METHOD_ON_OFF = 0x0107; 925 926 /* for Sun keyboards */ 927 /** @since 1.2 */ 928 public static final int VK_CUT = 0xFFD1; 929 /** @since 1.2 */ 930 public static final int VK_COPY = 0xFFCD; 931 /** @since 1.2 */ 932 public static final int VK_PASTE = 0xFFCF; 933 /** @since 1.2 */ 934 public static final int VK_UNDO = 0xFFCB; 935 /** @since 1.2 */ 936 public static final int VK_AGAIN = 0xFFC9; 937 /** @since 1.2 */ 938 public static final int VK_FIND = 0xFFD0; 939 /** @since 1.2 */ 940 public static final int VK_PROPS = 0xFFCA; 941 /** @since 1.2 */ 942 public static final int VK_STOP = 0xFFC8; 943 944 /** 945 * Constant for the Compose function key. 946 * @since 1.2 947 */ 948 public static final int VK_COMPOSE = 0xFF20; 949 950 /** 951 * Constant for the AltGraph function key. 952 * @since 1.2 953 */ 954 public static final int VK_ALT_GRAPH = 0xFF7E; 955 956 /** 957 * Constant for the Begin key. 958 * @since 1.5 959 */ 960 public static final int VK_BEGIN = 0xFF58; 961 962 /** 963 * This value is used to indicate that the keyCode is unknown. 964 * KEY_TYPED events do not have a keyCode value; this value 965 * is used instead. 966 */ 967 public static final int VK_UNDEFINED = 0x0; 968 969 /** 970 * KEY_PRESSED and KEY_RELEASED events which do not map to a 971 * valid Unicode character use this for the keyChar value. 972 */ 973 public static final char CHAR_UNDEFINED = 0xFFFF; 974 975 /** 976 * A constant indicating that the keyLocation is indeterminate 977 * or not relevant. 978 * {@code KEY_TYPED} events do not have a keyLocation; this value 979 * is used instead. 980 * @since 1.4 981 */ 982 public static final int KEY_LOCATION_UNKNOWN = 0; 983 984 /** 985 * A constant indicating that the key pressed or released 986 * is not distinguished as the left or right version of a key, 987 * and did not originate on the numeric keypad (or did not 988 * originate with a virtual key corresponding to the numeric 989 * keypad). 990 * @since 1.4 991 */ 992 public static final int KEY_LOCATION_STANDARD = 1; 993 994 /** 995 * A constant indicating that the key pressed or released is in 996 * the left key location (there is more than one possible location 997 * for this key). Example: the left shift key. 998 * @since 1.4 999 */ 1000 public static final int KEY_LOCATION_LEFT = 2; 1001 1002 /** 1003 * A constant indicating that the key pressed or released is in 1004 * the right key location (there is more than one possible location 1005 * for this key). Example: the right shift key. 1006 * @since 1.4 1007 */ 1008 public static final int KEY_LOCATION_RIGHT = 3; 1009 1010 /** 1011 * A constant indicating that the key event originated on the 1012 * numeric keypad or with a virtual key corresponding to the 1013 * numeric keypad. 1014 * @since 1.4 1015 */ 1016 public static final int KEY_LOCATION_NUMPAD = 4; 1017 1018 /** 1019 * The unique value assigned to each of the keys on the 1020 * keyboard. There is a common set of key codes that 1021 * can be fired by most keyboards. 1022 * The symbolic name for a key code should be used rather 1023 * than the code value itself. 1024 * 1025 * @serial 1026 * @see #getKeyCode() 1027 * @see #setKeyCode(int) 1028 */ 1029 int keyCode; 1030 1031 /** 1032 * {@code keyChar} is a valid unicode character 1033 * that is fired by a key or a key combination on 1034 * a keyboard. 1035 * 1036 * @serial 1037 * @see #getKeyChar() 1038 * @see #setKeyChar(char) 1039 */ 1040 char keyChar; 1041 1042 /** 1043 * The location of the key on the keyboard. 1044 * 1045 * Some keys occur more than once on a keyboard, e.g. the left and 1046 * right shift keys. Additionally, some keys occur on the numeric 1047 * keypad. This variable is used to distinguish such keys. 1048 * 1049 * The only legal values are {@code KEY_LOCATION_UNKNOWN}, 1050 * {@code KEY_LOCATION_STANDARD}, {@code KEY_LOCATION_LEFT}, 1051 * {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}. 1052 * 1053 * @serial 1054 * @see #getKeyLocation() 1055 */ 1056 int keyLocation; 1057 1058 //set from native code. 1059 private transient long rawCode = 0; 1060 private transient long primaryLevelUnicode = 0; 1061 private transient long scancode = 0; // for MS Windows only 1062 private transient long extendedKeyCode = 0; 1063 1064 /* 1065 * JDK 1.1 serialVersionUID 1066 */ 1067 private static final long serialVersionUID = -2352130953028126954L; 1068 1069 static { 1070 /* ensure that the necessary native libraries are loaded */ NativeLibLoader.loadLibraries()1071 NativeLibLoader.loadLibraries(); 1072 if (!GraphicsEnvironment.isHeadless()) { initIDs()1073 initIDs(); 1074 } 1075 AWTAccessor.setKeyEventAccessor( new AWTAccessor.KeyEventAccessor() { public void setRawCode(KeyEvent ev, long rawCode) { ev.rawCode = rawCode; } public void setPrimaryLevelUnicode(KeyEvent ev, long primaryLevelUnicode) { ev.primaryLevelUnicode = primaryLevelUnicode; } public void setExtendedKeyCode(KeyEvent ev, long extendedKeyCode) { ev.extendedKeyCode = extendedKeyCode; } public Component getOriginalSource( KeyEvent ev ) { return ev.originalSource; } @Override public boolean isProxyActive(KeyEvent ev) { return ev.isProxyActive; } })1076 AWTAccessor.setKeyEventAccessor( 1077 new AWTAccessor.KeyEventAccessor() { 1078 public void setRawCode(KeyEvent ev, long rawCode) { 1079 ev.rawCode = rawCode; 1080 } 1081 1082 public void setPrimaryLevelUnicode(KeyEvent ev, 1083 long primaryLevelUnicode) { 1084 ev.primaryLevelUnicode = primaryLevelUnicode; 1085 } 1086 1087 public void setExtendedKeyCode(KeyEvent ev, 1088 long extendedKeyCode) { 1089 ev.extendedKeyCode = extendedKeyCode; 1090 } 1091 1092 public Component getOriginalSource( KeyEvent ev ) { 1093 return ev.originalSource; 1094 } 1095 1096 @Override 1097 public boolean isProxyActive(KeyEvent ev) { 1098 return ev.isProxyActive; 1099 } 1100 }); 1101 } 1102 1103 /** 1104 * Initialize JNI field and method IDs for fields that may be 1105 * accessed from C. 1106 */ initIDs()1107 private static native void initIDs(); 1108 1109 /** 1110 * The original event source. 1111 * 1112 * Event source can be changed during processing, but in some cases 1113 * we need to be able to obtain original source. 1114 * 1115 * @since 1.8 1116 */ 1117 private Component originalSource; 1118 KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation, boolean isProxyActive)1119 private KeyEvent(Component source, int id, long when, int modifiers, 1120 int keyCode, char keyChar, int keyLocation, boolean isProxyActive) { 1121 this(source, id, when, modifiers, keyCode, keyChar, keyLocation); 1122 this.isProxyActive = isProxyActive; 1123 } 1124 1125 /** 1126 * Constructs a {@code KeyEvent} object. 1127 * <p>This method throws an 1128 * {@code IllegalArgumentException} if {@code source} 1129 * is {@code null}. 1130 * 1131 * @param source The {@code Component} that originated the event 1132 * @param id An integer indicating the type of event. 1133 * For information on allowable values, see 1134 * the class description for {@link KeyEvent} 1135 * @param when A long integer that specifies the time the event 1136 * occurred. 1137 * Passing negative or zero value 1138 * is not recommended 1139 * @param modifiers The modifier keys down during event (shift, ctrl, 1140 * alt, meta). 1141 * Passing negative value 1142 * is not recommended. 1143 * Zero value means that no modifiers were passed. 1144 * Use either an extended _DOWN_MASK or old _MASK modifiers, 1145 * however do not mix models in the one event. 1146 * The extended modifiers are preferred for using 1147 * @param keyCode The integer code for an actual key, or VK_UNDEFINED 1148 * (for a key-typed event) 1149 * @param keyChar The Unicode character generated by this event, or 1150 * CHAR_UNDEFINED (for key-pressed and key-released 1151 * events which do not map to a valid Unicode character) 1152 * @param keyLocation Identifies the key location. The only legal 1153 * values are {@code KEY_LOCATION_UNKNOWN}, 1154 * {@code KEY_LOCATION_STANDARD}, {@code KEY_LOCATION_LEFT}, 1155 * {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}. 1156 * @throws IllegalArgumentException 1157 * if {@code id} is {@code KEY_TYPED} and 1158 * {@code keyChar} is {@code CHAR_UNDEFINED}; 1159 * or if {@code id} is {@code KEY_TYPED} and 1160 * {@code keyCode} is not {@code VK_UNDEFINED}; 1161 * or if {@code id} is {@code KEY_TYPED} and 1162 * {@code keyLocation} is not {@code KEY_LOCATION_UNKNOWN}; 1163 * or if {@code keyLocation} is not one of the legal 1164 * values enumerated above. 1165 * @throws IllegalArgumentException if {@code source} is null 1166 * @see #getSource() 1167 * @see #getID() 1168 * @see #getWhen() 1169 * @see #getModifiers() 1170 * @see #getKeyCode() 1171 * @see #getKeyChar() 1172 * @see #getKeyLocation() 1173 * @since 1.4 1174 */ 1175 @SuppressWarnings("deprecation") KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)1176 public KeyEvent(Component source, int id, long when, int modifiers, 1177 int keyCode, char keyChar, int keyLocation) { 1178 super(source, id, when, modifiers); 1179 if (id == KEY_TYPED) { 1180 if (keyChar == CHAR_UNDEFINED) { 1181 throw new IllegalArgumentException("invalid keyChar"); 1182 } 1183 if (keyCode != VK_UNDEFINED) { 1184 throw new IllegalArgumentException("invalid keyCode"); 1185 } 1186 if (keyLocation != KEY_LOCATION_UNKNOWN) { 1187 throw new IllegalArgumentException("invalid keyLocation"); 1188 } 1189 } 1190 1191 this.keyCode = keyCode; 1192 this.keyChar = keyChar; 1193 1194 if ((keyLocation < KEY_LOCATION_UNKNOWN) || 1195 (keyLocation > KEY_LOCATION_NUMPAD)) { 1196 throw new IllegalArgumentException("invalid keyLocation"); 1197 } 1198 this.keyLocation = keyLocation; 1199 if ((getModifiers() != 0) && (getModifiersEx() == 0)) { 1200 setNewModifiers(); 1201 } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) { 1202 setOldModifiers(); 1203 } 1204 originalSource = source; 1205 } 1206 1207 /** 1208 * Constructs a {@code KeyEvent} object. 1209 * <p> This method throws an 1210 * {@code IllegalArgumentException} if {@code source} 1211 * is {@code null}. 1212 * 1213 * @param source The {@code Component} that originated the event 1214 * @param id An integer indicating the type of event. 1215 * For information on allowable values, see 1216 * the class description for {@link KeyEvent} 1217 * @param when A long integer that specifies the time the event 1218 * occurred. 1219 * Passing negative or zero value 1220 * is not recommended 1221 * @param modifiers The modifier keys down during event (shift, ctrl, 1222 * alt, meta). 1223 * Passing negative value 1224 * is not recommended. 1225 * Zero value means that no modifiers were passed. 1226 * Use either an extended _DOWN_MASK or old _MASK modifiers, 1227 * however do not mix models in the one event. 1228 * The extended modifiers are preferred for using 1229 * @param keyCode The integer code for an actual key, or VK_UNDEFINED 1230 * (for a key-typed event) 1231 * @param keyChar The Unicode character generated by this event, or 1232 * CHAR_UNDEFINED (for key-pressed and key-released 1233 * events which do not map to a valid Unicode character) 1234 * @throws IllegalArgumentException if {@code id} is 1235 * {@code KEY_TYPED} and {@code keyChar} is 1236 * {@code CHAR_UNDEFINED}; or if {@code id} is 1237 * {@code KEY_TYPED} and {@code keyCode} is not 1238 * {@code VK_UNDEFINED} 1239 * @throws IllegalArgumentException if {@code source} is null 1240 * @see #getSource() 1241 * @see #getID() 1242 * @see #getWhen() 1243 * @see #getModifiers() 1244 * @see #getKeyCode() 1245 * @see #getKeyChar() 1246 */ KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)1247 public KeyEvent(Component source, int id, long when, int modifiers, 1248 int keyCode, char keyChar) { 1249 this(source, id, when, modifiers, keyCode, keyChar, 1250 KEY_LOCATION_UNKNOWN); 1251 } 1252 1253 /** 1254 * @deprecated as of JDK1.1; use {@link #KeyEvent(Component, int, long, int, int, char)} instead 1255 * @param source The {@code Component} that originated the event 1256 * @param id An integer indicating the type of event. 1257 * For information on allowable values, see 1258 * the class description for {@link KeyEvent} 1259 * @param when A long integer that specifies the time the event 1260 * occurred. 1261 * Passing negative or zero value 1262 * is not recommended 1263 * @param modifiers The modifier keys down during event (shift, ctrl, 1264 * alt, meta). 1265 * Passing negative value 1266 * is not recommended. 1267 * Zero value means that no modifiers were passed. 1268 * Use either an extended _DOWN_MASK or old _MASK modifiers, 1269 * however do not mix models in the one event. 1270 * The extended modifiers are preferred for using 1271 * @param keyCode The integer code for an actual key, or VK_UNDEFINED 1272 * (for a key-typed event) 1273 */ 1274 @Deprecated KeyEvent(Component source, int id, long when, int modifiers, int keyCode)1275 public KeyEvent(Component source, int id, long when, int modifiers, 1276 int keyCode) { 1277 this(source, id, when, modifiers, keyCode, (char)keyCode); 1278 } 1279 1280 /** 1281 * Returns the integer keyCode associated with the key in this event. 1282 * 1283 * @return the integer code for an actual key on the keyboard. 1284 * (For {@code KEY_TYPED} events, the keyCode is 1285 * {@code VK_UNDEFINED}.) 1286 */ getKeyCode()1287 public int getKeyCode() { 1288 return keyCode; 1289 } 1290 1291 /** 1292 * Set the keyCode value to indicate a physical key. 1293 * 1294 * @param keyCode an integer corresponding to an actual key on the keyboard. 1295 */ setKeyCode(int keyCode)1296 public void setKeyCode(int keyCode) { 1297 this.keyCode = keyCode; 1298 } 1299 1300 /** 1301 * Returns the character associated with the key in this event. 1302 * For example, the {@code KEY_TYPED} event for shift + "a" 1303 * returns the value for "A". 1304 * <p> 1305 * {@code KEY_PRESSED} and {@code KEY_RELEASED} events 1306 * are not intended for reporting of character input. Therefore, 1307 * the values returned by this method are guaranteed to be 1308 * meaningful only for {@code KEY_TYPED} events. 1309 * 1310 * @return the Unicode character defined for this key event. 1311 * If no valid Unicode character exists for this key event, 1312 * {@code CHAR_UNDEFINED} is returned. 1313 */ getKeyChar()1314 public char getKeyChar() { 1315 return keyChar; 1316 } 1317 1318 /** 1319 * Set the keyChar value to indicate a logical character. 1320 * 1321 * @param keyChar a char corresponding to the combination of keystrokes 1322 * that make up this event. 1323 */ setKeyChar(char keyChar)1324 public void setKeyChar(char keyChar) { 1325 this.keyChar = keyChar; 1326 } 1327 1328 /** 1329 * Set the modifiers to indicate additional keys that were held down 1330 * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent. 1331 * <p> 1332 * NOTE: use of this method is not recommended, because many AWT 1333 * implementations do not recognize modifier changes. This is 1334 * especially true for {@code KEY_TYPED} events where the shift 1335 * modifier is changed. 1336 * 1337 * @param modifiers an integer combination of the modifier constants. 1338 * @see InputEvent 1339 * @deprecated as of JDK1.1.4 1340 */ 1341 @Deprecated setModifiers(int modifiers)1342 public void setModifiers(int modifiers) { 1343 this.modifiers = modifiers; 1344 if ((getModifiers() != 0) && (getModifiersEx() == 0)) { 1345 setNewModifiers(); 1346 } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) { 1347 setOldModifiers(); 1348 } 1349 } 1350 1351 /** 1352 * Returns the location of the key that originated this key event. 1353 * 1354 * Some keys occur more than once on a keyboard, e.g. the left and 1355 * right shift keys. Additionally, some keys occur on the numeric 1356 * keypad. This provides a way of distinguishing such keys. 1357 * 1358 * @return the location of the key that was pressed or released. 1359 * Always returns {@code KEY_LOCATION_UNKNOWN} for 1360 * {@code KEY_TYPED} events. 1361 * @since 1.4 1362 */ getKeyLocation()1363 public int getKeyLocation() { 1364 return keyLocation; 1365 } 1366 1367 /** 1368 * Returns a String describing the keyCode, such as "HOME", "F1" or "A". 1369 * These strings can be localized by changing the awt.properties file. 1370 * 1371 * @param keyCode the key whose description is to be returned 1372 * @return a string containing a text description for a physical key, 1373 * identified by its keyCode 1374 */ getKeyText(int keyCode)1375 public static String getKeyText(int keyCode) { 1376 if (keyCode >= VK_0 && keyCode <= VK_9 || 1377 keyCode >= VK_A && keyCode <= VK_Z) { 1378 return String.valueOf((char)keyCode); 1379 } 1380 1381 switch(keyCode) { 1382 case VK_ENTER: return Toolkit.getProperty("AWT.enter", "Enter"); 1383 case VK_BACK_SPACE: return Toolkit.getProperty("AWT.backSpace", "Backspace"); 1384 case VK_TAB: return Toolkit.getProperty("AWT.tab", "Tab"); 1385 case VK_CANCEL: return Toolkit.getProperty("AWT.cancel", "Cancel"); 1386 case VK_CLEAR: return Toolkit.getProperty("AWT.clear", "Clear"); 1387 case VK_COMPOSE: return Toolkit.getProperty("AWT.compose", "Compose"); 1388 case VK_PAUSE: return Toolkit.getProperty("AWT.pause", "Pause"); 1389 case VK_CAPS_LOCK: return Toolkit.getProperty("AWT.capsLock", "Caps Lock"); 1390 case VK_ESCAPE: return Toolkit.getProperty("AWT.escape", "Escape"); 1391 case VK_SPACE: return Toolkit.getProperty("AWT.space", "Space"); 1392 case VK_PAGE_UP: return Toolkit.getProperty("AWT.pgup", "Page Up"); 1393 case VK_PAGE_DOWN: return Toolkit.getProperty("AWT.pgdn", "Page Down"); 1394 case VK_END: return Toolkit.getProperty("AWT.end", "End"); 1395 case VK_HOME: return Toolkit.getProperty("AWT.home", "Home"); 1396 case VK_LEFT: return Toolkit.getProperty("AWT.left", "Left"); 1397 case VK_UP: return Toolkit.getProperty("AWT.up", "Up"); 1398 case VK_RIGHT: return Toolkit.getProperty("AWT.right", "Right"); 1399 case VK_DOWN: return Toolkit.getProperty("AWT.down", "Down"); 1400 case VK_BEGIN: return Toolkit.getProperty("AWT.begin", "Begin"); 1401 1402 // modifiers 1403 case VK_SHIFT: return Toolkit.getProperty("AWT.shift", "Shift"); 1404 case VK_CONTROL: return Toolkit.getProperty("AWT.control", "Control"); 1405 case VK_ALT: return Toolkit.getProperty("AWT.alt", "Alt"); 1406 case VK_META: return Toolkit.getProperty("AWT.meta", "Meta"); 1407 case VK_ALT_GRAPH: return Toolkit.getProperty("AWT.altGraph", "Alt Graph"); 1408 1409 // punctuation 1410 case VK_COMMA: return Toolkit.getProperty("AWT.comma", "Comma"); 1411 case VK_PERIOD: return Toolkit.getProperty("AWT.period", "Period"); 1412 case VK_SLASH: return Toolkit.getProperty("AWT.slash", "Slash"); 1413 case VK_SEMICOLON: return Toolkit.getProperty("AWT.semicolon", "Semicolon"); 1414 case VK_EQUALS: return Toolkit.getProperty("AWT.equals", "Equals"); 1415 case VK_OPEN_BRACKET: return Toolkit.getProperty("AWT.openBracket", "Open Bracket"); 1416 case VK_BACK_SLASH: return Toolkit.getProperty("AWT.backSlash", "Back Slash"); 1417 case VK_CLOSE_BRACKET: return Toolkit.getProperty("AWT.closeBracket", "Close Bracket"); 1418 1419 // numpad numeric keys handled below 1420 case VK_MULTIPLY: return Toolkit.getProperty("AWT.multiply", "NumPad *"); 1421 case VK_ADD: return Toolkit.getProperty("AWT.add", "NumPad +"); 1422 case VK_SEPARATOR: return Toolkit.getProperty("AWT.separator", "NumPad ,"); 1423 case VK_SUBTRACT: return Toolkit.getProperty("AWT.subtract", "NumPad -"); 1424 case VK_DECIMAL: return Toolkit.getProperty("AWT.decimal", "NumPad ."); 1425 case VK_DIVIDE: return Toolkit.getProperty("AWT.divide", "NumPad /"); 1426 case VK_DELETE: return Toolkit.getProperty("AWT.delete", "Delete"); 1427 case VK_NUM_LOCK: return Toolkit.getProperty("AWT.numLock", "Num Lock"); 1428 case VK_SCROLL_LOCK: return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock"); 1429 1430 case VK_WINDOWS: return Toolkit.getProperty("AWT.windows", "Windows"); 1431 case VK_CONTEXT_MENU: return Toolkit.getProperty("AWT.context", "Context Menu"); 1432 1433 case VK_F1: return Toolkit.getProperty("AWT.f1", "F1"); 1434 case VK_F2: return Toolkit.getProperty("AWT.f2", "F2"); 1435 case VK_F3: return Toolkit.getProperty("AWT.f3", "F3"); 1436 case VK_F4: return Toolkit.getProperty("AWT.f4", "F4"); 1437 case VK_F5: return Toolkit.getProperty("AWT.f5", "F5"); 1438 case VK_F6: return Toolkit.getProperty("AWT.f6", "F6"); 1439 case VK_F7: return Toolkit.getProperty("AWT.f7", "F7"); 1440 case VK_F8: return Toolkit.getProperty("AWT.f8", "F8"); 1441 case VK_F9: return Toolkit.getProperty("AWT.f9", "F9"); 1442 case VK_F10: return Toolkit.getProperty("AWT.f10", "F10"); 1443 case VK_F11: return Toolkit.getProperty("AWT.f11", "F11"); 1444 case VK_F12: return Toolkit.getProperty("AWT.f12", "F12"); 1445 case VK_F13: return Toolkit.getProperty("AWT.f13", "F13"); 1446 case VK_F14: return Toolkit.getProperty("AWT.f14", "F14"); 1447 case VK_F15: return Toolkit.getProperty("AWT.f15", "F15"); 1448 case VK_F16: return Toolkit.getProperty("AWT.f16", "F16"); 1449 case VK_F17: return Toolkit.getProperty("AWT.f17", "F17"); 1450 case VK_F18: return Toolkit.getProperty("AWT.f18", "F18"); 1451 case VK_F19: return Toolkit.getProperty("AWT.f19", "F19"); 1452 case VK_F20: return Toolkit.getProperty("AWT.f20", "F20"); 1453 case VK_F21: return Toolkit.getProperty("AWT.f21", "F21"); 1454 case VK_F22: return Toolkit.getProperty("AWT.f22", "F22"); 1455 case VK_F23: return Toolkit.getProperty("AWT.f23", "F23"); 1456 case VK_F24: return Toolkit.getProperty("AWT.f24", "F24"); 1457 1458 case VK_PRINTSCREEN: return Toolkit.getProperty("AWT.printScreen", "Print Screen"); 1459 case VK_INSERT: return Toolkit.getProperty("AWT.insert", "Insert"); 1460 case VK_HELP: return Toolkit.getProperty("AWT.help", "Help"); 1461 case VK_BACK_QUOTE: return Toolkit.getProperty("AWT.backQuote", "Back Quote"); 1462 case VK_QUOTE: return Toolkit.getProperty("AWT.quote", "Quote"); 1463 1464 case VK_KP_UP: return Toolkit.getProperty("AWT.up", "Up"); 1465 case VK_KP_DOWN: return Toolkit.getProperty("AWT.down", "Down"); 1466 case VK_KP_LEFT: return Toolkit.getProperty("AWT.left", "Left"); 1467 case VK_KP_RIGHT: return Toolkit.getProperty("AWT.right", "Right"); 1468 1469 case VK_DEAD_GRAVE: return Toolkit.getProperty("AWT.deadGrave", "Dead Grave"); 1470 case VK_DEAD_ACUTE: return Toolkit.getProperty("AWT.deadAcute", "Dead Acute"); 1471 case VK_DEAD_CIRCUMFLEX: return Toolkit.getProperty("AWT.deadCircumflex", "Dead Circumflex"); 1472 case VK_DEAD_TILDE: return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde"); 1473 case VK_DEAD_MACRON: return Toolkit.getProperty("AWT.deadMacron", "Dead Macron"); 1474 case VK_DEAD_BREVE: return Toolkit.getProperty("AWT.deadBreve", "Dead Breve"); 1475 case VK_DEAD_ABOVEDOT: return Toolkit.getProperty("AWT.deadAboveDot", "Dead Above Dot"); 1476 case VK_DEAD_DIAERESIS: return Toolkit.getProperty("AWT.deadDiaeresis", "Dead Diaeresis"); 1477 case VK_DEAD_ABOVERING: return Toolkit.getProperty("AWT.deadAboveRing", "Dead Above Ring"); 1478 case VK_DEAD_DOUBLEACUTE: return Toolkit.getProperty("AWT.deadDoubleAcute", "Dead Double Acute"); 1479 case VK_DEAD_CARON: return Toolkit.getProperty("AWT.deadCaron", "Dead Caron"); 1480 case VK_DEAD_CEDILLA: return Toolkit.getProperty("AWT.deadCedilla", "Dead Cedilla"); 1481 case VK_DEAD_OGONEK: return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek"); 1482 case VK_DEAD_IOTA: return Toolkit.getProperty("AWT.deadIota", "Dead Iota"); 1483 case VK_DEAD_VOICED_SOUND: return Toolkit.getProperty("AWT.deadVoicedSound", "Dead Voiced Sound"); 1484 case VK_DEAD_SEMIVOICED_SOUND: return Toolkit.getProperty("AWT.deadSemivoicedSound", "Dead Semivoiced Sound"); 1485 1486 case VK_AMPERSAND: return Toolkit.getProperty("AWT.ampersand", "Ampersand"); 1487 case VK_ASTERISK: return Toolkit.getProperty("AWT.asterisk", "Asterisk"); 1488 case VK_QUOTEDBL: return Toolkit.getProperty("AWT.quoteDbl", "Double Quote"); 1489 case VK_LESS: return Toolkit.getProperty("AWT.Less", "Less"); 1490 case VK_GREATER: return Toolkit.getProperty("AWT.greater", "Greater"); 1491 case VK_BRACELEFT: return Toolkit.getProperty("AWT.braceLeft", "Left Brace"); 1492 case VK_BRACERIGHT: return Toolkit.getProperty("AWT.braceRight", "Right Brace"); 1493 case VK_AT: return Toolkit.getProperty("AWT.at", "At"); 1494 case VK_COLON: return Toolkit.getProperty("AWT.colon", "Colon"); 1495 case VK_CIRCUMFLEX: return Toolkit.getProperty("AWT.circumflex", "Circumflex"); 1496 case VK_DOLLAR: return Toolkit.getProperty("AWT.dollar", "Dollar"); 1497 case VK_EURO_SIGN: return Toolkit.getProperty("AWT.euro", "Euro"); 1498 case VK_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.exclamationMark", "Exclamation Mark"); 1499 case VK_INVERTED_EXCLAMATION_MARK: return Toolkit.getProperty("AWT.invertedExclamationMark", "Inverted Exclamation Mark"); 1500 case VK_LEFT_PARENTHESIS: return Toolkit.getProperty("AWT.leftParenthesis", "Left Parenthesis"); 1501 case VK_NUMBER_SIGN: return Toolkit.getProperty("AWT.numberSign", "Number Sign"); 1502 case VK_MINUS: return Toolkit.getProperty("AWT.minus", "Minus"); 1503 case VK_PLUS: return Toolkit.getProperty("AWT.plus", "Plus"); 1504 case VK_RIGHT_PARENTHESIS: return Toolkit.getProperty("AWT.rightParenthesis", "Right Parenthesis"); 1505 case VK_UNDERSCORE: return Toolkit.getProperty("AWT.underscore", "Underscore"); 1506 1507 case VK_FINAL: return Toolkit.getProperty("AWT.final", "Final"); 1508 case VK_CONVERT: return Toolkit.getProperty("AWT.convert", "Convert"); 1509 case VK_NONCONVERT: return Toolkit.getProperty("AWT.noconvert", "No Convert"); 1510 case VK_ACCEPT: return Toolkit.getProperty("AWT.accept", "Accept"); 1511 case VK_MODECHANGE: return Toolkit.getProperty("AWT.modechange", "Mode Change"); 1512 case VK_KANA: return Toolkit.getProperty("AWT.kana", "Kana"); 1513 case VK_KANJI: return Toolkit.getProperty("AWT.kanji", "Kanji"); 1514 case VK_ALPHANUMERIC: return Toolkit.getProperty("AWT.alphanumeric", "Alphanumeric"); 1515 case VK_KATAKANA: return Toolkit.getProperty("AWT.katakana", "Katakana"); 1516 case VK_HIRAGANA: return Toolkit.getProperty("AWT.hiragana", "Hiragana"); 1517 case VK_FULL_WIDTH: return Toolkit.getProperty("AWT.fullWidth", "Full-Width"); 1518 case VK_HALF_WIDTH: return Toolkit.getProperty("AWT.halfWidth", "Half-Width"); 1519 case VK_ROMAN_CHARACTERS: return Toolkit.getProperty("AWT.romanCharacters", "Roman Characters"); 1520 case VK_ALL_CANDIDATES: return Toolkit.getProperty("AWT.allCandidates", "All Candidates"); 1521 case VK_PREVIOUS_CANDIDATE: return Toolkit.getProperty("AWT.previousCandidate", "Previous Candidate"); 1522 case VK_CODE_INPUT: return Toolkit.getProperty("AWT.codeInput", "Code Input"); 1523 case VK_JAPANESE_KATAKANA: return Toolkit.getProperty("AWT.japaneseKatakana", "Japanese Katakana"); 1524 case VK_JAPANESE_HIRAGANA: return Toolkit.getProperty("AWT.japaneseHiragana", "Japanese Hiragana"); 1525 case VK_JAPANESE_ROMAN: return Toolkit.getProperty("AWT.japaneseRoman", "Japanese Roman"); 1526 case VK_KANA_LOCK: return Toolkit.getProperty("AWT.kanaLock", "Kana Lock"); 1527 case VK_INPUT_METHOD_ON_OFF: return Toolkit.getProperty("AWT.inputMethodOnOff", "Input Method On/Off"); 1528 1529 case VK_AGAIN: return Toolkit.getProperty("AWT.again", "Again"); 1530 case VK_UNDO: return Toolkit.getProperty("AWT.undo", "Undo"); 1531 case VK_COPY: return Toolkit.getProperty("AWT.copy", "Copy"); 1532 case VK_PASTE: return Toolkit.getProperty("AWT.paste", "Paste"); 1533 case VK_CUT: return Toolkit.getProperty("AWT.cut", "Cut"); 1534 case VK_FIND: return Toolkit.getProperty("AWT.find", "Find"); 1535 case VK_PROPS: return Toolkit.getProperty("AWT.props", "Props"); 1536 case VK_STOP: return Toolkit.getProperty("AWT.stop", "Stop"); 1537 } 1538 1539 if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) { 1540 String numpad = Toolkit.getProperty("AWT.numpad", "NumPad"); 1541 char c = (char)(keyCode - VK_NUMPAD0 + '0'); 1542 return numpad + "-" + c; 1543 } 1544 1545 if ((keyCode & 0x01000000) != 0) { 1546 return String.valueOf((char)(keyCode ^ 0x01000000 )); 1547 } 1548 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown"); 1549 return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16); 1550 } 1551 1552 /** 1553 * Returns a {@code String} describing the modifier key(s), 1554 * such as "Shift", or "Ctrl+Shift". These strings can be 1555 * localized by changing the {@code awt.properties} file. 1556 * <p> 1557 * Note that {@code InputEvent.ALT_MASK} and 1558 * {@code InputEvent.BUTTON2_MASK} have the same value, 1559 * so the string "Alt" is returned for both modifiers. Likewise, 1560 * {@code InputEvent.META_MASK} and 1561 * {@code InputEvent.BUTTON3_MASK} have the same value, 1562 * so the string "Meta" is returned for both modifiers. 1563 * 1564 * @param modifiers the modifier mask to be processed 1565 * @return string a text description of the combination of modifier 1566 * keys that were held down during the event 1567 * @see InputEvent#getModifiersExText(int) 1568 * @deprecated It is recommended that extended modifier keys and 1569 * {@link InputEvent#getModifiersExText(int)} be used instead 1570 */ 1571 @Deprecated(since = "9") getKeyModifiersText(int modifiers)1572 public static String getKeyModifiersText(int modifiers) { 1573 StringBuilder buf = new StringBuilder(); 1574 if ((modifiers & InputEvent.META_MASK) != 0) { 1575 buf.append(Toolkit.getProperty("AWT.meta", "Meta")); 1576 buf.append("+"); 1577 } 1578 if ((modifiers & InputEvent.CTRL_MASK) != 0) { 1579 buf.append(Toolkit.getProperty("AWT.control", "Ctrl")); 1580 buf.append("+"); 1581 } 1582 if ((modifiers & InputEvent.ALT_MASK) != 0) { 1583 buf.append(Toolkit.getProperty("AWT.alt", "Alt")); 1584 buf.append("+"); 1585 } 1586 if ((modifiers & InputEvent.SHIFT_MASK) != 0) { 1587 buf.append(Toolkit.getProperty("AWT.shift", "Shift")); 1588 buf.append("+"); 1589 } 1590 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { 1591 buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph")); 1592 buf.append("+"); 1593 } 1594 if ((modifiers & InputEvent.BUTTON1_MASK) != 0) { 1595 buf.append(Toolkit.getProperty("AWT.button1", "Button1")); 1596 buf.append("+"); 1597 } 1598 if (buf.length() > 0) { 1599 buf.setLength(buf.length()-1); // remove trailing '+' 1600 } 1601 return buf.toString(); 1602 } 1603 1604 1605 /** 1606 * Returns whether the key in this event is an "action" key. 1607 * Typically an action key does not fire a unicode character and is 1608 * not a modifier key. 1609 * 1610 * @return {@code true} if the key is an "action" key, 1611 * {@code false} otherwise 1612 */ isActionKey()1613 public boolean isActionKey() { 1614 switch (keyCode) { 1615 case VK_HOME: 1616 case VK_END: 1617 case VK_PAGE_UP: 1618 case VK_PAGE_DOWN: 1619 case VK_UP: 1620 case VK_DOWN: 1621 case VK_LEFT: 1622 case VK_RIGHT: 1623 case VK_BEGIN: 1624 1625 case VK_KP_LEFT: 1626 case VK_KP_UP: 1627 case VK_KP_RIGHT: 1628 case VK_KP_DOWN: 1629 1630 case VK_F1: 1631 case VK_F2: 1632 case VK_F3: 1633 case VK_F4: 1634 case VK_F5: 1635 case VK_F6: 1636 case VK_F7: 1637 case VK_F8: 1638 case VK_F9: 1639 case VK_F10: 1640 case VK_F11: 1641 case VK_F12: 1642 case VK_F13: 1643 case VK_F14: 1644 case VK_F15: 1645 case VK_F16: 1646 case VK_F17: 1647 case VK_F18: 1648 case VK_F19: 1649 case VK_F20: 1650 case VK_F21: 1651 case VK_F22: 1652 case VK_F23: 1653 case VK_F24: 1654 case VK_PRINTSCREEN: 1655 case VK_SCROLL_LOCK: 1656 case VK_CAPS_LOCK: 1657 case VK_NUM_LOCK: 1658 case VK_PAUSE: 1659 case VK_INSERT: 1660 1661 case VK_FINAL: 1662 case VK_CONVERT: 1663 case VK_NONCONVERT: 1664 case VK_ACCEPT: 1665 case VK_MODECHANGE: 1666 case VK_KANA: 1667 case VK_KANJI: 1668 case VK_ALPHANUMERIC: 1669 case VK_KATAKANA: 1670 case VK_HIRAGANA: 1671 case VK_FULL_WIDTH: 1672 case VK_HALF_WIDTH: 1673 case VK_ROMAN_CHARACTERS: 1674 case VK_ALL_CANDIDATES: 1675 case VK_PREVIOUS_CANDIDATE: 1676 case VK_CODE_INPUT: 1677 case VK_JAPANESE_KATAKANA: 1678 case VK_JAPANESE_HIRAGANA: 1679 case VK_JAPANESE_ROMAN: 1680 case VK_KANA_LOCK: 1681 case VK_INPUT_METHOD_ON_OFF: 1682 1683 case VK_AGAIN: 1684 case VK_UNDO: 1685 case VK_COPY: 1686 case VK_PASTE: 1687 case VK_CUT: 1688 case VK_FIND: 1689 case VK_PROPS: 1690 case VK_STOP: 1691 1692 case VK_HELP: 1693 case VK_WINDOWS: 1694 case VK_CONTEXT_MENU: 1695 return true; 1696 } 1697 return false; 1698 } 1699 1700 /** 1701 * Returns a parameter string identifying this event. 1702 * This method is useful for event logging and for debugging. 1703 * 1704 * @return a string identifying the event and its attributes 1705 */ 1706 @SuppressWarnings("deprecation") paramString()1707 public String paramString() { 1708 StringBuilder str = new StringBuilder(100); 1709 1710 switch (id) { 1711 case KEY_PRESSED: 1712 str.append("KEY_PRESSED"); 1713 break; 1714 case KEY_RELEASED: 1715 str.append("KEY_RELEASED"); 1716 break; 1717 case KEY_TYPED: 1718 str.append("KEY_TYPED"); 1719 break; 1720 default: 1721 str.append("unknown type"); 1722 break; 1723 } 1724 1725 str.append(",keyCode=").append(keyCode); 1726 str.append(",keyText=").append(getKeyText(keyCode)); 1727 1728 /* Some keychars don't print well, e.g. escape, backspace, 1729 * tab, return, delete, cancel. Get keyText for the keyCode 1730 * instead of the keyChar. 1731 */ 1732 str.append(",keyChar="); 1733 switch (keyChar) { 1734 case '\b': 1735 str.append(getKeyText(VK_BACK_SPACE)); 1736 break; 1737 case '\t': 1738 str.append(getKeyText(VK_TAB)); 1739 break; 1740 case '\n': 1741 str.append(getKeyText(VK_ENTER)); 1742 break; 1743 case '\u0018': 1744 str.append(getKeyText(VK_CANCEL)); 1745 break; 1746 case '\u001b': 1747 str.append(getKeyText(VK_ESCAPE)); 1748 break; 1749 case '\u007f': 1750 str.append(getKeyText(VK_DELETE)); 1751 break; 1752 case CHAR_UNDEFINED: 1753 str.append(Toolkit.getProperty("AWT.undefined", "Undefined")); 1754 str.append(" keyChar"); 1755 break; 1756 default: 1757 str.append("'").append(keyChar).append("'"); 1758 break; 1759 } 1760 1761 if (getModifiers() != 0) { 1762 str.append(",modifiers=").append(getKeyModifiersText(modifiers)); 1763 } 1764 if (getModifiersEx() != 0) { 1765 str.append(",extModifiers=").append(getModifiersExText(modifiers)); 1766 } 1767 1768 str.append(",keyLocation="); 1769 switch (keyLocation) { 1770 case KEY_LOCATION_UNKNOWN: 1771 str.append("KEY_LOCATION_UNKNOWN"); 1772 break; 1773 case KEY_LOCATION_STANDARD: 1774 str.append("KEY_LOCATION_STANDARD"); 1775 break; 1776 case KEY_LOCATION_LEFT: 1777 str.append("KEY_LOCATION_LEFT"); 1778 break; 1779 case KEY_LOCATION_RIGHT: 1780 str.append("KEY_LOCATION_RIGHT"); 1781 break; 1782 case KEY_LOCATION_NUMPAD: 1783 str.append("KEY_LOCATION_NUMPAD"); 1784 break; 1785 default: 1786 str.append("KEY_LOCATION_UNKNOWN"); 1787 break; 1788 } 1789 str.append(",rawCode=").append(rawCode); 1790 str.append(",primaryLevelUnicode=").append(primaryLevelUnicode); 1791 str.append(",scancode=").append(scancode); 1792 str.append(",extendedKeyCode=0x").append(Long.toHexString(extendedKeyCode)); 1793 1794 return str.toString(); 1795 } 1796 /** 1797 * Returns an extended key code for the event. 1798 * The extended key code is a unique id assigned to a key on the keyboard 1799 * just like {@code keyCode}. However, unlike {@code keyCode}, this value depends on the 1800 * current keyboard layout. For instance, pressing the left topmost letter key 1801 * in a common English layout produces the same value as {@code keyCode}, {@code VK_Q}. 1802 * Pressing the same key in a regular Russian layout gives another code, unique for the 1803 * letter "Cyrillic I short". 1804 * 1805 * @return an extended key code for the event 1806 * @since 1.7 1807 */ getExtendedKeyCode()1808 public int getExtendedKeyCode() { 1809 return (int)extendedKeyCode; 1810 } 1811 /** 1812 * Returns an extended key code for a unicode character. 1813 * 1814 * @param c the unicode character to be processed 1815 * @return for a unicode character with a corresponding {@code VK_} constant -- this 1816 * {@code VK_} constant; for a character appearing on the primary 1817 * level of a known keyboard layout -- a unique integer. 1818 * If a character does not appear on the primary level of a known keyboard, 1819 * {@code VK_UNDEFINED} is returned. 1820 * 1821 * @since 1.7 1822 */ getExtendedKeyCodeForChar(int c)1823 public static int getExtendedKeyCodeForChar(int c) { 1824 // Return a keycode (if any) associated with a character. 1825 return sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(c); 1826 } 1827 1828 /** 1829 * Sets new modifiers by the old ones. The key modifiers 1830 * override overlapping mouse modifiers. 1831 */ 1832 @SuppressWarnings("deprecation") setNewModifiers()1833 private void setNewModifiers() { 1834 if ((modifiers & SHIFT_MASK) != 0) { 1835 modifiers |= SHIFT_DOWN_MASK; 1836 } 1837 if ((modifiers & ALT_MASK) != 0) { 1838 modifiers |= ALT_DOWN_MASK; 1839 } 1840 if ((modifiers & CTRL_MASK) != 0) { 1841 modifiers |= CTRL_DOWN_MASK; 1842 } 1843 if ((modifiers & META_MASK) != 0) { 1844 modifiers |= META_DOWN_MASK; 1845 } 1846 if ((modifiers & ALT_GRAPH_MASK) != 0) { 1847 modifiers |= ALT_GRAPH_DOWN_MASK; 1848 } 1849 if ((modifiers & BUTTON1_MASK) != 0) { 1850 modifiers |= BUTTON1_DOWN_MASK; 1851 } 1852 } 1853 1854 /** 1855 * Sets old modifiers by the new ones. 1856 */ 1857 @SuppressWarnings("deprecation") setOldModifiers()1858 private void setOldModifiers() { 1859 if ((modifiers & SHIFT_DOWN_MASK) != 0) { 1860 modifiers |= SHIFT_MASK; 1861 } 1862 if ((modifiers & ALT_DOWN_MASK) != 0) { 1863 modifiers |= ALT_MASK; 1864 } 1865 if ((modifiers & CTRL_DOWN_MASK) != 0) { 1866 modifiers |= CTRL_MASK; 1867 } 1868 if ((modifiers & META_DOWN_MASK) != 0) { 1869 modifiers |= META_MASK; 1870 } 1871 if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) { 1872 modifiers |= ALT_GRAPH_MASK; 1873 } 1874 if ((modifiers & BUTTON1_DOWN_MASK) != 0) { 1875 modifiers |= BUTTON1_MASK; 1876 } 1877 } 1878 1879 /** 1880 * Sets new modifiers by the old ones. The key modifiers 1881 * override overlapping mouse modifiers. 1882 * 1883 * @param s the {@code ObjectInputStream} to read 1884 * @throws ClassNotFoundException if the class of a serialized object could 1885 * not be found 1886 * @throws IOException if an I/O error occurs 1887 * @serial 1888 */ 1889 @SuppressWarnings("deprecation") readObject(ObjectInputStream s)1890 private void readObject(ObjectInputStream s) 1891 throws IOException, ClassNotFoundException { 1892 s.defaultReadObject(); 1893 if (getModifiers() != 0 && getModifiersEx() == 0) { 1894 setNewModifiers(); 1895 } 1896 } 1897 } 1898