APIdia Launch Partner
Top Description Fields Constructors Methods
javafx.scene.input

public final Class KeyEvent

extends InputEvent
Class Inheritance
Imports
com.sun.javafx.tk.Toolkit, javafx.beans.NamedArg, javafx.event.EventTarget, .EventType

An event which indicates that a keystroke occurred in a javafx.scene.Node.

This event is generated when a key is pressed, released, or typed. Depending on the type of the event it is passed to onKeyPressed, onKeyTyped or onKeyReleased function.

"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 character variable always contains a valid Unicode character(s) 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 character variable is guaranteed to be meaningful only for key typed events.

For key pressed and key released events, the code variable contains the event's key code. For key typed events, the code variable always contains KeyCode.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 code variable, which contains a virtual key code.

On Mac and Linux dead key sequences are handled using input method events instead of key events. See InputMethodEvent.

For triggering context menus see the ContextMenuEvent.

Since
JavaFX 2.0

Field Summary

Modifier and TypeField and Description
private final boolean
altDown

Returns whether or not the Alt modifier is down on this event.

public static final EventType<KeyEvent>
ANY

Hides javafx.scene.input.InputEvent.ANY.

Common supertype for all key event types.
public static final String
CHAR_UNDEFINED

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

private final String
character

The Unicode character or sequence of characters associated with the key typed event.

private final KeyCode
code

The integer key code associated with the key in this key pressed or key released event.

private final boolean
controlDown

Returns whether or not the Control modifier is down on this event.

public static final EventType<KeyEvent>
KEY_PRESSED

This event occurs when a key has been pressed.

public static final EventType<KeyEvent>
KEY_RELEASED

This event occurs when a key has been released.

public static final EventType<KeyEvent>
KEY_TYPED

This event occurs when a character-generating key was typed (pressed and released).

private final boolean
metaDown

Returns whether or not the Meta modifier is down on this event.

private static final long
private final boolean
shiftDown

Returns whether or not the Shift modifier is down on this event.

private final String
text

A String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events.

Constructor Summary

AccessConstructor and Description
public
KeyEvent(Object
the source of the event. Can be null.
source
,
EventTarget
the target of the event. Can be null.
target
,
EventType<KeyEvent>
The type of the event.
eventType
,
String
The character or sequence of characters associated with the event
character
,
String
A String describing the key code
text
,
KeyCode
The integer key code
code
,
boolean
true if shift modifier was pressed.
shiftDown
,
boolean
true if control modifier was pressed.
controlDown
,
boolean
true if alt modifier was pressed.
altDown
,
boolean
true if meta modifier was pressed.
metaDown
)

Constructs a new KeyEvent event from the specified parameters.

public
KeyEvent(EventType<KeyEvent>
The type of the event.
eventType
,
String
The character or sequence of characters associated with the event
character
,
String
A String describing the key code
text
,
KeyCode
The integer key code
code
,
boolean
true if shift modifier was pressed.
shiftDown
,
boolean
true if control modifier was pressed.
controlDown
,
boolean
true if alt modifier was pressed.
altDown
,
boolean
true if meta modifier was pressed.
metaDown
)

Constructs a new KeyEvent event from the specified parameters, with a null source and target.

Method Summary

Modifier and TypeMethod and Description
public KeyEvent
copyFor(Object newSource, EventTarget newTarget)

Overrides javafx.event.Event.copyFor.

Creates and returns a copy of this event with the specified event source and target.

public KeyEvent

Returns:

the event copy with the fields substituted
copyFor
(Object
the new source of the copied event
source
,
EventTarget
the new target of the copied event
target
,
EventType<KeyEvent>
the new event type.
type
)

Creates a copy of the given event with the given fields substituted.

public final String

Returns:

The Unicode character(s) associated with the key typed event
getCharacter
()

The Unicode character or sequence of characters associated with the key typed event.

public final KeyCode

Returns:

The key code associated with the key in this event, KeyCode.UNDEFINED for key typed event
getCode
()

The key code associated with the key in this key pressed or key released event.

public EventType<KeyEvent>
getEventType()

Overrides javafx.scene.input.InputEvent.getEventType.

Gets the event type of this event.

public final String

Returns:

A String describing the key code
getText
()

A String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events.

public final boolean

Returns:

whether or not the Alt modifier is down on this event.
isAltDown
()

Returns whether or not the Alt modifier is down on this event.

public final boolean

Returns:

whether or not the Control modifier is down on this event.
isControlDown
()

Returns whether or not the Control modifier is down on this event.

public final boolean

Returns:

whether or not the Meta modifier is down on this event.
isMetaDown
()

Returns whether or not the Meta modifier is down on this event.

public final boolean

Returns:

whether or not the Shift modifier is down on this event.
isShiftDown
()

Returns whether or not the Shift modifier is down on this event.

public final boolean

Returns:

true if the shortcut modifier is down, false otherwise
isShortcutDown
()

Returns whether or not the host platform common shortcut modifier is down on this event.

public String

Returns:

a string representation of this KeyEvent object.
toString
()

Overrides java.util.EventObject.toString.

Returns a string representation of this KeyEvent object.

Field Detail

altDownback to summary
private final boolean altDown

Returns whether or not the Alt modifier is down on this event.

ANYback to summary
public static final EventType<KeyEvent> ANY

Hides javafx.scene.input.InputEvent.ANY.

Common supertype for all key event types.

CHAR_UNDEFINEDback to summary
public static final String CHAR_UNDEFINED

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

characterback to summary
private final String character

The Unicode character or sequence of characters associated with the key typed event. Contains multiple elements if the key produced a single Unicode character from outside of the Basic Multilingual Plane which needs to be encoded by the corresponding surrogate pair in Java or if the key produced multiple Unicode characters itself.

For example, character will have the value "A" for a key typed event generated by pressing SHIFT + 'a'. For key pressed and key released events, character is always CHAR_UNDEFINED.

codeback to summary
private final KeyCode code

The integer key code associated with the key in this key pressed or key released event. For key typed events, code is always KeyCode.UNDEFINED.

controlDownback to summary
private final boolean controlDown

Returns whether or not the Control modifier is down on this event.

KEY_PRESSEDback to summary
public static final EventType<KeyEvent> KEY_PRESSED

This event occurs when a key has been pressed.

KEY_RELEASEDback to summary
public static final EventType<KeyEvent> KEY_RELEASED

This event occurs when a key has been released.

KEY_TYPEDback to summary
public static final EventType<KeyEvent> KEY_TYPED

This event occurs when a character-generating key was typed (pressed and released). The event contains the character field containing the typed string, the code and text fields are not used.

metaDownback to summary
private final boolean metaDown

Returns whether or not the Meta modifier is down on this event.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides javafx.scene.input.InputEvent.serialVersionUID.

shiftDownback to summary
private final boolean shiftDown

Returns whether or not the Shift modifier is down on this event.

textback to summary
private final String text

A String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events. For key typed events, text is always the empty string.

Constructor Detail

KeyEventback to summary
public KeyEvent(Object source, EventTarget target, EventType<KeyEvent> eventType, String character, String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown)

Constructs a new KeyEvent event from the specified parameters.

Parameters
source:Object

the source of the event. Can be null.

target:EventTarget

the target of the event. Can be null.

eventType:EventType<KeyEvent>

The type of the event.

character:String

The character or sequence of characters associated with the event

text:String

A String describing the key code

code:KeyCode

The integer key code

shiftDown:boolean

true if shift modifier was pressed.

controlDown:boolean

true if control modifier was pressed.

altDown:boolean

true if alt modifier was pressed.

metaDown:boolean

true if meta modifier was pressed.

Since
JavaFX 8.0
KeyEventback to summary
public KeyEvent(EventType<KeyEvent> eventType, String character, String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown)

Constructs a new KeyEvent event from the specified parameters, with a null source and target.

Parameters
eventType:EventType<KeyEvent>

The type of the event.

character:String

The character or sequence of characters associated with the event

text:String

A String describing the key code

code:KeyCode

The integer key code

shiftDown:boolean

true if shift modifier was pressed.

controlDown:boolean

true if control modifier was pressed.

altDown:boolean

true if alt modifier was pressed.

metaDown:boolean

true if meta modifier was pressed.

Since
JavaFX 8.0

Method Detail

copyForback to summary
public KeyEvent copyFor(Object newSource, EventTarget newTarget)

Overrides javafx.event.Event.copyFor.

Doc from javafx.event.Event.copyFor.

Creates and returns a copy of this event with the specified event source and target. If the source or target is set to null, it is replaced by the NULL_SOURCE_TARGET value.

Annotations
@Override
copyForback to summary
public KeyEvent copyFor(Object source, EventTarget target, EventType<KeyEvent> type)

Creates a copy of the given event with the given fields substituted.

Parameters
source:Object

the new source of the copied event

target:EventTarget

the new target of the copied event

type:EventType<KeyEvent>

the new event type.

Returns:KeyEvent

the event copy with the fields substituted

Since
JavaFX 8.0
getCharacterback to summary
public final String getCharacter()

The Unicode character or sequence of characters associated with the key typed event. Contains multiple elements if the key produced a single Unicode character from outside of the Basic Multilingual Plane which needs to be encoded by the corresponding surrogate pair in Java or if the key produced multiple Unicode characters itself.

For example, character will have the value "A" for a key typed event generated by pressing SHIFT + 'a'. For key pressed and key released events, character is always CHAR_UNDEFINED.

Returns:String

The Unicode character(s) associated with the key typed event

getCodeback to summary
public final KeyCode getCode()

The key code associated with the key in this key pressed or key released event. For key typed events, code is always KeyCode.UNDEFINED.

Returns:KeyCode

The key code associated with the key in this event, KeyCode.UNDEFINED for key typed event

getEventTypeback to summary
public EventType<KeyEvent> getEventType()

Overrides javafx.scene.input.InputEvent.getEventType.

Doc from javafx.event.Event.getEventType.

Gets the event type of this event. Objects of the same Event class can have different event types. These event types further specify what kind of event occurred.

Annotations
@Override
getTextback to summary
public final String getText()

A String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events. For key typed events, text is always the empty string.

Returns:String

A String describing the key code

isAltDownback to summary
public final boolean isAltDown()

Returns whether or not the Alt modifier is down on this event.

Returns:boolean

whether or not the Alt modifier is down on this event.

isControlDownback to summary
public final boolean isControlDown()

Returns whether or not the Control modifier is down on this event.

Returns:boolean

whether or not the Control modifier is down on this event.

isMetaDownback to summary
public final boolean isMetaDown()

Returns whether or not the Meta modifier is down on this event.

Returns:boolean

whether or not the Meta modifier is down on this event.

isShiftDownback to summary
public final boolean isShiftDown()

Returns whether or not the Shift modifier is down on this event.

Returns:boolean

whether or not the Shift modifier is down on this event.

isShortcutDownback to summary
public final boolean isShortcutDown()

Returns whether or not the host platform common shortcut modifier is down on this event. This common shortcut modifier is a modifier key which is used commonly in shortcuts on the host platform. It is for example control on Windows and meta (command key) on Mac.

Returns:boolean

true if the shortcut modifier is down, false otherwise

toStringback to summary
public String toString()

Overrides java.util.EventObject.toString.

Returns a string representation of this KeyEvent object.

Returns:String

a string representation of this KeyEvent object.

Annotations
@Override