Class KeyEvent
- java.lang.Object
-
- java.util.EventObject
-
- java.awt.AWTEvent
-
- java.awt.event.ComponentEvent
-
- java.awt.event.InputEvent
-
- java.awt.event.KeyEvent
-
- All Implemented Interfaces:
- java.io.Serializable
public class KeyEvent extends InputEventAn event which indicates that a keystroke occurred in a component.This low-level event is generated by a component object (such as a text field) when a key is pressed, released, or typed. The event is passed to every
KeyListenerorKeyAdapterobject which registered to receive such events using the component'saddKeyListenermethod. (KeyAdapterobjects implement theKeyListenerinterface.) Each such listener object gets thisKeyEventwhen the event occurs."Key typed" events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input. In the simplest case, a key typed event is produced by a single key press (e.g., 'a'). Often, however, characters are produced by series of key presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to key typed events may be many-to-one or many-to-many. Key releases are not usually necessary to generate a key typed event, but there are some cases where the key typed event is not generated until a key is released (e.g., entering ASCII sequences via the Alt-Numpad method in Windows). No key typed events are generated for keys that don't generate Unicode characters (e.g., action keys, modifier keys, etc.).
The getKeyChar method always returns a valid Unicode character or CHAR_UNDEFINED. Character input is reported by KEY_TYPED events: KEY_PRESSED and KEY_RELEASED events are not necessarily associated with character input. Therefore, the result of the getKeyChar method is guaranteed to be meaningful only for KEY_TYPED events.
For key pressed and key released events, the getKeyCode method returns the event's keyCode. For key typed events, the getKeyCode method always returns
VK_UNDEFINED. ThegetExtendedKeyCodemethod may also be used with many international keyboard layouts."Key pressed" and "key released" events are lower-level and depend on the platform and keyboard layout. They are generated whenever a key is pressed or released, and are the only way to find out about keys that don't generate character input (e.g., action keys, modifier keys, etc.). The key being pressed or released is indicated by the
getKeyCodeandgetExtendedKeyCodemethods, which return a virtual key code.Virtual key codes are used to report which keyboard key has been pressed, rather than a character generated by the combination of one or more keystrokes (such as "A", which comes from shift and "a").
For example, pressing the Shift key will cause a KEY_PRESSED event with a VK_SHIFT keyCode, while pressing the 'a' key will result in a VK_A keyCode. After the 'a' key is released, a KEY_RELEASED event will be fired with VK_A. Separately, a KEY_TYPED event with a keyChar value of 'A' is generated.
Pressing and releasing a key on the keyboard results in the generating the following key events (in order):
But in some cases (e.g. auto-repeat or input method is activated) the order could be different (and platform dependent).KEY_PRESSEDKEY_TYPED(is only generated if a valid Unicode character could be generated.)KEY_RELEASEDNotes:
- Key combinations which do not result in Unicode characters, such as action keys like F1 and the HELP key, do not generate KEY_TYPED events.
- Not all keyboards or systems are capable of generating all virtual key codes. No attempt is made in Java to generate these keys artificially.
- Virtual key codes do not identify a physical key: they depend on the platform and keyboard layout. For example, the key that generates VK_Q when using a U.S. keyboard layout will generate VK_A when using a French keyboard layout.
- The key that generates
VK_Qwhen using a U.S. keyboard layout also generates a unique code for Russian or Hebrew layout. There is no aVK_constant for these and many other codes in various layouts. These codes may be obtained by usinggetExtendedKeyCodeand are used whenever aVK_constant is used. - Not all characters have a keycode associated with them. For example, there is no keycode for the question mark because there is no keyboard for which it appears on the primary layer.
- In order to support the platform-independent handling of action keys, the Java platform uses a few additional virtual key constants for functions that would otherwise have to be recognized by interpreting virtual key codes and modifiers. For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES is returned instead of VK_CONVERT with the ALT modifier.
- As specified in Focus Specification key events are dispatched to the focus owner by default.
WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants. Sun reserves the right to change these values as needed to accomodate a wider range of keyboards in the future.
An unspecified behavior will be caused if the
idparameter of any particularKeyEventinstance is not in the range fromKEY_FIRSTtoKEY_LAST.- Since:
- 1.1
- See Also:
KeyAdapter,KeyListener, Tutorial: Writing a Key Listener, Serialized Form
-
-
Fields Modifier and Type Field and Description 
ACTION_EVENT_MASK 
The event mask for selecting action events.
ADJUSTMENT_EVENT_MASK 
The event mask for selecting adjustment events.
ALT_DOWN_MASK 
The Alt key extended modifier constant.
ALT_GRAPH_DOWN_MASK 
The AltGraph key extended modifier constant.
ALT_GRAPH_MASK 
The AltGraph key modifier constant.
ALT_MASK 
The Alt key modifier constant.
BUTTON1_DOWN_MASK 
The Mouse Button1 extended modifier constant.
BUTTON1_MASK 
The Mouse Button1 modifier constant.
BUTTON2_DOWN_MASK 
The Mouse Button2 extended modifier constant.
BUTTON2_MASK 
The Mouse Button2 modifier constant.
BUTTON3_DOWN_MASK 
The Mouse Button3 extended modifier constant.
BUTTON3_MASK 
The Mouse Button3 modifier constant.
CHAR_UNDEFINED 
KEY_PRESSED and KEY_RELEASED events which do not map to a valid Unicode character use this for the keyChar value.
COMPONENT_EVENT_MASK 
The event mask for selecting component events.
COMPONENT_FIRST 
The first number in the range of ids used for component events.
COMPONENT_HIDDEN 
This event indicates that the component was rendered invisible.
COMPONENT_LAST 
The last number in the range of ids used for component events.
COMPONENT_MOVED 
This event indicates that the component's position changed.
COMPONENT_RESIZED 
This event indicates that the component's size changed.
COMPONENT_SHOWN 
This event indicates that the component was made visible.
consumed 
Controls whether or not the event is sent back down to the peer once the source has processed it - false means it's sent to the peer; true means it's not.
CONTAINER_EVENT_MASK 
The event mask for selecting container events.
CTRL_DOWN_MASK 
The Control key extended modifier constant.
CTRL_MASK 
The Control key modifier constant.
FOCUS_EVENT_MASK 
The event mask for selecting focus events.
HIERARCHY_BOUNDS_EVENT_MASK 
The event mask for selecting hierarchy bounds events.
HIERARCHY_EVENT_MASK 
The event mask for selecting hierarchy events.
id 
The event's id.
INPUT_METHOD_EVENT_MASK 
The event mask for selecting input method events.
INVOCATION_EVENT_MASK 
The event mask for selecting invocation events.
ITEM_EVENT_MASK 
The event mask for selecting item events.
KEY_EVENT_MASK 
The event mask for selecting key events.
KEY_FIRST 
The first number in the range of ids used for key events.
KEY_LAST 
The last number in the range of ids used for key events.
KEY_LOCATION_LEFT 
A constant indicating that the key pressed or released is in the left key location (there is more than one possible location for this key).
KEY_LOCATION_NUMPAD 
A constant indicating that the key event originated on the numeric keypad or with a virtual key corresponding to the numeric keypad.
KEY_LOCATION_RIGHT 
A constant indicating that the key pressed or released is in the right key location (there is more than one possible location for this key).
KEY_LOCATION_STANDARD 
A constant indicating that the key pressed or released is not distinguished as the left or right version of a key, and did not originate on the numeric keypad (or did not originate with a virtual key corresponding to the numeric keypad).
KEY_LOCATION_UNKNOWN 
A constant indicating that the keyLocation is indeterminate or not relevant.
KEY_PRESSED 
The "key pressed" event.
KEY_RELEASED 
The "key released" event.
KEY_TYPED 
The "key typed" event.
META_DOWN_MASK 
The Meta key extended modifier constant.
META_MASK 
The Meta key modifier constant.
MOUSE_EVENT_MASK 
The event mask for selecting mouse events.
MOUSE_MOTION_EVENT_MASK 
The event mask for selecting mouse motion events.
MOUSE_WHEEL_EVENT_MASK 
The event mask for selecting mouse wheel events.
PAINT_EVENT_MASK 
The event mask for selecting paint events.
RESERVED_ID_MAX 
The maximum value for reserved AWT event IDs.
SHIFT_DOWN_MASK 
The Shift key extended modifier constant.
SHIFT_MASK 
The Shift key modifier constant.
source 
The object on which the Event initially occurred.
TEXT_EVENT_MASK 
The event mask for selecting text events.
VK_0 
VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
VK_1 


VK_2 


VK_3 


VK_4 


VK_5 


VK_6 


VK_7 


VK_8 


VK_9 


VK_A 
VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
VK_ACCEPT 
Constant for the Accept or Commit function key.
VK_ADD 


VK_AGAIN 


VK_ALL_CANDIDATES 
Constant for the All Candidates function key.
VK_ALPHANUMERIC 
Constant for the Alphanumeric function key.
VK_ALT 


VK_ALT_GRAPH 
Constant for the AltGraph function key.
VK_AMPERSAND 


VK_ASTERISK 


VK_AT 
Constant for the "@" key.
VK_B 


VK_BACK_QUOTE 


VK_BACK_SLASH 
Constant for the back slash key, "\"
VK_BACK_SPACE 


VK_BEGIN 
Constant for the Begin key.
VK_BRACELEFT 


VK_BRACERIGHT 


VK_C 


VK_CANCEL 


VK_CAPS_LOCK 


VK_CIRCUMFLEX 
Constant for the "^" key.
VK_CLEAR 


VK_CLOSE_BRACKET 
Constant for the close bracket key, "]"
VK_CODE_INPUT 
Constant for the Code Input function key.
VK_COLON 
Constant for the ":" key.
VK_COMMA 
Constant for the comma key, ","
VK_COMPOSE 
Constant for the Compose function key.
VK_CONTEXT_MENU 
Constant for the Microsoft Windows Context Menu key.
VK_CONTROL 


VK_CONVERT 
Constant for the Convert function key.
VK_COPY 


VK_CUT 


VK_D 


VK_DEAD_ABOVEDOT 


VK_DEAD_ABOVERING 


VK_DEAD_ACUTE 


VK_DEAD_BREVE 


VK_DEAD_CARON 


VK_DEAD_CEDILLA 


VK_DEAD_CIRCUMFLEX 


VK_DEAD_DIAERESIS 


VK_DEAD_DOUBLEACUTE 


VK_DEAD_GRAVE 


VK_DEAD_IOTA 


VK_DEAD_MACRON 


VK_DEAD_OGONEK 


VK_DEAD_SEMIVOICED_SOUND 


VK_DEAD_TILDE 


VK_DEAD_VOICED_SOUND 


VK_DECIMAL 


VK_DELETE 


VK_DIVIDE 


VK_DOLLAR 
Constant for the "$" key.
VK_DOWN 
Constant for the non-numpad down arrow key.
VK_E 


VK_END 


VK_ENTER 


VK_EQUALS 
Constant for the equals key, "="
VK_ESCAPE 


VK_EURO_SIGN 
Constant for the Euro currency sign key.
VK_EXCLAMATION_MARK 
Constant for the "!" key.
VK_F 


VK_F1 
Constant for the F1 function key.
VK_F10 
Constant for the F10 function key.
VK_F11 
Constant for the F11 function key.
VK_F12 
Constant for the F12 function key.
VK_F13 
Constant for the F13 function key.
VK_F14 
Constant for the F14 function key.
VK_F15 
Constant for the F15 function key.
VK_F16 
Constant for the F16 function key.
VK_F17 
Constant for the F17 function key.
VK_F18 
Constant for the F18 function key.
VK_F19 
Constant for the F19 function key.
VK_F2 
Constant for the F2 function key.
VK_F20 
Constant for the F20 function key.
VK_F21 
Constant for the F21 function key.
VK_F22 
Constant for the F22 function key.
VK_F23 
Constant for the F23 function key.
VK_F24 
Constant for the F24 function key.
VK_F3 
Constant for the F3 function key.
VK_F4 
Constant for the F4 function key.
VK_F5 
Constant for the F5 function key.
VK_F6 
Constant for the F6 function key.
VK_F7 
Constant for the F7 function key.
VK_F8 
Constant for the F8 function key.
VK_F9 
Constant for the F9 function key.
VK_FINAL 


VK_FIND 


VK_FULL_WIDTH 
Constant for the Full-Width Characters function key.
VK_G 


VK_GREATER 


VK_H 


VK_HALF_WIDTH 
Constant for the Half-Width Characters function key.
VK_HELP 


VK_HIRAGANA 
Constant for the Hiragana function key.
VK_HOME 


VK_I 


VK_INPUT_METHOD_ON_OFF 
Constant for the input method on/off key.
VK_INSERT 


VK_INVERTED_EXCLAMATION_MARK 
Constant for the inverted exclamation mark key.
VK_J 


VK_JAPANESE_HIRAGANA 
Constant for the Japanese-Hiragana function key.
VK_JAPANESE_KATAKANA 
Constant for the Japanese-Katakana function key.
VK_JAPANESE_ROMAN 
Constant for the Japanese-Roman function key.
VK_K 


VK_KANA 


VK_KANA_LOCK 
Constant for the locking Kana function key.
VK_KANJI 


VK_KATAKANA 
Constant for the Katakana function key.
VK_KP_DOWN 
Constant for the numeric keypad down arrow key.
VK_KP_LEFT 
Constant for the numeric keypad left arrow key.
VK_KP_RIGHT 
Constant for the numeric keypad right arrow key.
VK_KP_UP 
Constant for the numeric keypad up arrow key.
VK_L 


VK_LEFT 
Constant for the non-numpad left arrow key.
VK_LEFT_PARENTHESIS 
Constant for the "(" key.
VK_LESS 


VK_M 


VK_META 


VK_MINUS 
Constant for the minus key, "-"
VK_MODECHANGE 


VK_MULTIPLY 


VK_N 


VK_NONCONVERT 
Constant for the Don't Convert function key.
VK_NUM_LOCK 


VK_NUMBER_SIGN 
Constant for the "#" key.
VK_NUMPAD0 


VK_NUMPAD1 


VK_NUMPAD2 


VK_NUMPAD3 


VK_NUMPAD4 


VK_NUMPAD5 


VK_NUMPAD6 


VK_NUMPAD7 


VK_NUMPAD8 


VK_NUMPAD9 


VK_O 


VK_OPEN_BRACKET 
Constant for the open bracket key, "["
VK_P 


VK_PAGE_DOWN 


VK_PAGE_UP 


VK_PASTE 


VK_PAUSE 


VK_PERIOD 
Constant for the period key, "."
VK_PLUS 
Constant for the "+" key.
VK_PREVIOUS_CANDIDATE 
Constant for the Previous Candidate function key.
VK_PRINTSCREEN 


VK_PROPS 


VK_Q 


VK_QUOTE 


VK_QUOTEDBL 


VK_R 


VK_RIGHT 
Constant for the non-numpad right arrow key.
VK_RIGHT_PARENTHESIS 
Constant for the ")" key.
VK_ROMAN_CHARACTERS 
Constant for the Roman Characters function key.
VK_S 


VK_SCROLL_LOCK 


VK_SEMICOLON 
Constant for the semicolon key, ";"
VK_SEPARATER 
This constant is obsolete, and is included only for backwards compatibility.
VK_SEPARATOR 
Constant for the Numpad Separator key.
VK_SHIFT 


VK_SLASH 
Constant for the forward slash key, "/"
VK_SPACE 


VK_STOP 


VK_SUBTRACT 


VK_T 


VK_TAB 


VK_U 


VK_UNDEFINED 
This value is used to indicate that the keyCode is unknown.
VK_UNDERSCORE 
Constant for the "_" key.
VK_UNDO 


VK_UP 
Constant for the non-numpad up arrow key.
VK_V 


VK_W 


VK_WINDOWS 
Constant for the Microsoft Windows "Windows" key.
VK_X 


VK_Y 


VK_Z 


WINDOW_EVENT_MASK 
The event mask for selecting window events.
WINDOW_FOCUS_EVENT_MASK 
The event mask for selecting window focus events.
WINDOW_STATE_EVENT_MASK 
The event mask for selecting window state events.
-
Static Methods Modifier and Type Static Method and Description 
getExtendedKeyCodeForChar 
Returns an extended key code for a unicode character.
getKeyModifiersText 
Returns aStringdescribing the modifier key(s), such as "Shift", or "Ctrl+Shift".
getKeyText 
Returns a String describing the keyCode, such as "HOME", "F1" or "A".
getMaskForButton 
A method to obtain a mask for any existing mouse button.
getModifiersExText 
Returns a String describing the extended modifier keys and mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".
-
Constructors Constructor and Description 
KeyEvent 
Deprecated as of JDK1.1
KeyEvent 
Constructs aKeyEventobject.
KeyEvent 
Constructs aKeyEventobject.
-
Methods Modifier and Type Method and Description 
consume 
Consumes this event so that it will not be processed in the default manner by the source which originated it.
getComponent 
Returns the originator of the event.
getExtendedKeyCode 
Returns an extended key code for the event.
getID 
Returns the event type.
getKeyChar 
Returns the character associated with the key in this event.
getKeyCode 
Returns the integer keyCode associated with the key in this event.
getKeyLocation 
Returns the location of the key that originated this key event.
getModifiers 
Returns the modifier mask for this event.
getModifiersEx 
Returns the extended modifier mask for this event.
getSource 
The object on which the Event initially occurred.
getWhen 
Returns the difference in milliseconds between the timestamp of when this event occurred and midnight, January 1, 1970 UTC.
isActionKey 
Returns whether the key in this event is an "action" key.
isAltDown 
Returns whether or not the Alt modifier is down on this event.
isAltGraphDown 
Returns whether or not the AltGraph modifier is down on this event.
isConsumed 
Returns whether or not this event has been consumed.
isControlDown 
Returns whether or not the Control modifier is down on this event.
isMetaDown 
Returns whether or not the Meta modifier is down on this event.
isShiftDown 
Returns whether or not the Shift modifier is down on this event.
paramString 
Returns a parameter string identifying this event.
setKeyChar 
Set the keyChar value to indicate a logical character.
setKeyCode 
Set the keyCode value to indicate a physical key.
setModifiers 
Deprecated as of JDK1.1.4
setSource 
Retargets an event to a new source.
toString 
Returns a String representation of this object.
-
