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