java.awt.event
Class KeyEvent

public class KeyEvent
extends java.awt.event.InputEvent
An 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 KeyListener or KeyAdapter object which registered to receive such events using the component's addKeyListener method. (KeyAdapter objects implement the KeyListener interface.) Each such listener object gets this KeyEvent when 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.

"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 getKeyCode method, which returns 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.

Notes:

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.

Version:
1.75 05/18/04
Author:
Carl Quinn
Amy Fowler
Norbert Lindenberg
Since:
1.1
See Also:
KeyAdapter
KeyListener
Tutorial: Writing a Key Listener
Reference: The Java Class Libraries (update file)
Field Detail

isProxyActive

private boolean isProxyActive
Stores the state of native event dispatching system - true, if when the event was created event proxying mechanism was active - false, if it was inactive Used in Component.dispatchEventImpl to correctly dispatch events when proxy is active

KEY_FIRST

public static final int KEY_FIRST
The first number in the range of ids used for key events.

KEY_LAST

public static final int KEY_LAST
The last number in the range of ids used for key events.

KEY_TYPED

public static final int KEY_TYPED
The "key typed" event. This event is generated when a character is entered. In the simplest case, it is produced by a single key press. Often, however, characters are produced by series of key presses, and the mapping from key pressed events to key typed events may be many-to-one or many-to-many.

KEY_PRESSED

public static final int KEY_PRESSED
The "key pressed" event. This event is generated when a key is pushed down.

KEY_RELEASED

public static final int KEY_RELEASED
The "key released" event. This event is generated when a key is let up.

VK_LEFT

public static final int VK_LEFT
Constant for the non-numpad left arrow key.
See Also:
#VK_KP_LEFT

VK_UP

public static final int VK_UP
Constant for the non-numpad up arrow key.
See Also:
#VK_KP_UP

VK_RIGHT

public static final int VK_RIGHT
Constant for the non-numpad right arrow key.
See Also:
#VK_KP_RIGHT

VK_DOWN

public static final int VK_DOWN
Constant for the non-numpad down arrow key.
See Also:
#VK_KP_DOWN

VK_COMMA

public static final int VK_COMMA
Constant for the comma key, ","

VK_MINUS

public static final int VK_MINUS
Constant for the minus key, "-"
Since:
1.2

VK_PERIOD

public static final int VK_PERIOD
Constant for the period key, "."

VK_SLASH

public static final int VK_SLASH
Constant for the forward slash key, "/"

VK_0

public static final int VK_0
VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)

VK_SEMICOLON

public static final int VK_SEMICOLON
Constant for the semicolon key, ";"

VK_EQUALS

public static final int VK_EQUALS
Constant for the equals key, "="

VK_A

public static final int VK_A
VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)

VK_OPEN_BRACKET

public static final int VK_OPEN_BRACKET
Constant for the open bracket key, "["

VK_BACK_SLASH

public static final int VK_BACK_SLASH
Constant for the back slash key, "\"

VK_CLOSE_BRACKET

public static final int VK_CLOSE_BRACKET
Constant for the close bracket key, "]"

VK_SEPARATER

public static final int VK_SEPARATER
This constant is obsolete, and is included only for backwards compatibility.
See Also:
#VK_SEPARATOR

VK_SEPARATOR

public static final int VK_SEPARATOR
Constant for the Numpad Separator key.
Since:
1.4

VK_F1

public static final int VK_F1
Constant for the F1 function key.

VK_F2

public static final int VK_F2
Constant for the F2 function key.

VK_F3

public static final int VK_F3
Constant for the F3 function key.

VK_F4

public static final int VK_F4
Constant for the F4 function key.

VK_F5

public static final int VK_F5
Constant for the F5 function key.

VK_F6

public static final int VK_F6
Constant for the F6 function key.

VK_F7

public static final int VK_F7
Constant for the F7 function key.

VK_F8

public static final int VK_F8
Constant for the F8 function key.

VK_F9

public static final int VK_F9
Constant for the F9 function key.

VK_F10

public static final int VK_F10
Constant for the F10 function key.

VK_F11

public static final int VK_F11
Constant for the F11 function key.

VK_F12

public static final int VK_F12
Constant for the F12 function key.

VK_F13

public static final int VK_F13
Constant for the F13 function key.
Since:
1.2

VK_F14

public static final int VK_F14
Constant for the F14 function key.
Since:
1.2

VK_F15

public static final int VK_F15
Constant for the F15 function key.
Since:
1.2

VK_F16

public static final int VK_F16
Constant for the F16 function key.
Since:
1.2

VK_F17

public static final int VK_F17
Constant for the F17 function key.
Since:
1.2

VK_F18

public static final int VK_F18
Constant for the F18 function key.
Since:
1.2

VK_F19

public static final int VK_F19
Constant for the F19 function key.
Since:
1.2

VK_F20

public static final int VK_F20
Constant for the F20 function key.
Since:
1.2

VK_F21

public static final int VK_F21
Constant for the F21 function key.
Since:
1.2

VK_F22

public static final int VK_F22
Constant for the F22 function key.
Since:
1.2

VK_F23

public static final int VK_F23
Constant for the F23 function key.
Since:
1.2

VK_F24

public static final int VK_F24
Constant for the F24 function key.
Since:
1.2

VK_KP_UP

public static final int VK_KP_UP
Constant for the numeric keypad up arrow key.
Since:
1.2
See Also:
#VK_UP

VK_KP_DOWN

public static final int VK_KP_DOWN
Constant for the numeric keypad down arrow key.
Since:
1.2
See Also:
#VK_DOWN

VK_KP_LEFT

public static final int VK_KP_LEFT
Constant for the numeric keypad left arrow key.
Since:
1.2
See Also:
#VK_LEFT

VK_KP_RIGHT

public static final int VK_KP_RIGHT
Constant for the numeric keypad right arrow key.
Since:
1.2
See Also:
#VK_RIGHT

VK_DEAD_GRAVE

public static final int VK_DEAD_GRAVE
Since:
1.2

VK_DEAD_ACUTE

public static final int VK_DEAD_ACUTE
Since:
1.2

VK_DEAD_CIRCUMFLEX

public static final int VK_DEAD_CIRCUMFLEX
Since:
1.2

VK_DEAD_TILDE

public static final int VK_DEAD_TILDE
Since:
1.2

VK_DEAD_MACRON

public static final int VK_DEAD_MACRON
Since:
1.2

VK_DEAD_BREVE

public static final int VK_DEAD_BREVE
Since:
1.2

VK_DEAD_ABOVEDOT

public static final int VK_DEAD_ABOVEDOT
Since:
1.2

VK_DEAD_DIAERESIS

public static final int VK_DEAD_DIAERESIS
Since:
1.2

VK_DEAD_ABOVERING

public static final int VK_DEAD_ABOVERING
Since:
1.2

VK_DEAD_DOUBLEACUTE

public static final int VK_DEAD_DOUBLEACUTE
Since:
1.2

VK_DEAD_CARON

public static final int VK_DEAD_CARON
Since:
1.2

VK_DEAD_CEDILLA

public static final int VK_DEAD_CEDILLA
Since:
1.2

VK_DEAD_OGONEK

public static final int VK_DEAD_OGONEK
Since:
1.2

VK_DEAD_IOTA

public static final int VK_DEAD_IOTA
Since:
1.2

VK_DEAD_VOICED_SOUND

public static final int VK_DEAD_VOICED_SOUND
Since:
1.2

VK_DEAD_SEMIVOICED_SOUND

public static final int VK_DEAD_SEMIVOICED_SOUND
Since:
1.2

VK_AMPERSAND

public static final int VK_AMPERSAND
Since:
1.2

VK_ASTERISK

public static final int VK_ASTERISK
Since:
1.2

VK_QUOTEDBL

public static final int VK_QUOTEDBL
Since:
1.2

VK_LESS

public static final int VK_LESS
Since:
1.2

VK_GREATER

public static final int VK_GREATER
Since:
1.2

VK_BRACELEFT

public static final int VK_BRACELEFT
Since:
1.2

VK_BRACERIGHT

public static final int VK_BRACERIGHT
Since:
1.2

VK_AT

public static final int VK_AT
Constant for the "@" key.
Since:
1.2

VK_COLON

public static final int VK_COLON
Constant for the ":" key.
Since:
1.2

VK_CIRCUMFLEX

public static final int VK_CIRCUMFLEX
Constant for the "^" key.
Since:
1.2

VK_DOLLAR

public static final int VK_DOLLAR
Constant for the "$" key.
Since:
1.2

VK_EURO_SIGN

public static final int VK_EURO_SIGN
Constant for the Euro currency sign key.
Since:
1.2

VK_EXCLAMATION_MARK

public static final int VK_EXCLAMATION_MARK
Constant for the "!" key.
Since:
1.2

VK_INVERTED_EXCLAMATION_MARK

public static final int VK_INVERTED_EXCLAMATION_MARK
Constant for the inverted exclamation mark key.
Since:
1.2

VK_LEFT_PARENTHESIS

public static final int VK_LEFT_PARENTHESIS
Constant for the "(" key.
Since:
1.2

VK_NUMBER_SIGN

public static final int VK_NUMBER_SIGN
Constant for the "#" key.
Since:
1.2

VK_PLUS

public static final int VK_PLUS
Constant for the "+" key.
Since:
1.2

VK_RIGHT_PARENTHESIS

public static final int VK_RIGHT_PARENTHESIS
Constant for the ")" key.
Since:
1.2

VK_UNDERSCORE

public static final int VK_UNDERSCORE
Constant for the "_" key.
Since:
1.2

VK_WINDOWS

public static final int VK_WINDOWS
Constant for the Microsoft Windows "Windows" key. It is used for both the left and right version of the key.
Since:
1.5
See Also:
#getKeyLocation()

VK_CONTEXT_MENU

public static final int VK_CONTEXT_MENU
Constant for the Microsoft Windows Context Menu key.
Since:
1.5

VK_CONVERT

public static final int VK_CONVERT
Constant for the Convert function key.

VK_NONCONVERT

public static final int VK_NONCONVERT
Constant for the Don't Convert function key.

VK_ACCEPT

public static final int VK_ACCEPT
Constant for the Accept or Commit function key.

VK_ALPHANUMERIC

public static final int VK_ALPHANUMERIC
Constant for the Alphanumeric function key.
Since:
1.2

VK_KATAKANA

public static final int VK_KATAKANA
Constant for the Katakana function key.
Since:
1.2

VK_HIRAGANA

public static final int VK_HIRAGANA
Constant for the Hiragana function key.
Since:
1.2

VK_FULL_WIDTH

public static final int VK_FULL_WIDTH
Constant for the Full-Width Characters function key.
Since:
1.2

VK_HALF_WIDTH

public static final int VK_HALF_WIDTH
Constant for the Half-Width Characters function key.
Since:
1.2

VK_ROMAN_CHARACTERS

public static final int VK_ROMAN_CHARACTERS
Constant for the Roman Characters function key.
Since:
1.2

VK_ALL_CANDIDATES

public static final int VK_ALL_CANDIDATES
Constant for the All Candidates function key.
Since:
1.2

VK_PREVIOUS_CANDIDATE

public static final int VK_PREVIOUS_CANDIDATE
Constant for the Previous Candidate function key.
Since:
1.2

VK_CODE_INPUT

public static final int VK_CODE_INPUT
Constant for the Code Input function key.
Since:
1.2

VK_JAPANESE_KATAKANA

public static final int VK_JAPANESE_KATAKANA
Constant for the Japanese-Katakana function key. This key switches to a Japanese input method and selects its Katakana input mode.
Since:
1.2

VK_JAPANESE_HIRAGANA

public static final int VK_JAPANESE_HIRAGANA
Constant for the Japanese-Hiragana function key. This key switches to a Japanese input method and selects its Hiragana input mode.
Since:
1.2

VK_JAPANESE_ROMAN

public static final int VK_JAPANESE_ROMAN
Constant for the Japanese-Roman function key. This key switches to a Japanese input method and selects its Roman-Direct input mode.
Since:
1.2

VK_KANA_LOCK

public static final int VK_KANA_LOCK
Constant for the locking Kana function key. This key locks the keyboard into a Kana layout.
Since:
1.3

VK_INPUT_METHOD_ON_OFF

public static final int VK_INPUT_METHOD_ON_OFF
Constant for the input method on/off key.
Since:
1.3

VK_CUT

public static final int VK_CUT
Since:
1.2

VK_COPY

public static final int VK_COPY
Since:
1.2

VK_PASTE

public static final int VK_PASTE
Since:
1.2

VK_UNDO

public static final int VK_UNDO
Since:
1.2

VK_AGAIN

public static final int VK_AGAIN
Since:
1.2

VK_FIND

public static final int VK_FIND
Since:
1.2

VK_PROPS

public static final int VK_PROPS
Since:
1.2

VK_STOP

public static final int VK_STOP
Since:
1.2

VK_COMPOSE

public static final int VK_COMPOSE
Constant for the Compose function key.
Since:
1.2

VK_ALT_GRAPH

public static final int VK_ALT_GRAPH
Constant for the AltGraph function key.
Since:
1.2

VK_BEGIN

public static final int VK_BEGIN
Constant for the Begin key.
Since:
1.5

VK_UNDEFINED

public static final int VK_UNDEFINED
This value is used to indicate that the keyCode is unknown. KEY_TYPED events do not have a keyCode value; this value is used instead.

CHAR_UNDEFINED

public static final char CHAR_UNDEFINED
KEY_PRESSED and KEY_RELEASED events which do not map to a valid Unicode character use this for the keyChar value.

KEY_LOCATION_UNKNOWN

public static final int KEY_LOCATION_UNKNOWN
A constant indicating that the keyLocation is indeterminate or not relevant. KEY_TYPED events do not have a keyLocation; this value is used instead.
Since:
1.4

KEY_LOCATION_STANDARD

public static final int 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).
Since:
1.4

KEY_LOCATION_LEFT

public static final int 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). Example: the left shift key.
Since:
1.4

KEY_LOCATION_RIGHT

public static final int 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). Example: the right shift key.
Since:
1.4

KEY_LOCATION_NUMPAD

public static final int 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.
Since:
1.4

keyCode

int keyCode
The unique value assigned to each of the keys on the keyboard. There is a common set of key codes that can be fired by most keyboards. The symbolic name for a key code should be used rather than the code value itself.
See Also:
#getKeyCode()
#setKeyCode(int)

keyChar

char keyChar
keyChar is a valid unicode character that is fired by a key or a key combination on a keyboard.
See Also:
#getKeyChar()
#setKeyChar(char)

keyLocation

int keyLocation
The location of the key on the keyboard. Some keys occur more than once on a keyboard, e.g. the left and right shift keys. Additionally, some keys occur on the numeric keypad. This variable is used to distinguish such keys. The only legal values are KEY_LOCATION_UNKNOWN, KEY_LOCATION_STANDARD, KEY_LOCATION_LEFT, KEY_LOCATION_RIGHT, and KEY_LOCATION_NUMPAD.
See Also:
#getKeyLocation()

Constructor Detail

KeyEvent

private KeyEvent(java.awt.Component source,
                 int id,
                 long when,
                 int modifiers,
                 int keyCode,
                 char keyChar,
                 int keyLocation,
                 boolean isProxyActive)
Constructs a KeyEvent object.

Note that passing in an invalid id results in unspecified behavior. This method throws an IllegalArgumentException if source is null.

Parameters:
source - the Component that originated the event
id - an integer identifying the type of event
when - a long integer that specifies the time the event occurred
modifiers - the modifier keys down during event (shift, ctrl, alt, meta) Either extended _DOWN_MASK or old _MASK modifiers should be used, but both models should not be mixed in one event. Use of the extended modifiers is preferred.
keyCode - the integer code for an actual key, or VK_UNDEFINED (for a key-typed event)
keyChar - the Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character)
keyLocation - identifies the key location. The only legal values are KEY_LOCATION_UNKNOWN, KEY_LOCATION_STANDARD, KEY_LOCATION_LEFT, KEY_LOCATION_RIGHT, and KEY_LOCATION_NUMPAD.
Throws:
IllegalArgumentException - if id is KEY_TYPED and keyChar is CHAR_UNDEFINED; or if id is KEY_TYPED and keyCode is not VK_UNDEFINED; or if id is KEY_TYPED and keyLocation is not KEY_LOCATION_UNKNOWN; or if keyLocation is not one of the legal values enumerated above.
IllegalArgumentException - if source is null
Since:
1.4

KeyEvent

public KeyEvent(java.awt.Component source,
                int id,
                long when,
                int modifiers,
                int keyCode,
                char keyChar)
Constructs a KeyEvent object.

Note that passing in an invalid id results in unspecified behavior. This method throws an IllegalArgumentException if source is null.

Parameters:
source - the Component that originated the event
id - an integer identifying the type of event
when - a long integer that specifies the time the event occurred
modifiers - the modifier keys down during event (shift, ctrl, alt, meta) Either extended _DOWN_MASK or old _MASK modifiers should be used, but both models should not be mixed in one event. Use of the extended modifiers is preferred.
keyCode - the integer code for an actual key, or VK_UNDEFINED (for a key-typed event)
keyChar - the Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character)
Throws:
IllegalArgumentException - if id is KEY_TYPED and keyChar is CHAR_UNDEFINED; or if id is KEY_TYPED and keyCode is not VK_UNDEFINED
IllegalArgumentException - if source is null

KeyEvent

public KeyEvent(java.awt.Component source,
                int id,
                long when,
                int modifiers,
                int keyCode)
Deprecated.  as of JDK1.1

Method Detail

initIDs

private static native void initIDs()
Initialize JNI field and method IDs for fields that may be accessed from C.

getKeyCode

public int getKeyCode()
Returns the integer keyCode associated with the key in this event.
Returns:
the integer code for an actual key on the keyboard. (For KEY_TYPED events, the keyCode is VK_UNDEFINED.)

setKeyCode

public void setKeyCode(int keyCode)
Set the keyCode value to indicate a physical key.
Parameters:
keyCode - an integer corresponding to an actual key on the keyboard.

getKeyChar

public char getKeyChar()
Returns the character associated with the key in this event. For example, the KEY_TYPED event for shift + "a" returns the value for "A".

KEY_PRESSED and KEY_RELEASED events are not intended for reporting of character input. Therefore, the values returned by this method are guaranteed to be meaningful only for KEY_TYPED events.

Returns:
the Unicode character defined for this key event. If no valid Unicode character exists for this key event, CHAR_UNDEFINED is returned.

setKeyChar

public void setKeyChar(char keyChar)
Set the keyChar value to indicate a logical character.
Parameters:
keyChar - a char corresponding to to the combination of keystrokes that make up this event.

setModifiers

public void setModifiers(int modifiers)
Deprecated.  as of JDK1.1.4
Set the modifiers to indicate additional keys that were held down (e.g. shift, ctrl, alt, meta) defined as part of InputEvent.

NOTE: use of this method is not recommended, because many AWT implementations do not recognize modifier changes. This is especially true for KEY_TYPED events where the shift modifier is changed.

Parameters:
modifiers - an integer combination of the modifier constants.
See Also:
InputEvent

getKeyLocation

public int getKeyLocation()
Returns the location of the key that originated this key event. Some keys occur more than once on a keyboard, e.g. the left and right shift keys. Additionally, some keys occur on the numeric keypad. This provides a way of distinguishing such keys.
Returns:
the location of the key that was pressed or released. Always returns KEY_LOCATION_UNKNOWN for KEY_TYPED events.
Since:
1.4

getKeyText

public static java.lang.String getKeyText(int keyCode)
Returns a String describing the keyCode, such as "HOME", "F1" or "A". These strings can be localized by changing the awt.properties file.
Returns:
a string containing a text description for a physical key, identified by its keyCode

getKeyModifiersText

public static java.lang.String getKeyModifiersText(int modifiers)
Returns a String describing the modifier key(s), such as "Shift", or "Ctrl+Shift". These strings can be localized by changing the awt.properties file.

Note that InputEvent.ALT_MASK and InputEvent.BUTTON2_MASK have the same value, so the string "Alt" is returned for both modifiers. Likewise, InputEvent.META_MASK and InputEvent.BUTTON3_MASK have the same value, so the string "Meta" is returned for both modifiers.

Returns:
string a text description of the combination of modifier keys that were held down during the event
See Also:
InputEvent#getModifiersExText(int)

isActionKey

public boolean isActionKey()
Returns whether the key in this event is an "action" key. Typically an action key does not fire a unicode character and is not a modifier key.
Returns:
true if the key is an "action" key, false otherwise

paramString

public java.lang.String paramString()
Returns a parameter string identifying this event. This method is useful for event logging and for debugging.
Returns:
a string identifying the event and its attributes

setNewModifiers

private void setNewModifiers()
Sets new modifiers by the old ones. The key modifiers override overlaping mouse modifiers.

setOldModifiers

private void setOldModifiers()
Sets old modifiers by the new ones.

readObject

private void readObject(java.io.ObjectInputStream s)
Sets new modifiers by the old ones. The key modifiers override overlaping mouse modifiers.