0001 /*
0002 * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
0003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0004 *
0005 * This code is free software; you can redistribute it and/or modify it
0006 * under the terms of the GNU General Public License version 2 only, as
0007 * published by the Free Software Foundation. Sun designates this
0008 * particular file as subject to the "Classpath" exception as provided
0009 * by Sun in the LICENSE file that accompanied this code.
0010 *
0011 * This code is distributed in the hope that it will be useful, but WITHOUT
0012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014 * version 2 for more details (a copy is included in the LICENSE file that
0015 * accompanied this code).
0016 *
0017 * You should have received a copy of the GNU General Public License version
0018 * 2 along with this work; if not, write to the Free Software Foundation,
0019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0020 *
0021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0022 * CA 95054 USA or visit www.sun.com if you need additional information or
0023 * have any questions.
0024 */
0025
0026 package java.awt.event;
0027
0028 import java.awt.Event;
0029 import java.awt.Component;
0030 import java.awt.GraphicsEnvironment;
0031 import java.awt.Toolkit;
0032 import java.io.IOException;
0033 import java.io.ObjectInputStream;
0034
0035 /**
0036 * An event which indicates that a keystroke occurred in a component.
0037 * <p>
0038 * This low-level event is generated by a component object (such as a text
0039 * field) when a key is pressed, released, or typed.
0040 * The event is passed to every <code>KeyListener</code>
0041 * or <code>KeyAdapter</code> object which registered to receive such
0042 * events using the component's <code>addKeyListener</code> method.
0043 * (<code>KeyAdapter</code> objects implement the
0044 * <code>KeyListener</code> interface.) Each such listener object
0045 * gets this <code>KeyEvent</code> when the event occurs.
0046 * <p>
0047 * <em>"Key typed" events</em> are higher-level and generally do not depend on
0048 * the platform or keyboard layout. They are generated when a Unicode character
0049 * is entered, and are the preferred way to find out about character input.
0050 * In the simplest case, a key typed event is produced by a single key press
0051 * (e.g., 'a'). Often, however, characters are produced by series of key
0052 * presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to
0053 * key typed events may be many-to-one or many-to-many. Key releases are not
0054 * usually necessary to generate a key typed event, but there are some cases
0055 * where the key typed event is not generated until a key is released (e.g.,
0056 * entering ASCII sequences via the Alt-Numpad method in Windows).
0057 * No key typed events are generated for keys that don't generate Unicode
0058 * characters (e.g., action keys, modifier keys, etc.).
0059 * <p>
0060 * The getKeyChar method always returns a valid Unicode character or
0061 * CHAR_UNDEFINED. Character input is reported by KEY_TYPED events:
0062 * KEY_PRESSED and KEY_RELEASED events are not necessarily associated
0063 * with character input. Therefore, the result of the getKeyChar method
0064 * is guaranteed to be meaningful only for KEY_TYPED events.
0065 * <p>
0066 * For key pressed and key released events, the getKeyCode method returns
0067 * the event's keyCode. For key typed events, the getKeyCode method
0068 * always returns VK_UNDEFINED.
0069 *
0070 * <p>
0071 * <em>"Key pressed" and "key released" events</em> are lower-level and depend
0072 * on the platform and keyboard layout. They are generated whenever a key is
0073 * pressed or released, and are the only way to find out about keys that don't
0074 * generate character input (e.g., action keys, modifier keys, etc.). The key
0075 * being pressed or released is indicated by the getKeyCode method, which returns
0076 * a virtual key code.
0077 *
0078 * <p>
0079 * <em>Virtual key codes</em> are used to report which keyboard key has
0080 * been pressed, rather than a character generated by the combination
0081 * of one or more keystrokes (such as "A", which comes from shift and "a").
0082 *
0083 * <p>
0084 * For example, pressing the Shift key will cause a KEY_PRESSED event
0085 * with a VK_SHIFT keyCode, while pressing the 'a' key will result in
0086 * a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event
0087 * will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar
0088 * value of 'A' is generated.
0089 *
0090 * <p>
0091 * Pressing and releasing a key on the keyboard results in the generating
0092 * the following key events (in order):
0093 * <PRE>
0094 * {@code KEY_PRESSED}
0095 * {@code KEY_TYPED} (is only generated if a valid Unicode character could be generated.)
0096 * {@code KEY_RELEASED}
0097 * </PRE>
0098 *
0099 * But in some cases (e.g. auto-repeat or input method is activated) the order
0100 * could be different (and platform dependent).
0101 *
0102 * <p>
0103 * Notes:
0104 * <ul>
0105 * <li>Key combinations which do not result in Unicode characters, such as action
0106 * keys like F1 and the HELP key, do not generate KEY_TYPED events.
0107 * <li>Not all keyboards or systems are capable of generating all
0108 * virtual key codes. No attempt is made in Java to generate these keys
0109 * artificially.
0110 * <li>Virtual key codes do not identify a physical key: they depend on the
0111 * platform and keyboard layout. For example, the key that generates VK_Q
0112 * when using a U.S. keyboard layout will generate VK_A when using a French
0113 * keyboard layout.
0114 * <li>Not all characters have a keycode associated with them. For example,
0115 * there is no keycode for the question mark because there is no keyboard
0116 * for which it appears on the primary layer.
0117 * <li>In order to support the platform-independent handling of action keys,
0118 * the Java platform uses a few additional virtual key constants for functions
0119 * that would otherwise have to be recognized by interpreting virtual key codes
0120 * and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES
0121 * is returned instead of VK_CONVERT with the ALT modifier.
0122 * <li>As specified in <a href="../doc-files/FocusSpec.html">Focus Specification</a>
0123 * key events are dispatched to the focus owner by default.
0124 * </ul>
0125 *
0126 * <p>
0127 * WARNING: Aside from those keys that are defined by the Java language
0128 * (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_
0129 * constants. Sun reserves the right to change these values as needed
0130 * to accomodate a wider range of keyboards in the future.
0131 *
0132 * @author Carl Quinn
0133 * @author Amy Fowler
0134 * @author Norbert Lindenberg
0135 * @version 1.88 05/05/07
0136 *
0137 * @see KeyAdapter
0138 * @see KeyListener
0139 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/keylistener.html">Tutorial: Writing a Key Listener</a>
0140 *
0141 * @since 1.1
0142 */
0143 public class KeyEvent extends InputEvent {
0144
0145 /**
0146 * Stores the state of native event dispatching system
0147 * - true, if when the event was created event proxying
0148 * mechanism was active
0149 * - false, if it was inactive
0150 * Used in Component.dispatchEventImpl to correctly dispatch
0151 * events when proxy is active
0152 */
0153 private boolean isProxyActive = false;
0154
0155 /**
0156 * The first number in the range of ids used for key events.
0157 */
0158 public static final int KEY_FIRST = 400;
0159
0160 /**
0161 * The last number in the range of ids used for key events.
0162 */
0163 public static final int KEY_LAST = 402;
0164
0165 /**
0166 * The "key typed" event. This event is generated when a character is
0167 * entered. In the simplest case, it is produced by a single key press.
0168 * Often, however, characters are produced by series of key presses, and
0169 * the mapping from key pressed events to key typed events may be
0170 * many-to-one or many-to-many.
0171 */
0172 public static final int KEY_TYPED = KEY_FIRST;
0173
0174 /**
0175 * The "key pressed" event. This event is generated when a key
0176 * is pushed down.
0177 */
0178 public static final int KEY_PRESSED = 1 + KEY_FIRST; //Event.KEY_PRESS
0179
0180 /**
0181 * The "key released" event. This event is generated when a key
0182 * is let up.
0183 */
0184 public static final int KEY_RELEASED = 2 + KEY_FIRST; //Event.KEY_RELEASE
0185
0186 /* Virtual key codes. */
0187
0188 public static final int VK_ENTER = '\n';
0189 public static final int VK_BACK_SPACE = '\b';
0190 public static final int VK_TAB = '\t';
0191 public static final int VK_CANCEL = 0x03;
0192 public static final int VK_CLEAR = 0x0C;
0193 public static final int VK_SHIFT = 0x10;
0194 public static final int VK_CONTROL = 0x11;
0195 public static final int VK_ALT = 0x12;
0196 public static final int VK_PAUSE = 0x13;
0197 public static final int VK_CAPS_LOCK = 0x14;
0198 public static final int VK_ESCAPE = 0x1B;
0199 public static final int VK_SPACE = 0x20;
0200 public static final int VK_PAGE_UP = 0x21;
0201 public static final int VK_PAGE_DOWN = 0x22;
0202 public static final int VK_END = 0x23;
0203 public static final int VK_HOME = 0x24;
0204
0205 /**
0206 * Constant for the non-numpad <b>left</b> arrow key.
0207 * @see #VK_KP_LEFT
0208 */
0209 public static final int VK_LEFT = 0x25;
0210
0211 /**
0212 * Constant for the non-numpad <b>up</b> arrow key.
0213 * @see #VK_KP_UP
0214 */
0215 public static final int VK_UP = 0x26;
0216
0217 /**
0218 * Constant for the non-numpad <b>right</b> arrow key.
0219 * @see #VK_KP_RIGHT
0220 */
0221 public static final int VK_RIGHT = 0x27;
0222
0223 /**
0224 * Constant for the non-numpad <b>down</b> arrow key.
0225 * @see #VK_KP_DOWN
0226 */
0227 public static final int VK_DOWN = 0x28;
0228
0229 /**
0230 * Constant for the comma key, ","
0231 */
0232 public static final int VK_COMMA = 0x2C;
0233
0234 /**
0235 * Constant for the minus key, "-"
0236 * @since 1.2
0237 */
0238 public static final int VK_MINUS = 0x2D;
0239
0240 /**
0241 * Constant for the period key, "."
0242 */
0243 public static final int VK_PERIOD = 0x2E;
0244
0245 /**
0246 * Constant for the forward slash key, "/"
0247 */
0248 public static final int VK_SLASH = 0x2F;
0249
0250 /** VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
0251 public static final int VK_0 = 0x30;
0252 public static final int VK_1 = 0x31;
0253 public static final int VK_2 = 0x32;
0254 public static final int VK_3 = 0x33;
0255 public static final int VK_4 = 0x34;
0256 public static final int VK_5 = 0x35;
0257 public static final int VK_6 = 0x36;
0258 public static final int VK_7 = 0x37;
0259 public static final int VK_8 = 0x38;
0260 public static final int VK_9 = 0x39;
0261
0262 /**
0263 * Constant for the semicolon key, ";"
0264 */
0265 public static final int VK_SEMICOLON = 0x3B;
0266
0267 /**
0268 * Constant for the equals key, "="
0269 */
0270 public static final int VK_EQUALS = 0x3D;
0271
0272 /** VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
0273 public static final int VK_A = 0x41;
0274 public static final int VK_B = 0x42;
0275 public static final int VK_C = 0x43;
0276 public static final int VK_D = 0x44;
0277 public static final int VK_E = 0x45;
0278 public static final int VK_F = 0x46;
0279 public static final int VK_G = 0x47;
0280 public static final int VK_H = 0x48;
0281 public static final int VK_I = 0x49;
0282 public static final int VK_J = 0x4A;
0283 public static final int VK_K = 0x4B;
0284 public static final int VK_L = 0x4C;
0285 public static final int VK_M = 0x4D;
0286 public static final int VK_N = 0x4E;
0287 public static final int VK_O = 0x4F;
0288 public static final int VK_P = 0x50;
0289 public static final int VK_Q = 0x51;
0290 public static final int VK_R = 0x52;
0291 public static final int VK_S = 0x53;
0292 public static final int VK_T = 0x54;
0293 public static final int VK_U = 0x55;
0294 public static final int VK_V = 0x56;
0295 public static final int VK_W = 0x57;
0296 public static final int VK_X = 0x58;
0297 public static final int VK_Y = 0x59;
0298 public static final int VK_Z = 0x5A;
0299
0300 /**
0301 * Constant for the open bracket key, "["
0302 */
0303 public static final int VK_OPEN_BRACKET = 0x5B;
0304
0305 /**
0306 * Constant for the back slash key, "\"
0307 */
0308 public static final int VK_BACK_SLASH = 0x5C;
0309
0310 /**
0311 * Constant for the close bracket key, "]"
0312 */
0313 public static final int VK_CLOSE_BRACKET = 0x5D;
0314
0315 public static final int VK_NUMPAD0 = 0x60;
0316 public static final int VK_NUMPAD1 = 0x61;
0317 public static final int VK_NUMPAD2 = 0x62;
0318 public static final int VK_NUMPAD3 = 0x63;
0319 public static final int VK_NUMPAD4 = 0x64;
0320 public static final int VK_NUMPAD5 = 0x65;
0321 public static final int VK_NUMPAD6 = 0x66;
0322 public static final int VK_NUMPAD7 = 0x67;
0323 public static final int VK_NUMPAD8 = 0x68;
0324 public static final int VK_NUMPAD9 = 0x69;
0325 public static final int VK_MULTIPLY = 0x6A;
0326 public static final int VK_ADD = 0x6B;
0327
0328 /**
0329 * This constant is obsolete, and is included only for backwards
0330 * compatibility.
0331 * @see #VK_SEPARATOR
0332 */
0333 public static final int VK_SEPARATER = 0x6C;
0334
0335 /**
0336 * Constant for the Numpad Separator key.
0337 * @since 1.4
0338 */
0339 public static final int VK_SEPARATOR = VK_SEPARATER;
0340
0341 public static final int VK_SUBTRACT = 0x6D;
0342 public static final int VK_DECIMAL = 0x6E;
0343 public static final int VK_DIVIDE = 0x6F;
0344 public static final int VK_DELETE = 0x7F; /* ASCII DEL */
0345 public static final int VK_NUM_LOCK = 0x90;
0346 public static final int VK_SCROLL_LOCK = 0x91;
0347
0348 /** Constant for the F1 function key. */
0349 public static final int VK_F1 = 0x70;
0350
0351 /** Constant for the F2 function key. */
0352 public static final int VK_F2 = 0x71;
0353
0354 /** Constant for the F3 function key. */
0355 public static final int VK_F3 = 0x72;
0356
0357 /** Constant for the F4 function key. */
0358 public static final int VK_F4 = 0x73;
0359
0360 /** Constant for the F5 function key. */
0361 public static final int VK_F5 = 0x74;
0362
0363 /** Constant for the F6 function key. */
0364 public static final int VK_F6 = 0x75;
0365
0366 /** Constant for the F7 function key. */
0367 public static final int VK_F7 = 0x76;
0368
0369 /** Constant for the F8 function key. */
0370 public static final int VK_F8 = 0x77;
0371
0372 /** Constant for the F9 function key. */
0373 public static final int VK_F9 = 0x78;
0374
0375 /** Constant for the F10 function key. */
0376 public static final int VK_F10 = 0x79;
0377
0378 /** Constant for the F11 function key. */
0379 public static final int VK_F11 = 0x7A;
0380
0381 /** Constant for the F12 function key. */
0382 public static final int VK_F12 = 0x7B;
0383
0384 /**
0385 * Constant for the F13 function key.
0386 * @since 1.2
0387 */
0388 /* F13 - F24 are used on IBM 3270 keyboard; use random range for constants. */
0389 public static final int VK_F13 = 0xF000;
0390
0391 /**
0392 * Constant for the F14 function key.
0393 * @since 1.2
0394 */
0395 public static final int VK_F14 = 0xF001;
0396
0397 /**
0398 * Constant for the F15 function key.
0399 * @since 1.2
0400 */
0401 public static final int VK_F15 = 0xF002;
0402
0403 /**
0404 * Constant for the F16 function key.
0405 * @since 1.2
0406 */
0407 public static final int VK_F16 = 0xF003;
0408
0409 /**
0410 * Constant for the F17 function key.
0411 * @since 1.2
0412 */
0413 public static final int VK_F17 = 0xF004;
0414
0415 /**
0416 * Constant for the F18 function key.
0417 * @since 1.2
0418 */
0419 public static final int VK_F18 = 0xF005;
0420
0421 /**
0422 * Constant for the F19 function key.
0423 * @since 1.2
0424 */
0425 public static final int VK_F19 = 0xF006;
0426
0427 /**
0428 * Constant for the F20 function key.
0429 * @since 1.2
0430 */
0431 public static final int VK_F20 = 0xF007;
0432
0433 /**
0434 * Constant for the F21 function key.
0435 * @since 1.2
0436 */
0437 public static final int VK_F21 = 0xF008;
0438
0439 /**
0440 * Constant for the F22 function key.
0441 * @since 1.2
0442 */
0443 public static final int VK_F22 = 0xF009;
0444
0445 /**
0446 * Constant for the F23 function key.
0447 * @since 1.2
0448 */
0449 public static final int VK_F23 = 0xF00A;
0450
0451 /**
0452 * Constant for the F24 function key.
0453 * @since 1.2
0454 */
0455 public static final int VK_F24 = 0xF00B;
0456
0457 public static final int VK_PRINTSCREEN = 0x9A;
0458 public static final int VK_INSERT = 0x9B;
0459 public static final int VK_HELP = 0x9C;
0460 public static final int VK_META = 0x9D;
0461
0462 public static final int VK_BACK_QUOTE = 0xC0;
0463 public static final int VK_QUOTE = 0xDE;
0464
0465 /**
0466 * Constant for the numeric keypad <b>up</b> arrow key.
0467 * @see #VK_UP
0468 * @since 1.2
0469 */
0470 public static final int VK_KP_UP = 0xE0;
0471
0472 /**
0473 * Constant for the numeric keypad <b>down</b> arrow key.
0474 * @see #VK_DOWN
0475 * @since 1.2
0476 */
0477 public static final int VK_KP_DOWN = 0xE1;
0478
0479 /**
0480 * Constant for the numeric keypad <b>left</b> arrow key.
0481 * @see #VK_LEFT
0482 * @since 1.2
0483 */
0484 public static final int VK_KP_LEFT = 0xE2;
0485
0486 /**
0487 * Constant for the numeric keypad <b>right</b> arrow key.
0488 * @see #VK_RIGHT
0489 * @since 1.2
0490 */
0491 public static final int VK_KP_RIGHT = 0xE3;
0492
0493 /* For European keyboards */
0494 /** @since 1.2 */
0495 public static final int VK_DEAD_GRAVE = 0x80;
0496 /** @since 1.2 */
0497 public static final int VK_DEAD_ACUTE = 0x81;
0498 /** @since 1.2 */
0499 public static final int VK_DEAD_CIRCUMFLEX = 0x82;
0500 /** @since 1.2 */
0501 public static final int VK_DEAD_TILDE = 0x83;
0502 /** @since 1.2 */
0503 public static final int VK_DEAD_MACRON = 0x84;
0504 /** @since 1.2 */
0505 public static final int VK_DEAD_BREVE = 0x85;
0506 /** @since 1.2 */
0507 public static final int VK_DEAD_ABOVEDOT = 0x86;
0508 /** @since 1.2 */
0509 public static final int VK_DEAD_DIAERESIS = 0x87;
0510 /** @since 1.2 */
0511 public static final int VK_DEAD_ABOVERING = 0x88;
0512 /** @since 1.2 */
0513 public static final int VK_DEAD_DOUBLEACUTE = 0x89;
0514 /** @since 1.2 */
0515 public static final int VK_DEAD_CARON = 0x8a;
0516 /** @since 1.2 */
0517 public static final int VK_DEAD_CEDILLA = 0x8b;
0518 /** @since 1.2 */
0519 public static final int VK_DEAD_OGONEK = 0x8c;
0520 /** @since 1.2 */
0521 public static final int VK_DEAD_IOTA = 0x8d;
0522 /** @since 1.2 */
0523 public static final int VK_DEAD_VOICED_SOUND = 0x8e;
0524 /** @since 1.2 */
0525 public static final int VK_DEAD_SEMIVOICED_SOUND = 0x8f;
0526
0527 /** @since 1.2 */
0528 public static final int VK_AMPERSAND = 0x96;
0529 /** @since 1.2 */
0530 public static final int VK_ASTERISK = 0x97;
0531 /** @since 1.2 */
0532 public static final int VK_QUOTEDBL = 0x98;
0533 /** @since 1.2 */
0534 public static final int VK_LESS = 0x99;
0535
0536 /** @since 1.2 */
0537 public static final int VK_GREATER = 0xa0;
0538 /** @since 1.2 */
0539 public static final int VK_BRACELEFT = 0xa1;
0540 /** @since 1.2 */
0541 public static final int VK_BRACERIGHT = 0xa2;
0542
0543 /**
0544 * Constant for the "@" key.
0545 * @since 1.2
0546 */
0547 public static final int VK_AT = 0x0200;
0548
0549 /**
0550 * Constant for the ":" key.
0551 * @since 1.2
0552 */
0553 public static final int VK_COLON = 0x0201;
0554
0555 /**
0556 * Constant for the "^" key.
0557 * @since 1.2
0558 */
0559 public static final int VK_CIRCUMFLEX = 0x0202;
0560
0561 /**
0562 * Constant for the "$" key.
0563 * @since 1.2
0564 */
0565 public static final int VK_DOLLAR = 0x0203;
0566
0567 /**
0568 * Constant for the Euro currency sign key.
0569 * @since 1.2
0570 */
0571 public static final int VK_EURO_SIGN = 0x0204;
0572
0573 /**
0574 * Constant for the "!" key.
0575 * @since 1.2
0576 */
0577 public static final int VK_EXCLAMATION_MARK = 0x0205;
0578
0579 /**
0580 * Constant for the inverted exclamation mark key.
0581 * @since 1.2
0582 */
0583 public static final int VK_INVERTED_EXCLAMATION_MARK = 0x0206;
0584
0585 /**
0586 * Constant for the "(" key.
0587 * @since 1.2
0588 */
0589 public static final int VK_LEFT_PARENTHESIS = 0x0207;
0590
0591 /**
0592 * Constant for the "#" key.
0593 * @since 1.2
0594 */
0595 public static final int VK_NUMBER_SIGN = 0x0208;
0596
0597 /**
0598 * Constant for the "+" key.
0599 * @since 1.2
0600 */
0601 public static final int VK_PLUS = 0x0209;
0602
0603 /**
0604 * Constant for the ")" key.
0605 * @since 1.2
0606 */
0607 public static final int VK_RIGHT_PARENTHESIS = 0x020A;
0608
0609 /**
0610 * Constant for the "_" key.
0611 * @since 1.2
0612 */
0613 public static final int VK_UNDERSCORE = 0x020B;
0614
0615 /**
0616 * Constant for the Microsoft Windows "Windows" key.
0617 * It is used for both the left and right version of the key.
0618 * @see #getKeyLocation()
0619 * @since 1.5
0620 */
0621 public static final int VK_WINDOWS = 0x020C;
0622
0623 /**
0624 * Constant for the Microsoft Windows Context Menu key.
0625 * @since 1.5
0626 */
0627 public static final int VK_CONTEXT_MENU = 0x020D;
0628
0629 /* for input method support on Asian Keyboards */
0630
0631 /* not clear what this means - listed in Microsoft Windows API */
0632 public static final int VK_FINAL = 0x0018;
0633
0634 /** Constant for the Convert function key. */
0635 /* Japanese PC 106 keyboard, Japanese Solaris keyboard: henkan */
0636 public static final int VK_CONVERT = 0x001C;
0637
0638 /** Constant for the Don't Convert function key. */
0639 /* Japanese PC 106 keyboard: muhenkan */
0640 public static final int VK_NONCONVERT = 0x001D;
0641
0642 /** Constant for the Accept or Commit function key. */
0643 /* Japanese Solaris keyboard: kakutei */
0644 public static final int VK_ACCEPT = 0x001E;
0645
0646 /* not clear what this means - listed in Microsoft Windows API */
0647 public static final int VK_MODECHANGE = 0x001F;
0648
0649 /* replaced by VK_KANA_LOCK for Microsoft Windows and Solaris;
0650 might still be used on other platforms */
0651 public static final int VK_KANA = 0x0015;
0652
0653 /* replaced by VK_INPUT_METHOD_ON_OFF for Microsoft Windows and Solaris;
0654 might still be used for other platforms */
0655 public static final int VK_KANJI = 0x0019;
0656
0657 /**
0658 * Constant for the Alphanumeric function key.
0659 * @since 1.2
0660 */
0661 /* Japanese PC 106 keyboard: eisuu */
0662 public static final int VK_ALPHANUMERIC = 0x00F0;
0663
0664 /**
0665 * Constant for the Katakana function key.
0666 * @since 1.2
0667 */
0668 /* Japanese PC 106 keyboard: katakana */
0669 public static final int VK_KATAKANA = 0x00F1;
0670
0671 /**
0672 * Constant for the Hiragana function key.
0673 * @since 1.2
0674 */
0675 /* Japanese PC 106 keyboard: hiragana */
0676 public static final int VK_HIRAGANA = 0x00F2;
0677
0678 /**
0679 * Constant for the Full-Width Characters function key.
0680 * @since 1.2
0681 */
0682 /* Japanese PC 106 keyboard: zenkaku */
0683 public static final int VK_FULL_WIDTH = 0x00F3;
0684
0685 /**
0686 * Constant for the Half-Width Characters function key.
0687 * @since 1.2
0688 */
0689 /* Japanese PC 106 keyboard: hankaku */
0690 public static final int VK_HALF_WIDTH = 0x00F4;
0691
0692 /**
0693 * Constant for the Roman Characters function key.
0694 * @since 1.2
0695 */
0696 /* Japanese PC 106 keyboard: roumaji */
0697 public static final int VK_ROMAN_CHARACTERS = 0x00F5;
0698
0699 /**
0700 * Constant for the All Candidates function key.
0701 * @since 1.2
0702 */
0703 /* Japanese PC 106 keyboard - VK_CONVERT + ALT: zenkouho */
0704 public static final int VK_ALL_CANDIDATES = 0x0100;
0705
0706 /**
0707 * Constant for the Previous Candidate function key.
0708 * @since 1.2
0709 */
0710 /* Japanese PC 106 keyboard - VK_CONVERT + SHIFT: maekouho */
0711 public static final int VK_PREVIOUS_CANDIDATE = 0x0101;
0712
0713 /**
0714 * Constant for the Code Input function key.
0715 * @since 1.2
0716 */
0717 /* Japanese PC 106 keyboard - VK_ALPHANUMERIC + ALT: kanji bangou */
0718 public static final int VK_CODE_INPUT = 0x0102;
0719
0720 /**
0721 * Constant for the Japanese-Katakana function key.
0722 * This key switches to a Japanese input method and selects its Katakana input mode.
0723 * @since 1.2
0724 */
0725 /* Japanese Macintosh keyboard - VK_JAPANESE_HIRAGANA + SHIFT */
0726 public static final int VK_JAPANESE_KATAKANA = 0x0103;
0727
0728 /**
0729 * Constant for the Japanese-Hiragana function key.
0730 * This key switches to a Japanese input method and selects its Hiragana input mode.
0731 * @since 1.2
0732 */
0733 /* Japanese Macintosh keyboard */
0734 public static final int VK_JAPANESE_HIRAGANA = 0x0104;
0735
0736 /**
0737 * Constant for the Japanese-Roman function key.
0738 * This key switches to a Japanese input method and selects its Roman-Direct input mode.
0739 * @since 1.2
0740 */
0741 /* Japanese Macintosh keyboard */
0742 public static final int VK_JAPANESE_ROMAN = 0x0105;
0743
0744 /**
0745 * Constant for the locking Kana function key.
0746 * This key locks the keyboard into a Kana layout.
0747 * @since 1.3
0748 */
0749 /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */
0750 public static final int VK_KANA_LOCK = 0x0106;
0751
0752 /**
0753 * Constant for the input method on/off key.
0754 * @since 1.3
0755 */
0756 /* Japanese PC 106 keyboard: kanji. Japanese Solaris keyboard: nihongo */
0757 public static final int VK_INPUT_METHOD_ON_OFF = 0x0107;
0758
0759 /* for Sun keyboards */
0760 /** @since 1.2 */
0761 public static final int VK_CUT = 0xFFD1;
0762 /** @since 1.2 */
0763 public static final int VK_COPY = 0xFFCD;
0764 /** @since 1.2 */
0765 public static final int VK_PASTE = 0xFFCF;
0766 /** @since 1.2 */
0767 public static final int VK_UNDO = 0xFFCB;
0768 /** @since 1.2 */
0769 public static final int VK_AGAIN = 0xFFC9;
0770 /** @since 1.2 */
0771 public static final int VK_FIND = 0xFFD0;
0772 /** @since 1.2 */
0773 public static final int VK_PROPS = 0xFFCA;
0774 /** @since 1.2 */
0775 public static final int VK_STOP = 0xFFC8;
0776
0777 /**
0778 * Constant for the Compose function key.
0779 * @since 1.2
0780 */
0781 public static final int VK_COMPOSE = 0xFF20;
0782
0783 /**
0784 * Constant for the AltGraph function key.
0785 * @since 1.2
0786 */
0787 public static final int VK_ALT_GRAPH = 0xFF7E;
0788
0789 /**
0790 * Constant for the Begin key.
0791 * @since 1.5
0792 */
0793 public static final int VK_BEGIN = 0xFF58;
0794
0795 /**
0796 * This value is used to indicate that the keyCode is unknown.
0797 * KEY_TYPED events do not have a keyCode value; this value
0798 * is used instead.
0799 */
0800 public static final int VK_UNDEFINED = 0x0;
0801
0802 /**
0803 * KEY_PRESSED and KEY_RELEASED events which do not map to a
0804 * valid Unicode character use this for the keyChar value.
0805 */
0806 public static final char CHAR_UNDEFINED = 0xFFFF;
0807
0808 /**
0809 * A constant indicating that the keyLocation is indeterminate
0810 * or not relevant.
0811 * <code>KEY_TYPED</code> events do not have a keyLocation; this value
0812 * is used instead.
0813 * @since 1.4
0814 */
0815 public static final int KEY_LOCATION_UNKNOWN = 0;
0816
0817 /**
0818 * A constant indicating that the key pressed or released
0819 * is not distinguished as the left or right version of a key,
0820 * and did not originate on the numeric keypad (or did not
0821 * originate with a virtual key corresponding to the numeric
0822 * keypad).
0823 * @since 1.4
0824 */
0825 public static final int KEY_LOCATION_STANDARD = 1;
0826
0827 /**
0828 * A constant indicating that the key pressed or released is in
0829 * the left key location (there is more than one possible location
0830 * for this key). Example: the left shift key.
0831 * @since 1.4
0832 */
0833 public static final int KEY_LOCATION_LEFT = 2;
0834
0835 /**
0836 * A constant indicating that the key pressed or released is in
0837 * the right key location (there is more than one possible location
0838 * for this key). Example: the right shift key.
0839 * @since 1.4
0840 */
0841 public static final int KEY_LOCATION_RIGHT = 3;
0842
0843 /**
0844 * A constant indicating that the key event originated on the
0845 * numeric keypad or with a virtual key corresponding to the
0846 * numeric keypad.
0847 * @since 1.4
0848 */
0849 public static final int KEY_LOCATION_NUMPAD = 4;
0850
0851 /**
0852 * The unique value assigned to each of the keys on the
0853 * keyboard. There is a common set of key codes that
0854 * can be fired by most keyboards.
0855 * The symbolic name for a key code should be used rather
0856 * than the code value itself.
0857 *
0858 * @serial
0859 * @see #getKeyCode()
0860 * @see #setKeyCode(int)
0861 */
0862 int keyCode;
0863
0864 /**
0865 * <code>keyChar</code> is a valid unicode character
0866 * that is fired by a key or a key combination on
0867 * a keyboard.
0868 *
0869 * @serial
0870 * @see #getKeyChar()
0871 * @see #setKeyChar(char)
0872 */
0873 char keyChar;
0874
0875 /**
0876 * The location of the key on the keyboard.
0877 *
0878 * Some keys occur more than once on a keyboard, e.g. the left and
0879 * right shift keys. Additionally, some keys occur on the numeric
0880 * keypad. This variable is used to distinguish such keys.
0881 *
0882 * The only legal values are <code>KEY_LOCATION_UNKNOWN</code>,
0883 * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
0884 * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
0885 *
0886 * @serial
0887 * @see #getKeyLocation()
0888 */
0889 int keyLocation;
0890
0891 /*
0892 * JDK 1.1 serialVersionUID
0893 */
0894 private static final long serialVersionUID = -2352130953028126954L;
0895
0896 static {
0897 /* ensure that the necessary native libraries are loaded */
0898 NativeLibLoader.loadLibraries();
0899 if (!GraphicsEnvironment.isHeadless()) {
0900 initIDs();
0901 }
0902 }
0903
0904 /**
0905 * Initialize JNI field and method IDs for fields that may be
0906 * accessed from C.
0907 */
0908 private static native void initIDs();
0909
0910 private KeyEvent(Component source, int id, long when,
0911 int modifiers, int keyCode, char keyChar, int keyLocation,
0912 boolean isProxyActive) {
0913 this (source, id, when, modifiers, keyCode, keyChar, keyLocation);
0914 this .isProxyActive = isProxyActive;
0915 }
0916
0917 /**
0918 * Constructs a <code>KeyEvent</code> object.
0919 * <p>Note that passing in an invalid <code>id</code> results in
0920 * unspecified behavior. This method throws an
0921 * <code>IllegalArgumentException</code> if <code>source</code>
0922 * is <code>null</code>.
0923 *
0924 * @param source the <code>Component</code> that originated the event
0925 * @param id an integer identifying the type of event
0926 * @param when a long integer that specifies the time the event
0927 * occurred
0928 * @param modifiers the modifier keys down during event (shift, ctrl,
0929 * alt, meta)
0930 * Either extended _DOWN_MASK or old _MASK modifiers
0931 * should be used, but both models should not be mixed
0932 * in one event. Use of the extended modifiers is
0933 * preferred.
0934 * @param keyCode the integer code for an actual key, or VK_UNDEFINED
0935 * (for a key-typed event)
0936 * @param keyChar the Unicode character generated by this event, or
0937 * CHAR_UNDEFINED (for key-pressed and key-released
0938 * events which do not map to a valid Unicode character)
0939 * @param keyLocation identifies the key location. The only legal
0940 * values are <code>KEY_LOCATION_UNKNOWN</code>,
0941 * <code>KEY_LOCATION_STANDARD</code>, <code>KEY_LOCATION_LEFT</code>,
0942 * <code>KEY_LOCATION_RIGHT</code>, and <code>KEY_LOCATION_NUMPAD</code>.
0943 * @throws IllegalArgumentException
0944 * if <code>id</code> is <code>KEY_TYPED</code> and
0945 * <code>keyChar</code> is <code>CHAR_UNDEFINED</code>;
0946 * or if <code>id</code> is <code>KEY_TYPED</code> and
0947 * <code>keyCode</code> is not <code>VK_UNDEFINED</code>;
0948 * or if <code>id</code> is <code>KEY_TYPED</code> and
0949 * <code>keyLocation</code> is not <code>KEY_LOCATION_UNKNOWN</code>;
0950 * or if <code>keyLocation</code> is not one of the legal
0951 * values enumerated above.
0952 * @throws IllegalArgumentException if <code>source</code> is null
0953 * @since 1.4
0954 */
0955 public KeyEvent(Component source, int id, long when, int modifiers,
0956 int keyCode, char keyChar, int keyLocation) {
0957 super (source, id, when, modifiers);
0958 if (id == KEY_TYPED) {
0959 if (keyChar == CHAR_UNDEFINED) {
0960 throw new IllegalArgumentException("invalid keyChar");
0961 }
0962 if (keyCode != VK_UNDEFINED) {
0963 throw new IllegalArgumentException("invalid keyCode");
0964 }
0965 if (keyLocation != KEY_LOCATION_UNKNOWN) {
0966 throw new IllegalArgumentException(
0967 "invalid keyLocation");
0968 }
0969 }
0970
0971 this .keyCode = keyCode;
0972 this .keyChar = keyChar;
0973
0974 if ((keyLocation < KEY_LOCATION_UNKNOWN)
0975 || (keyLocation > KEY_LOCATION_NUMPAD)) {
0976 throw new IllegalArgumentException("invalid keyLocation");
0977 }
0978 this .keyLocation = keyLocation;
0979 if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
0980 setNewModifiers();
0981 } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
0982 setOldModifiers();
0983 }
0984 }
0985
0986 /**
0987 * Constructs a <code>KeyEvent</code> object.
0988 * <p>Note that passing in an invalid <code>id</code> results in
0989 * unspecified behavior. This method throws an
0990 * <code>IllegalArgumentException</code> if <code>source</code>
0991 * is <code>null</code>.
0992 *
0993 * @param source the <code>Component</code> that originated the event
0994 * @param id an integer identifying the type of event
0995 * @param when a long integer that specifies the time the event
0996 * occurred
0997 * @param modifiers the modifier keys down during event (shift, ctrl,
0998 * alt, meta)
0999 * Either extended _DOWN_MASK or old _MASK modifiers
1000 * should be used, but both models should not be mixed
1001 * in one event. Use of the extended modifiers is
1002 * preferred.
1003 * @param keyCode the integer code for an actual key, or VK_UNDEFINED
1004 * (for a key-typed event)
1005 * @param keyChar the Unicode character generated by this event, or
1006 * CHAR_UNDEFINED (for key-pressed and key-released
1007 * events which do not map to a valid Unicode character)
1008 * @throws IllegalArgumentException if <code>id</code> is
1009 * <code>KEY_TYPED</code> and <code>keyChar</code> is
1010 * <code>CHAR_UNDEFINED</code>; or if <code>id</code> is
1011 * <code>KEY_TYPED</code> and <code>keyCode</code> is not
1012 * <code>VK_UNDEFINED</code>
1013 * @throws IllegalArgumentException if <code>source</code> is null
1014 */
1015 public KeyEvent(Component source, int id, long when, int modifiers,
1016 int keyCode, char keyChar) {
1017 this (source, id, when, modifiers, keyCode, keyChar,
1018 KEY_LOCATION_UNKNOWN);
1019 }
1020
1021 /**
1022 * @deprecated as of JDK1.1
1023 */
1024 @Deprecated
1025 public KeyEvent(Component source, int id, long when, int modifiers,
1026 int keyCode) {
1027 this (source, id, when, modifiers, keyCode, (char) keyCode);
1028 }
1029
1030 /**
1031 * Returns the integer keyCode associated with the key in this event.
1032 *
1033 * @return the integer code for an actual key on the keyboard.
1034 * (For <code>KEY_TYPED</code> events, the keyCode is
1035 * <code>VK_UNDEFINED</code>.)
1036 */
1037 public int getKeyCode() {
1038 return keyCode;
1039 }
1040
1041 /**
1042 * Set the keyCode value to indicate a physical key.
1043 *
1044 * @param keyCode an integer corresponding to an actual key on the keyboard.
1045 */
1046 public void setKeyCode(int keyCode) {
1047 this .keyCode = keyCode;
1048 }
1049
1050 /**
1051 * Returns the character associated with the key in this event.
1052 * For example, the <code>KEY_TYPED</code> event for shift + "a"
1053 * returns the value for "A".
1054 * <p>
1055 * <code>KEY_PRESSED</code> and <code>KEY_RELEASED</code> events
1056 * are not intended for reporting of character input. Therefore,
1057 * the values returned by this method are guaranteed to be
1058 * meaningful only for <code>KEY_TYPED</code> events.
1059 *
1060 * @return the Unicode character defined for this key event.
1061 * If no valid Unicode character exists for this key event,
1062 * <code>CHAR_UNDEFINED</code> is returned.
1063 */
1064 public char getKeyChar() {
1065 return keyChar;
1066 }
1067
1068 /**
1069 * Set the keyChar value to indicate a logical character.
1070 *
1071 * @param keyChar a char corresponding to to the combination of keystrokes
1072 * that make up this event.
1073 */
1074 public void setKeyChar(char keyChar) {
1075 this .keyChar = keyChar;
1076 }
1077
1078 /**
1079 * Set the modifiers to indicate additional keys that were held down
1080 * (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.
1081 * <p>
1082 * NOTE: use of this method is not recommended, because many AWT
1083 * implementations do not recognize modifier changes. This is
1084 * especially true for <code>KEY_TYPED</code> events where the shift
1085 * modifier is changed.
1086 *
1087 * @param modifiers an integer combination of the modifier constants.
1088 * @see InputEvent
1089 * @deprecated as of JDK1.1.4
1090 */
1091 @Deprecated
1092 public void setModifiers(int modifiers) {
1093 this .modifiers = modifiers;
1094 if ((getModifiers() != 0) && (getModifiersEx() == 0)) {
1095 setNewModifiers();
1096 } else if ((getModifiers() == 0) && (getModifiersEx() != 0)) {
1097 setOldModifiers();
1098 }
1099 }
1100
1101 /**
1102 * Returns the location of the key that originated this key event.
1103 *
1104 * Some keys occur more than once on a keyboard, e.g. the left and
1105 * right shift keys. Additionally, some keys occur on the numeric
1106 * keypad. This provides a way of distinguishing such keys.
1107 *
1108 * @return the location of the key that was pressed or released.
1109 * Always returns <code>KEY_LOCATION_UNKNOWN</code> for
1110 * <code>KEY_TYPED</code> events.
1111 * @since 1.4
1112 */
1113 public int getKeyLocation() {
1114 return keyLocation;
1115 }
1116
1117 /**
1118 * Returns a String describing the keyCode, such as "HOME", "F1" or "A".
1119 * These strings can be localized by changing the awt.properties file.
1120 *
1121 * @return a string containing a text description for a physical key,
1122 * identified by its keyCode
1123 */
1124 public static String getKeyText(int keyCode) {
1125 if (keyCode >= VK_0 && keyCode <= VK_9 || keyCode >= VK_A
1126 && keyCode <= VK_Z) {
1127 return String.valueOf((char) keyCode);
1128 }
1129
1130 switch (keyCode) {
1131 case VK_ENTER:
1132 return Toolkit.getProperty("AWT.enter", "Enter");
1133 case VK_BACK_SPACE:
1134 return Toolkit.getProperty("AWT.backSpace", "Backspace");
1135 case VK_TAB:
1136 return Toolkit.getProperty("AWT.tab", "Tab");
1137 case VK_CANCEL:
1138 return Toolkit.getProperty("AWT.cancel", "Cancel");
1139 case VK_CLEAR:
1140 return Toolkit.getProperty("AWT.clear", "Clear");
1141 case VK_COMPOSE:
1142 return Toolkit.getProperty("AWT.compose", "Compose");
1143 case VK_PAUSE:
1144 return Toolkit.getProperty("AWT.pause", "Pause");
1145 case VK_CAPS_LOCK:
1146 return Toolkit.getProperty("AWT.capsLock", "Caps Lock");
1147 case VK_ESCAPE:
1148 return Toolkit.getProperty("AWT.escape", "Escape");
1149 case VK_SPACE:
1150 return Toolkit.getProperty("AWT.space", "Space");
1151 case VK_PAGE_UP:
1152 return Toolkit.getProperty("AWT.pgup", "Page Up");
1153 case VK_PAGE_DOWN:
1154 return Toolkit.getProperty("AWT.pgdn", "Page Down");
1155 case VK_END:
1156 return Toolkit.getProperty("AWT.end", "End");
1157 case VK_HOME:
1158 return Toolkit.getProperty("AWT.home", "Home");
1159 case VK_LEFT:
1160 return Toolkit.getProperty("AWT.left", "Left");
1161 case VK_UP:
1162 return Toolkit.getProperty("AWT.up", "Up");
1163 case VK_RIGHT:
1164 return Toolkit.getProperty("AWT.right", "Right");
1165 case VK_DOWN:
1166 return Toolkit.getProperty("AWT.down", "Down");
1167 case VK_BEGIN:
1168 return Toolkit.getProperty("AWT.begin", "Begin");
1169
1170 // modifiers
1171 case VK_SHIFT:
1172 return Toolkit.getProperty("AWT.shift", "Shift");
1173 case VK_CONTROL:
1174 return Toolkit.getProperty("AWT.control", "Control");
1175 case VK_ALT:
1176 return Toolkit.getProperty("AWT.alt", "Alt");
1177 case VK_META:
1178 return Toolkit.getProperty("AWT.meta", "Meta");
1179 case VK_ALT_GRAPH:
1180 return Toolkit.getProperty("AWT.altGraph", "Alt Graph");
1181
1182 // punctuation
1183 case VK_COMMA:
1184 return Toolkit.getProperty("AWT.comma", "Comma");
1185 case VK_PERIOD:
1186 return Toolkit.getProperty("AWT.period", "Period");
1187 case VK_SLASH:
1188 return Toolkit.getProperty("AWT.slash", "Slash");
1189 case VK_SEMICOLON:
1190 return Toolkit.getProperty("AWT.semicolon", "Semicolon");
1191 case VK_EQUALS:
1192 return Toolkit.getProperty("AWT.equals", "Equals");
1193 case VK_OPEN_BRACKET:
1194 return Toolkit.getProperty("AWT.openBracket",
1195 "Open Bracket");
1196 case VK_BACK_SLASH:
1197 return Toolkit.getProperty("AWT.backSlash", "Back Slash");
1198 case VK_CLOSE_BRACKET:
1199 return Toolkit.getProperty("AWT.closeBracket",
1200 "Close Bracket");
1201
1202 // numpad numeric keys handled below
1203 case VK_MULTIPLY:
1204 return Toolkit.getProperty("AWT.multiply", "NumPad *");
1205 case VK_ADD:
1206 return Toolkit.getProperty("AWT.add", "NumPad +");
1207 case VK_SEPARATOR:
1208 return Toolkit.getProperty("AWT.separator", "NumPad ,");
1209 case VK_SUBTRACT:
1210 return Toolkit.getProperty("AWT.subtract", "NumPad -");
1211 case VK_DECIMAL:
1212 return Toolkit.getProperty("AWT.decimal", "NumPad .");
1213 case VK_DIVIDE:
1214 return Toolkit.getProperty("AWT.divide", "NumPad /");
1215 case VK_DELETE:
1216 return Toolkit.getProperty("AWT.delete", "Delete");
1217 case VK_NUM_LOCK:
1218 return Toolkit.getProperty("AWT.numLock", "Num Lock");
1219 case VK_SCROLL_LOCK:
1220 return Toolkit.getProperty("AWT.scrollLock", "Scroll Lock");
1221
1222 case VK_WINDOWS:
1223 return Toolkit.getProperty("AWT.windows", "Windows");
1224 case VK_CONTEXT_MENU:
1225 return Toolkit.getProperty("AWT.context", "Context Menu");
1226
1227 case VK_F1:
1228 return Toolkit.getProperty("AWT.f1", "F1");
1229 case VK_F2:
1230 return Toolkit.getProperty("AWT.f2", "F2");
1231 case VK_F3:
1232 return Toolkit.getProperty("AWT.f3", "F3");
1233 case VK_F4:
1234 return Toolkit.getProperty("AWT.f4", "F4");
1235 case VK_F5:
1236 return Toolkit.getProperty("AWT.f5", "F5");
1237 case VK_F6:
1238 return Toolkit.getProperty("AWT.f6", "F6");
1239 case VK_F7:
1240 return Toolkit.getProperty("AWT.f7", "F7");
1241 case VK_F8:
1242 return Toolkit.getProperty("AWT.f8", "F8");
1243 case VK_F9:
1244 return Toolkit.getProperty("AWT.f9", "F9");
1245 case VK_F10:
1246 return Toolkit.getProperty("AWT.f10", "F10");
1247 case VK_F11:
1248 return Toolkit.getProperty("AWT.f11", "F11");
1249 case VK_F12:
1250 return Toolkit.getProperty("AWT.f12", "F12");
1251 case VK_F13:
1252 return Toolkit.getProperty("AWT.f13", "F13");
1253 case VK_F14:
1254 return Toolkit.getProperty("AWT.f14", "F14");
1255 case VK_F15:
1256 return Toolkit.getProperty("AWT.f15", "F15");
1257 case VK_F16:
1258 return Toolkit.getProperty("AWT.f16", "F16");
1259 case VK_F17:
1260 return Toolkit.getProperty("AWT.f17", "F17");
1261 case VK_F18:
1262 return Toolkit.getProperty("AWT.f18", "F18");
1263 case VK_F19:
1264 return Toolkit.getProperty("AWT.f19", "F19");
1265 case VK_F20:
1266 return Toolkit.getProperty("AWT.f20", "F20");
1267 case VK_F21:
1268 return Toolkit.getProperty("AWT.f21", "F21");
1269 case VK_F22:
1270 return Toolkit.getProperty("AWT.f22", "F22");
1271 case VK_F23:
1272 return Toolkit.getProperty("AWT.f23", "F23");
1273 case VK_F24:
1274 return Toolkit.getProperty("AWT.f24", "F24");
1275
1276 case VK_PRINTSCREEN:
1277 return Toolkit.getProperty("AWT.printScreen",
1278 "Print Screen");
1279 case VK_INSERT:
1280 return Toolkit.getProperty("AWT.insert", "Insert");
1281 case VK_HELP:
1282 return Toolkit.getProperty("AWT.help", "Help");
1283 case VK_BACK_QUOTE:
1284 return Toolkit.getProperty("AWT.backQuote", "Back Quote");
1285 case VK_QUOTE:
1286 return Toolkit.getProperty("AWT.quote", "Quote");
1287
1288 case VK_KP_UP:
1289 return Toolkit.getProperty("AWT.up", "Up");
1290 case VK_KP_DOWN:
1291 return Toolkit.getProperty("AWT.down", "Down");
1292 case VK_KP_LEFT:
1293 return Toolkit.getProperty("AWT.left", "Left");
1294 case VK_KP_RIGHT:
1295 return Toolkit.getProperty("AWT.right", "Right");
1296
1297 case VK_DEAD_GRAVE:
1298 return Toolkit.getProperty("AWT.deadGrave", "Dead Grave");
1299 case VK_DEAD_ACUTE:
1300 return Toolkit.getProperty("AWT.deadAcute", "Dead Acute");
1301 case VK_DEAD_CIRCUMFLEX:
1302 return Toolkit.getProperty("AWT.deadCircumflex",
1303 "Dead Circumflex");
1304 case VK_DEAD_TILDE:
1305 return Toolkit.getProperty("AWT.deadTilde", "Dead Tilde");
1306 case VK_DEAD_MACRON:
1307 return Toolkit.getProperty("AWT.deadMacron", "Dead Macron");
1308 case VK_DEAD_BREVE:
1309 return Toolkit.getProperty("AWT.deadBreve", "Dead Breve");
1310 case VK_DEAD_ABOVEDOT:
1311 return Toolkit.getProperty("AWT.deadAboveDot",
1312 "Dead Above Dot");
1313 case VK_DEAD_DIAERESIS:
1314 return Toolkit.getProperty("AWT.deadDiaeresis",
1315 "Dead Diaeresis");
1316 case VK_DEAD_ABOVERING:
1317 return Toolkit.getProperty("AWT.deadAboveRing",
1318 "Dead Above Ring");
1319 case VK_DEAD_DOUBLEACUTE:
1320 return Toolkit.getProperty("AWT.deadDoubleAcute",
1321 "Dead Double Acute");
1322 case VK_DEAD_CARON:
1323 return Toolkit.getProperty("AWT.deadCaron", "Dead Caron");
1324 case VK_DEAD_CEDILLA:
1325 return Toolkit.getProperty("AWT.deadCedilla",
1326 "Dead Cedilla");
1327 case VK_DEAD_OGONEK:
1328 return Toolkit.getProperty("AWT.deadOgonek", "Dead Ogonek");
1329 case VK_DEAD_IOTA:
1330 return Toolkit.getProperty("AWT.deadIota", "Dead Iota");
1331 case VK_DEAD_VOICED_SOUND:
1332 return Toolkit.getProperty("AWT.deadVoicedSound",
1333 "Dead Voiced Sound");
1334 case VK_DEAD_SEMIVOICED_SOUND:
1335 return Toolkit.getProperty("AWT.deadSemivoicedSound",
1336 "Dead Semivoiced Sound");
1337
1338 case VK_AMPERSAND:
1339 return Toolkit.getProperty("AWT.ampersand", "Ampersand");
1340 case VK_ASTERISK:
1341 return Toolkit.getProperty("AWT.asterisk", "Asterisk");
1342 case VK_QUOTEDBL:
1343 return Toolkit.getProperty("AWT.quoteDbl", "Double Quote");
1344 case VK_LESS:
1345 return Toolkit.getProperty("AWT.Less", "Less");
1346 case VK_GREATER:
1347 return Toolkit.getProperty("AWT.greater", "Greater");
1348 case VK_BRACELEFT:
1349 return Toolkit.getProperty("AWT.braceLeft", "Left Brace");
1350 case VK_BRACERIGHT:
1351 return Toolkit.getProperty("AWT.braceRight", "Right Brace");
1352 case VK_AT:
1353 return Toolkit.getProperty("AWT.at", "At");
1354 case VK_COLON:
1355 return Toolkit.getProperty("AWT.colon", "Colon");
1356 case VK_CIRCUMFLEX:
1357 return Toolkit.getProperty("AWT.circumflex", "Circumflex");
1358 case VK_DOLLAR:
1359 return Toolkit.getProperty("AWT.dollar", "Dollar");
1360 case VK_EURO_SIGN:
1361 return Toolkit.getProperty("AWT.euro", "Euro");
1362 case VK_EXCLAMATION_MARK:
1363 return Toolkit.getProperty("AWT.exclamationMark",
1364 "Exclamation Mark");
1365 case VK_INVERTED_EXCLAMATION_MARK:
1366 return Toolkit.getProperty("AWT.invertedExclamationMark",
1367 "Inverted Exclamation Mark");
1368 case VK_LEFT_PARENTHESIS:
1369 return Toolkit.getProperty("AWT.leftParenthesis",
1370 "Left Parenthesis");
1371 case VK_NUMBER_SIGN:
1372 return Toolkit.getProperty("AWT.numberSign", "Number Sign");
1373 case VK_MINUS:
1374 return Toolkit.getProperty("AWT.minus", "Minus");
1375 case VK_PLUS:
1376 return Toolkit.getProperty("AWT.plus", "Plus");
1377 case VK_RIGHT_PARENTHESIS:
1378 return Toolkit.getProperty("AWT.rightParenthesis",
1379 "Right Parenthesis");
1380 case VK_UNDERSCORE:
1381 return Toolkit.getProperty("AWT.underscore", "Underscore");
1382
1383 case VK_FINAL:
1384 return Toolkit.getProperty("AWT.final", "Final");
1385 case VK_CONVERT:
1386 return Toolkit.getProperty("AWT.convert", "Convert");
1387 case VK_NONCONVERT:
1388 return Toolkit.getProperty("AWT.noconvert", "No Convert");
1389 case VK_ACCEPT:
1390 return Toolkit.getProperty("AWT.accept", "Accept");
1391 case VK_MODECHANGE:
1392 return Toolkit.getProperty("AWT.modechange", "Mode Change");
1393 case VK_KANA:
1394 return Toolkit.getProperty("AWT.kana", "Kana");
1395 case VK_KANJI:
1396 return Toolkit.getProperty("AWT.kanji", "Kanji");
1397 case VK_ALPHANUMERIC:
1398 return Toolkit.getProperty("AWT.alphanumeric",
1399 "Alphanumeric");
1400 case VK_KATAKANA:
1401 return Toolkit.getProperty("AWT.katakana", "Katakana");
1402 case VK_HIRAGANA:
1403 return Toolkit.getProperty("AWT.hiragana", "Hiragana");
1404 case VK_FULL_WIDTH:
1405 return Toolkit.getProperty("AWT.fullWidth", "Full-Width");
1406 case VK_HALF_WIDTH:
1407 return Toolkit.getProperty("AWT.halfWidth", "Half-Width");
1408 case VK_ROMAN_CHARACTERS:
1409 return Toolkit.getProperty("AWT.romanCharacters",
1410 "Roman Characters");
1411 case VK_ALL_CANDIDATES:
1412 return Toolkit.getProperty("AWT.allCandidates",
1413 "All Candidates");
1414 case VK_PREVIOUS_CANDIDATE:
1415 return Toolkit.getProperty("AWT.previousCandidate",
1416 "Previous Candidate");
1417 case VK_CODE_INPUT:
1418 return Toolkit.getProperty("AWT.codeInput", "Code Input");
1419 case VK_JAPANESE_KATAKANA:
1420 return Toolkit.getProperty("AWT.japaneseKatakana",
1421 "Japanese Katakana");
1422 case VK_JAPANESE_HIRAGANA:
1423 return Toolkit.getProperty("AWT.japaneseHiragana",
1424 "Japanese Hiragana");
1425 case VK_JAPANESE_ROMAN:
1426 return Toolkit.getProperty("AWT.japaneseRoman",
1427 "Japanese Roman");
1428 case VK_KANA_LOCK:
1429 return Toolkit.getProperty("AWT.kanaLock", "Kana Lock");
1430 case VK_INPUT_METHOD_ON_OFF:
1431 return Toolkit.getProperty("AWT.inputMethodOnOff",
1432 "Input Method On/Off");
1433
1434 case VK_AGAIN:
1435 return Toolkit.getProperty("AWT.again", "Again");
1436 case VK_UNDO:
1437 return Toolkit.getProperty("AWT.undo", "Undo");
1438 case VK_COPY:
1439 return Toolkit.getProperty("AWT.copy", "Copy");
1440 case VK_PASTE:
1441 return Toolkit.getProperty("AWT.paste", "Paste");
1442 case VK_CUT:
1443 return Toolkit.getProperty("AWT.cut", "Cut");
1444 case VK_FIND:
1445 return Toolkit.getProperty("AWT.find", "Find");
1446 case VK_PROPS:
1447 return Toolkit.getProperty("AWT.props", "Props");
1448 case VK_STOP:
1449 return Toolkit.getProperty("AWT.stop", "Stop");
1450 }
1451
1452 if (keyCode >= VK_NUMPAD0 && keyCode <= VK_NUMPAD9) {
1453 String numpad = Toolkit.getProperty("AWT.numpad", "NumPad");
1454 char c = (char) (keyCode - VK_NUMPAD0 + '0');
1455 return numpad + "-" + c;
1456 }
1457
1458 String unknown = Toolkit.getProperty("AWT.unknown", "Unknown");
1459 return unknown + " keyCode: 0x" + Integer.toString(keyCode, 16);
1460 }
1461
1462 /**
1463 * Returns a <code>String</code> describing the modifier key(s),
1464 * such as "Shift", or "Ctrl+Shift". These strings can be
1465 * localized by changing the <code>awt.properties</code> file.
1466 * <p>
1467 * Note that <code>InputEvent.ALT_MASK</code> and
1468 * <code>InputEvent.BUTTON2_MASK</code> have the same value,
1469 * so the string "Alt" is returned for both modifiers. Likewise,
1470 * <code>InputEvent.META_MASK</code> and
1471 * <code>InputEvent.BUTTON3_MASK</code> have the same value,
1472 * so the string "Meta" is returned for both modifiers.
1473 *
1474 * @return string a text description of the combination of modifier
1475 * keys that were held down during the event
1476 * @see InputEvent#getModifiersExText(int)
1477 */
1478 public static String getKeyModifiersText(int modifiers) {
1479 StringBuilder buf = new StringBuilder();
1480 if ((modifiers & InputEvent.META_MASK) != 0) {
1481 buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
1482 buf.append("+");
1483 }
1484 if ((modifiers & InputEvent.CTRL_MASK) != 0) {
1485 buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
1486 buf.append("+");
1487 }
1488 if ((modifiers & InputEvent.ALT_MASK) != 0) {
1489 buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
1490 buf.append("+");
1491 }
1492 if ((modifiers & InputEvent.SHIFT_MASK) != 0) {
1493 buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
1494 buf.append("+");
1495 }
1496 if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) {
1497 buf
1498 .append(Toolkit.getProperty("AWT.altGraph",
1499 "Alt Graph"));
1500 buf.append("+");
1501 }
1502 if ((modifiers & InputEvent.BUTTON1_MASK) != 0) {
1503 buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
1504 buf.append("+");
1505 }
1506 if (buf.length() > 0) {
1507 buf.setLength(buf.length() - 1); // remove trailing '+'
1508 }
1509 return buf.toString();
1510 }
1511
1512 /**
1513 * Returns whether the key in this event is an "action" key.
1514 * Typically an action key does not fire a unicode character and is
1515 * not a modifier key.
1516 *
1517 * @return <code>true</code> if the key is an "action" key,
1518 * <code>false</code> otherwise
1519 */
1520 public boolean isActionKey() {
1521 switch (keyCode) {
1522 case VK_HOME:
1523 case VK_END:
1524 case VK_PAGE_UP:
1525 case VK_PAGE_DOWN:
1526 case VK_UP:
1527 case VK_DOWN:
1528 case VK_LEFT:
1529 case VK_RIGHT:
1530 case VK_BEGIN:
1531
1532 case VK_KP_LEFT:
1533 case VK_KP_UP:
1534 case VK_KP_RIGHT:
1535 case VK_KP_DOWN:
1536
1537 case VK_F1:
1538 case VK_F2:
1539 case VK_F3:
1540 case VK_F4:
1541 case VK_F5:
1542 case VK_F6:
1543 case VK_F7:
1544 case VK_F8:
1545 case VK_F9:
1546 case VK_F10:
1547 case VK_F11:
1548 case VK_F12:
1549 case VK_F13:
1550 case VK_F14:
1551 case VK_F15:
1552 case VK_F16:
1553 case VK_F17:
1554 case VK_F18:
1555 case VK_F19:
1556 case VK_F20:
1557 case VK_F21:
1558 case VK_F22:
1559 case VK_F23:
1560 case VK_F24:
1561 case VK_PRINTSCREEN:
1562 case VK_SCROLL_LOCK:
1563 case VK_CAPS_LOCK:
1564 case VK_NUM_LOCK:
1565 case VK_PAUSE:
1566 case VK_INSERT:
1567
1568 case VK_FINAL:
1569 case VK_CONVERT:
1570 case VK_NONCONVERT:
1571 case VK_ACCEPT:
1572 case VK_MODECHANGE:
1573 case VK_KANA:
1574 case VK_KANJI:
1575 case VK_ALPHANUMERIC:
1576 case VK_KATAKANA:
1577 case VK_HIRAGANA:
1578 case VK_FULL_WIDTH:
1579 case VK_HALF_WIDTH:
1580 case VK_ROMAN_CHARACTERS:
1581 case VK_ALL_CANDIDATES:
1582 case VK_PREVIOUS_CANDIDATE:
1583 case VK_CODE_INPUT:
1584 case VK_JAPANESE_KATAKANA:
1585 case VK_JAPANESE_HIRAGANA:
1586 case VK_JAPANESE_ROMAN:
1587 case VK_KANA_LOCK:
1588 case VK_INPUT_METHOD_ON_OFF:
1589
1590 case VK_AGAIN:
1591 case VK_UNDO:
1592 case VK_COPY:
1593 case VK_PASTE:
1594 case VK_CUT:
1595 case VK_FIND:
1596 case VK_PROPS:
1597 case VK_STOP:
1598
1599 case VK_HELP:
1600 case VK_WINDOWS:
1601 case VK_CONTEXT_MENU:
1602 return true;
1603 }
1604 return false;
1605 }
1606
1607 /**
1608 * Returns a parameter string identifying this event.
1609 * This method is useful for event logging and for debugging.
1610 *
1611 * @return a string identifying the event and its attributes
1612 */
1613 public String paramString() {
1614 StringBuilder str = new StringBuilder(100);
1615
1616 switch (id) {
1617 case KEY_PRESSED:
1618 str.append("KEY_PRESSED");
1619 break;
1620 case KEY_RELEASED:
1621 str.append("KEY_RELEASED");
1622 break;
1623 case KEY_TYPED:
1624 str.append("KEY_TYPED");
1625 break;
1626 default:
1627 str.append("unknown type");
1628 break;
1629 }
1630
1631 str.append(",keyCode=").append(keyCode);
1632 str.append(",keyText=").append(getKeyText(keyCode));
1633
1634 /* Some keychars don't print well, e.g. escape, backspace,
1635 * tab, return, delete, cancel. Get keyText for the keyCode
1636 * instead of the keyChar.
1637 */
1638 str.append(",keyChar=");
1639 switch (keyChar) {
1640 case '\b':
1641 str.append(getKeyText(VK_BACK_SPACE));
1642 break;
1643 case '\t':
1644 str.append(getKeyText(VK_TAB));
1645 break;
1646 case '\n':
1647 str.append(getKeyText(VK_ENTER));
1648 break;
1649 case '\u0018':
1650 str.append(getKeyText(VK_CANCEL));
1651 break;
1652 case '\u001b':
1653 str.append(getKeyText(VK_ESCAPE));
1654 break;
1655 case '\u007f':
1656 str.append(getKeyText(VK_DELETE));
1657 break;
1658 case CHAR_UNDEFINED:
1659 str.append(Toolkit
1660 .getProperty("AWT.undefined", "Undefined"));
1661 str.append(" keyChar");
1662 break;
1663 default:
1664 str.append("'").append(keyChar).append("'");
1665 break;
1666 }
1667
1668 if (getModifiers() != 0) {
1669 str.append(",modifiers=").append(
1670 getKeyModifiersText(modifiers));
1671 }
1672 if (getModifiersEx() != 0) {
1673 str.append(",extModifiers=").append(
1674 getModifiersExText(modifiers));
1675 }
1676
1677 str.append(",keyLocation=");
1678 switch (keyLocation) {
1679 case KEY_LOCATION_UNKNOWN:
1680 str.append("KEY_LOCATION_UNKNOWN");
1681 break;
1682 case KEY_LOCATION_STANDARD:
1683 str.append("KEY_LOCATION_STANDARD");
1684 break;
1685 case KEY_LOCATION_LEFT:
1686 str.append("KEY_LOCATION_LEFT");
1687 break;
1688 case KEY_LOCATION_RIGHT:
1689 str.append("KEY_LOCATION_RIGHT");
1690 break;
1691 case KEY_LOCATION_NUMPAD:
1692 str.append("KEY_LOCATION_NUMPAD");
1693 break;
1694 default:
1695 str.append("KEY_LOCATION_UNKNOWN");
1696 break;
1697 }
1698
1699 return str.toString();
1700 }
1701
1702 /**
1703 * Sets new modifiers by the old ones. The key modifiers
1704 * override overlaping mouse modifiers.
1705 */
1706 private void setNewModifiers() {
1707 if ((modifiers & SHIFT_MASK) != 0) {
1708 modifiers |= SHIFT_DOWN_MASK;
1709 }
1710 if ((modifiers & ALT_MASK) != 0) {
1711 modifiers |= ALT_DOWN_MASK;
1712 }
1713 if ((modifiers & CTRL_MASK) != 0) {
1714 modifiers |= CTRL_DOWN_MASK;
1715 }
1716 if ((modifiers & META_MASK) != 0) {
1717 modifiers |= META_DOWN_MASK;
1718 }
1719 if ((modifiers & ALT_GRAPH_MASK) != 0) {
1720 modifiers |= ALT_GRAPH_DOWN_MASK;
1721 }
1722 if ((modifiers & BUTTON1_MASK) != 0) {
1723 modifiers |= BUTTON1_DOWN_MASK;
1724 }
1725 }
1726
1727 /**
1728 * Sets old modifiers by the new ones.
1729 */
1730 private void setOldModifiers() {
1731 if ((modifiers & SHIFT_DOWN_MASK) != 0) {
1732 modifiers |= SHIFT_MASK;
1733 }
1734 if ((modifiers & ALT_DOWN_MASK) != 0) {
1735 modifiers |= ALT_MASK;
1736 }
1737 if ((modifiers & CTRL_DOWN_MASK) != 0) {
1738 modifiers |= CTRL_MASK;
1739 }
1740 if ((modifiers & META_DOWN_MASK) != 0) {
1741 modifiers |= META_MASK;
1742 }
1743 if ((modifiers & ALT_GRAPH_DOWN_MASK) != 0) {
1744 modifiers |= ALT_GRAPH_MASK;
1745 }
1746 if ((modifiers & BUTTON1_DOWN_MASK) != 0) {
1747 modifiers |= BUTTON1_MASK;
1748 }
1749 }
1750
1751 /**
1752 * Sets new modifiers by the old ones. The key modifiers
1753 * override overlaping mouse modifiers.
1754 * @serial
1755 */
1756 private void readObject(ObjectInputStream s) throws IOException,
1757 ClassNotFoundException {
1758 s.defaultReadObject();
1759 if (getModifiers() != 0 && getModifiersEx() == 0) {
1760 setNewModifiers();
1761 }
1762 }
1763 }
|