java.lang.Object java.util.EventObject java.awt.AWTEvent java.awt.event.ComponentEvent java.awt.event.InputEvent java.awt.event.KeyEventpublic class KeyEvent extends InputEvent getKeyCode method will return VK_UNDEFINED,
and getKeyChar will return a valid Unicode character or
CHAR_UNDEFINED.
"Key pressed" and "key released" events are lower-level, and
are platform and keyboard dependent. They correspond to the actaul motion
on a keyboard, and return a virtual key code which labels the key that was
pressed. The getKeyCode method will return one of the
VK_* constants (except VK_UNDEFINED), and the
getKeyChar method is undefined.
Some keys do not generate key typed events, such as the F1 or HELP keys.
Not all keyboards can generate all virtual keys, and no attempt is made to
simulate the ones that can't be typed. Virtual keys correspond to the
keyboard layout, so for example, VK_Q in English is VK_A in French. Also,
there are some additional virtual keys to ease handling of actions, such
as VK_ALL_CANDIDATES in place of ALT+VK_CONVERT. Do not rely on the value
of the VK_* constants, except for VK_ENTER, VK_BACK_SPACE, and VK_TAB.
KeyAdapter, KeyListener, Serialized Form static char CHAR_UNDEFINED static int KEY_FIRST static int KEY_LAST static int KEY_LOCATION_LEFT static int KEY_LOCATION_NUMPAD static int KEY_LOCATION_RIGHT static int KEY_LOCATION_STANDARD static int KEY_LOCATION_UNKNOWN static int KEY_PRESSED static int KEY_RELEASED static int KEY_TYPED static int VK_0 static int VK_1 static int VK_2 static int VK_3 static int VK_4 static int VK_5 static int VK_6 static int VK_7 static int VK_8 static int VK_9 static int VK_A static int VK_ACCEPT static int VK_ADD static int VK_AGAIN static int VK_ALL_CANDIDATES static int VK_ALPHANUMERIC static int VK_ALT static int VK_ALT_GRAPH static int VK_AMPERSAND static int VK_ASTERISK static int VK_AT static int VK_B static int VK_BACK_QUOTE static int VK_BACK_SLASH static int VK_BACK_SPACE static int VK_BEGIN static int VK_BRACELEFT static int VK_BRACERIGHT static int VK_C static int VK_CANCEL static int VK_CAPS_LOCK static int VK_CIRCUMFLEX static int VK_CLEAR static int VK_CLOSE_BRACKET static int VK_CODE_INPUT static int VK_COLON static int VK_COMMA static int VK_COMPOSE static int VK_CONTEXT_MENU static int VK_CONTROL static int VK_CONVERT static int VK_COPY static int VK_CUT static int VK_D static int VK_DEAD_ABOVEDOT static int VK_DEAD_ABOVERING static int VK_DEAD_ACUTE static int VK_DEAD_BREVE static int VK_DEAD_CARON static int VK_DEAD_CEDILLA static int VK_DEAD_CIRCUMFLEX static int VK_DEAD_DIAERESIS static int VK_DEAD_DOUBLEACUTE static int VK_DEAD_GRAVE static int VK_DEAD_IOTA static int VK_DEAD_MACRON static int VK_DEAD_OGONEK static int VK_DEAD_SEMIVOICED_SOUND static int VK_DEAD_TILDE static int VK_DEAD_VOICED_SOUND static int VK_DECIMAL static int VK_DELETE static int VK_DIVIDE static int VK_DOLLAR static int VK_DOWN static int VK_E static int VK_END static int VK_ENTER static int VK_EQUALS static int VK_ESCAPE static int VK_EURO_SIGN static int VK_EXCLAMATION_MARK static int VK_F static int VK_F1 static int VK_F10 static int VK_F11 static int VK_F12 static int VK_F13 static int VK_F14 static int VK_F15 static int VK_F16 static int VK_F17 static int VK_F18 static int VK_F19 static int VK_F2 static int VK_F20 static int VK_F21 static int VK_F22 static int VK_F23 static int VK_F24 static int VK_F3 static int VK_F4 static int VK_F5 static int VK_F6 static int VK_F7 static int VK_F8 static int VK_F9 static int VK_FINAL static int VK_FIND static int VK_FULL_WIDTH static int VK_G static int VK_GREATER static int VK_H static int VK_HALF_WIDTH static int VK_HELP static int VK_HIRAGANA static int VK_HOME static int VK_I static int VK_INPUT_METHOD_ON_OFF static int VK_INSERT static int VK_INVERTED_EXCLAMATION_MARK static int VK_J static int VK_JAPANESE_HIRAGANA static int VK_JAPANESE_KATAKANA static int VK_JAPANESE_ROMAN static int VK_K static int VK_KANA static int VK_KANA_LOCK static int VK_KANJI static int VK_KATAKANA static int VK_KP_DOWN static int VK_KP_LEFT static int VK_KP_RIGHT static int VK_KP_UP static int VK_L static int VK_LEFT static int VK_LEFT_PARENTHESIS static int VK_LESS static int VK_M static int VK_META static int VK_MINUS static int VK_MODECHANGE static int VK_MULTIPLY static int VK_N static int VK_NONCONVERT static int VK_NUMBER_SIGN static int VK_NUMPAD0 static int VK_NUMPAD1 static int VK_NUMPAD2 static int VK_NUMPAD3 static int VK_NUMPAD4 static int VK_NUMPAD5 static int VK_NUMPAD6 static int VK_NUMPAD7 static int VK_NUMPAD8 static int VK_NUMPAD9 static int VK_NUM_LOCK static int VK_O static int VK_OPEN_BRACKET static int VK_P static int VK_PAGE_DOWN static int VK_PAGE_UP static int VK_PASTE static int VK_PAUSE static int VK_PERIOD static int VK_PLUS static int VK_PREVIOUS_CANDIDATE static int VK_PRINTSCREEN static int VK_PROPS static int VK_Q static int VK_QUOTE static int VK_QUOTEDBL static int VK_R static int VK_RIGHT static int VK_RIGHT_PARENTHESIS static int VK_ROMAN_CHARACTERS static int VK_S static int VK_SCROLL_LOCK static int VK_SEMICOLON static int VK_SEPARATER VK_SEPARATOR static int VK_SEPARATOR static int VK_SHIFT static int VK_SLASH static int VK_SPACE static int VK_STOP static int VK_SUBTRACT static int VK_T static int VK_TAB static int VK_U static int VK_UNDEFINED static int VK_UNDERSCORE static int VK_UNDO static int VK_UP static int VK_V static int VK_W static int VK_WINDOWS static int VK_X static int VK_Y static int VK_Z ACTION_EVENT_MASK, ADJUSTMENT_EVENT_MASK, COMPONENT_EVENT_MASK, CONTAINER_EVENT_MASK, FOCUS_EVENT_MASK, HIERARCHY_BOUNDS_EVENT_MASK, HIERARCHY_EVENT_MASK, INPUT_METHOD_EVENT_MASK, INVOCATION_EVENT_MASK, ITEM_EVENT_MASK, KEY_EVENT_MASK, MOUSE_EVENT_MASK, MOUSE_MOTION_EVENT_MASK, MOUSE_WHEEL_EVENT_MASK, PAINT_EVENT_MASK, RESERVED_ID_MAX, TEXT_EVENT_MASK, WINDOW_EVENT_MASK, WINDOW_FOCUS_EVENT_MASK, WINDOW_STATE_EVENT_MASK, consumed, id char getKeyChar() int getKeyCode() int getKeyLocation()static String getKeyModifiersText(int modifiers)static String getKeyText(int keyCode) boolean isActionKey() String paramString() void setKeyChar(char keyChar) void setKeyCode(int keyCode) void setModifiers(int modifiers)public static final char CHAR_UNDEFINED
The only char with no valid Unicode interpretation. This is used for key pressed and key released events which do not have a valid keyChar.
- Field Value:
- '\uffff'
public static final int KEY_FIRST
This is the first id in the range of event ids used by this class.
- Field Value:
- 400
public static final int KEY_LAST
This is the last id in the range of event ids used by this class.
- Field Value:
- 402
public static final int KEY_LOCATION_LEFT
Indicates the key is on the left side of the keyboard, such as the left shift.
- Field Value:
- 2
- Since:
- 1.4
public static final int KEY_LOCATION_NUMPAD
Indicates the key is on the numeric pad, such as the numpad 0.
- Field Value:
- 4
- Since:
- 1.4
public static final int KEY_LOCATION_RIGHT
Indicates the key is on the right side of the keyboard, such as the right shift.
- Field Value:
- 3
- Since:
- 1.4
public static final int KEY_LOCATION_STANDARD
Indicates a standard key location, with no left/right variants and not on the numeric pad.
- Field Value:
- 1
- Since:
- 1.4
public static final int KEY_LOCATION_UNKNOWN
Indicates unknown or irrelavent key location. This is also used for key typed events, which do not need a location.
- Field Value:
- 0
- Since:
- 1.4
public static final int KEY_PRESSED
This event id indicates a key was pressed.
- Field Value:
- 401
public static final int KEY_RELEASED
This event it indicates a key was released.
- Field Value:
- 402
public static final int KEY_TYPED
This event id indicates a key was typed, which is a key press followed by a key release to generate an actual Unicode character. It may take several key presses to generate one key typed event, and some action keys have no corresponding key typed.
- Field Value:
- 400
public static final int VK_ALL_CANDIDATES
The virtual key VK_ALL_CANDIDATES.
- Field Value:
- 256
- Since:
- 1.2
public static final int VK_ALPHANUMERIC
The virtual key VK_ALPHANUMERIC.
- Field Value:
- 240
- Since:
- 1.2
public static final int VK_ALT_GRAPH
The virtual key VK_ALT_GRAPH.
- Field Value:
- 65406
- Since:
- 1.2
public static final int VK_AMPERSAND
The virtual key VK_AMPERSAND.
- Field Value:
- 150
- Since:
- 1.2
public static final int VK_ASTERISK
The virtual key VK_ASTERISK.
- Field Value:
- 151
- Since:
- 1.2
public static final int VK_BACK_SPACE
The virtual key Backspace, which will always map to '\b'.
- Field Value:
- 8
public static final int VK_BRACELEFT
The virtual key VK_BRACELEFT.
- Field Value:
- 161
- Since:
- 1.2
public static final int VK_BRACERIGHT
The virtual key VK_BRACERIGHT.
- Field Value:
- 162
- Since:
- 1.2
public static final int VK_CIRCUMFLEX
The virtual key VK_CIRCUMFLEX.
- Field Value:
- 514
- Since:
- 1.2
public static final int VK_CLOSE_BRACKET
The virtual key VK_CLOSE_BRACKET.
- Field Value:
- 93
public static final int VK_CODE_INPUT
The virtual key VK_CODE_INPUT.
- Field Value:
- 258
- Since:
- 1.2
public static final int VK_CONTEXT_MENU
The context-menu key VK_CONTEXT_MENU
- Field Value:
- 525
- Since:
- 1.5
public static final int VK_DEAD_ABOVEDOT
The virtual key VK_DEAD_ABOVEDOT.
- Field Value:
- 134
- Since:
- 1.2
public static final int VK_DEAD_ABOVERING
The virtual key VK_DEAD_ABOVERING.
- Field Value:
- 136
- Since:
- 1.2
public static final int VK_DEAD_ACUTE
The virtual key VK_DEAD_ACUTE.
- Field Value:
- 129
- Since:
- 1.2
public static final int VK_DEAD_BREVE
The virtual key VK_DEAD_BREVE.
- Field Value:
- 133
- Since:
- 1.2
public static final int VK_DEAD_CARON
The virtual key VK_DEAD_CARON.
- Field Value:
- 138
- Since:
- 1.2
public static final int VK_DEAD_CEDILLA
The virtual key VK_DEAD_CEDILLA.
- Field Value:
- 139
- Since:
- 1.2
public static final int VK_DEAD_CIRCUMFLEX
The virtual key VK_DEAD_CIRCUMFLEX.
- Field Value:
- 130
- Since:
- 1.2
public static final int VK_DEAD_DIAERESIS
The virtual key VK_DEAD_DIAERESIS.
- Field Value:
- 135
- Since:
- 1.2
public static final int VK_DEAD_DOUBLEACUTE
The virtual key VK_DEAD_DOUBLEACUTE.
- Field Value:
- 137
- Since:
- 1.2
public static final int VK_DEAD_GRAVE
The virtual key VK_DEAD_GRAVE.
- Field Value:
- 128
- Since:
- 1.2
public static final int VK_DEAD_IOTA
The virtual key VK_DEAD_IOTA.
- Field Value:
- 141
- Since:
- 1.2
public static final int VK_DEAD_MACRON
The virtual key VK_DEAD_MACRON.
- Field Value:
- 132
- Since:
- 1.2
public static final int VK_DEAD_OGONEK
The virtual key VK_DEAD_OGONEK.
- Field Value:
- 140
- Since:
- 1.2
public static final int VK_DEAD_SEMIVOICED_SOUND
The virtual key VK_DEAD_SEMIVOICED_SOUND.
- Field Value:
- 143
- Since:
- 1.2
public static final int VK_DEAD_TILDE
The virtual key VK_DEAD_TILDE.
- Field Value:
- 131
- Since:
- 1.2
public static final int VK_DEAD_VOICED_SOUND
The virtual key VK_DEAD_VOICED_SOUND.
- Field Value:
- 142
- Since:
- 1.2
public static final int VK_DOWN
The virtual key for the non-numpad VK_DOWN.
- Field Value:
- 40
- See Also:
VK_KP_DOWN
public static final int VK_ENTER
The virtual key Enter, which will always map to '\n'.
- Field Value:
- 10
public static final int VK_EURO_SIGN
The virtual key VK_EURO_SIGN.
- Field Value:
- 516
- Since:
- 1.2
public static final int VK_EXCLAMATION_MARK
The virtual key VK_EXCLAMATION_MARK.
- Field Value:
- 517
- Since:
- 1.2
public static final int VK_FULL_WIDTH
The virtual key VK_FULL_WIDTH.
- Field Value:
- 243
- Since:
- 1.2
public static final int VK_HALF_WIDTH
The virtual key VK_HALF_WIDTH.
- Field Value:
- 244
- Since:
- 1.2
public static final int VK_HIRAGANA
The virtual key VK_HIRAGANA.
- Field Value:
- 242
- Since:
- 1.2
public static final int VK_INPUT_METHOD_ON_OFF
The virtual key VK_INPUT_METHOD_ON_OFF.
- Field Value:
- 263
- Since:
- 1.3
public static final int VK_INVERTED_EXCLAMATION_MARK
The virtual key VK_INVERTED_EXCLAMATION_MARK.
- Field Value:
- 518
- Since:
- 1.2
public static final int VK_JAPANESE_HIRAGANA
The virtual key VK_JAPANESE_HIRAGANA.
- Field Value:
- 260
- Since:
- 1.2
public static final int VK_JAPANESE_KATAKANA
The virtual key VK_JAPANESE_KATAKANA.
- Field Value:
- 259
- Since:
- 1.2
public static final int VK_JAPANESE_ROMAN
The virtual key VK_JAPANESE_ROMAN.
- Field Value:
- 261
- Since:
- 1.2
public static final int VK_KANA_LOCK
The virtual key VK_KANA_LOCK.
- Field Value:
- 262
- Since:
- 1.3
public static final int VK_KATAKANA
The virtual key VK_KATAKANA.
- Field Value:
- 241
- Since:
- 1.2
public static final int VK_KP_DOWN
The virtual key for the numpad VK_KP_DOWN.
- Field Value:
- 225
- Since:
- 1.2
- See Also:
VK_DOWN
public static final int VK_KP_LEFT
The virtual key for the numpad VK_KP_LEFT.
- Field Value:
- 226
- Since:
- 1.2
- See Also:
VK_LEFT
public static final int VK_KP_RIGHT
The virtual key for the numpad VK_KP_RIGHT.
- Field Value:
- 227
- Since:
- 1.2
- See Also:
VK_RIGHT
public static final int VK_KP_UP
The virtual key for the numpad VK_KP_UP.
- Field Value:
- 224
- Since:
- 1.2
- See Also:
VK_UP
public static final int VK_LEFT
The virtual key for the non-numpad VK_LEFT.
- Field Value:
- 37
- See Also:
VK_KP_LEFT
public static final int VK_LEFT_PARENTHESIS
The virtual key VK_LEFT_PARENTHESIS.
- Field Value:
- 519
- Since:
- 1.2
public static final int VK_NUMBER_SIGN
The virtual key VK_NUMBER_SIGN.
- Field Value:
- 520
- Since:
- 1.2
public static final int VK_OPEN_BRACKET
The virtual key VK_OPEN_BRACKET.
- Field Value:
- 91
public static final int VK_PREVIOUS_CANDIDATE
The virtual key VK_PREVIOUS_CANDIDATE.
- Field Value:
- 257
- Since:
- 1.2
public static final int VK_PRINTSCREEN
The virtual key VK_PRINTSCREEN.
- Field Value:
- 154
public static final int VK_QUOTEDBL
The virtual key VK_QUOTEDBL.
- Field Value:
- 152
- Since:
- 1.2
public static final int VK_RIGHT
The virtual key for the non-numpad VK_RIGHT.
- Field Value:
- 39
- See Also:
VK_KP_RIGHT
public static final int VK_RIGHT_PARENTHESIS
The virtual key VK_RIGHT_PARENTHESIS.
- Field Value:
- 522
- Since:
- 1.2
public static final int VK_ROMAN_CHARACTERS
The virtual key VK_ROMAN_CHARACTERS.
- Field Value:
- 245
- Since:
- 1.2
public static final int VK_SCROLL_LOCK
The virtual key VK_SCROLL_LOCK.
- Field Value:
- 145
public static final int VK_SEPARATER
Deprecated. use
VK_SEPARATORThe virtual key VK_SEPARATOR, handily mispelled for those who can't figure it out.
- Field Value:
- 108
public static final int VK_SEPARATOR
The virtual key VK_SEPARATOR.
- Field Value:
- 108
- Since:
- 1.4
public static final int VK_TAB
The virtual key Tab, which will always map to '\t'.
- Field Value:
- 9
public static final int VK_UNDEFINED
The virtual key VK_UNDEFINED. This is used for key typed events, which do not have a virtual key.
- Field Value:
- 0
public static final int VK_UNDERSCORE
The virtual key VK_UNDERSCORE.
- Field Value:
- 523
- Since:
- 1.2
public static final int VK_UP
The virtual key for the non-numpad VK_UP.
- Field Value:
- 38
- See Also:
VK_KP_UP
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode)
Deprecated.
Initializes a new instance ofKeyEventwith the specified information. Note that an invalid id leads to unspecified results.
- Parameters:
source- the component that generated this eventid- the event idwhen- the timestamp when the even occurredmodifiers- the modifier keys during the event, in old or new stylekeyCode- the integer constant for the virtual key type
- Throws:
IllegalArgumentException- if source is null, or if id == KEY_TYPED but keyCode != VK_UNDEFINED
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)
Initializes a new instance ofKeyEventwith the specified information. Note that an invalid id leads to unspecified results.
- Parameters:
source- the component that generated this eventid- the event idwhen- the timestamp when the even occurredmodifiers- the modifier keys during the event, in old or new stylekeyCode- the integer constant for the virtual key typekeyChar- the Unicode value of the key
- Throws:
IllegalArgumentException- if source is null, or if (id == KEY_TYPED && (keyCode != VK_UNDEFINED || keyChar == CHAR_UNDEFINED))
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)
Initializes a new instance ofKeyEventwith the specified information. Note that an invalid id leads to unspecified results.
- Parameters:
source- the component that generated this eventid- the event idwhen- the timestamp when the even occurredmodifiers- the modifier keys during the event, in old or new stylekeyCode- the integer constant for the virtual key typekeyChar- the Unicode value of the keykeyLocation- the location of the key
- Throws:
IllegalArgumentException- if source is null, if keyLocation is invalid, or if (id == KEY_TYPED && (keyCode != VK_UNDEFINED || keyChar == CHAR_UNDEFINED))
public char getKeyChar()
Returns the Unicode value for the event key. This will beCHAR_UNDEFINEDif there is no Unicode equivalent for this key, usually when this is a KEY_PRESSED or KEY_RELEASED event.
- Returns:
- the Unicode character for this event
public int getKeyCode()
Returns the key code for the event key. This will be one of theVK_*constants defined in this class. If the event type is KEY_TYPED, the result will be VK_UNDEFINED.
- Returns:
- the key code for this event
public int getKeyLocation()
Returns the keyboard location of the key that generated this event. This provides a way to distinguish between keys like left and right shift which share a common key code. The result will be one ofKEY_LOCATION_UNKNOWN,KEY_LOCATION_STANDARD,KEY_LOCATION_LEFT,KEY_LOCATION_RIGHT, orKEY_LOCATION_NUMPAD.
- Returns:
- the key location
- Since:
- 1.4
public static String getKeyModifiersText(int modifiers)
Returns a string describing the modifiers, such as "Shift" or "Ctrl+Button1". XXX Sun claims this can be localized via the awt.properties file - how do we implement that?
- Parameters:
modifiers- the old-style modifiers to convert to text
- Returns:
- a string representation of the modifiers in this bitmask
public static String getKeyText(int keyCode)
Returns the text name of key code, such as "HOME", "F1", or "A". XXX Sun claims this can be localized via the awt.properties file - how do we implement that?
- Returns:
- the text name of the key code
public boolean isActionKey()
Tests whether or not this key is an action key. An action key typically does not fire a KEY_TYPED event, and is not a modifier.
- Returns:
- true if this is an action key
public String paramString()
Returns a string identifying the event. This is formatted as the field name of the id type, followed by the keyCode, then the keyChar, modifiers (if any), extModifiers (if any), and keyLocation.
- Overrides:
- paramString in interface ComponentEvent
- Returns:
- a string identifying the event
public void setKeyChar(char keyChar)
Sets the Unicode character for this event to the specified value.
- Parameters:
keyChar- the new Unicode character for this event
public void setKeyCode(int keyCode)
Sets the key code for this event. This must be one of theVK_*constants defined in this class.
- Parameters:
keyCode- the new key code for this event
public void setModifiers(int modifiers)
Deprecated.
Sets the modifier keys to the specified value. This should be a union of the bit mask constants fromInputEvent. The use of this method is not recommended, particularly for KEY_TYPED events, which do not check if the modifiers were changed.
- Parameters:
modifiers- the new modifier value, in either old or new style
- See Also:
InputEvent