- All Implemented Interfaces:
- Serializable,- Cloneable
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.
 For triggering context menus see the ContextMenuEvent.
- Since:
- JavaFX 2.0
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionCommon supertype for all key event types.static final StringKEY_PRESSED and KEY_RELEASED events which do not map to a valid Unicode character use this for the keyChar value.This event occurs when a key has been pressed.This event occurs when a key has been released.This event occurs when a character-generating key was typed (pressed and released).Fields declared in class javafx.event.Eventconsumed, eventType, NULL_SOURCE_TARGET, targetFields declared in class java.util.EventObjectsource
- 
Constructor SummaryConstructorsConstructorDescriptionKeyEvent(Object source, EventTarget target, EventType<KeyEvent> eventType, String character, String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown) Constructs a newKeyEventevent from the specified parameters.KeyEvent(EventType<KeyEvent> eventType, String character, String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown) Constructs a newKeyEventevent from the specified parameters, with anullsource and target.
- 
Method SummaryModifier and TypeMethodDescriptioncopyFor(Object newSource, EventTarget newTarget) Creates and returns a copy of this event with the specified event source and target.copyFor(Object source, EventTarget target, EventType<KeyEvent> type) Creates a copy of the given event with the given fields substituted.final StringThe Unicode character or sequence of characters associated with the key typed event.final KeyCodegetCode()The key code associated with the key in this key pressed or key released event.Gets the event type of this event.final StringgetText()A String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events.final booleanReturns whether or not the Alt modifier is down on this event.final booleanReturns whether or not the Control modifier is down on this event.final booleanReturns whether or not the Meta modifier is down on this event.final booleanReturns whether or not the Shift modifier is down on this event.final booleanReturns whether or not the host platform common shortcut modifier is down on this event.toString()Returns a string representation of thisKeyEventobject.Methods declared in class java.util.EventObjectgetSource
- 
Field Details- 
ANYCommon supertype for all key event types.
- 
KEY_PRESSEDThis event occurs when a key has been pressed.
- 
KEY_RELEASEDThis event occurs when a key has been released.
- 
KEY_TYPEDThis event occurs when a character-generating key was typed (pressed and released). The event contains thecharacterfield containing the typed string, thecodeandtextfields are not used.
- 
CHAR_UNDEFINEDKEY_PRESSED and KEY_RELEASED events which do not map to a valid Unicode character use this for the keyChar value.
 
- 
- 
Constructor Details- 
KeyEventpublic KeyEvent(Object source, EventTarget target, EventType<KeyEvent> eventType, String character, String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown) Constructs a newKeyEventevent from the specified parameters.- Parameters:
- source- the source of the event. Can be null.
- target- the target of the event. Can be null.
- eventType- The type of the event.
- character- The character or sequence of characters associated with the event
- text- A String describing the key code
- code- The integer key code
- shiftDown- true if shift modifier was pressed.
- controlDown- true if control modifier was pressed.
- altDown- true if alt modifier was pressed.
- metaDown- true if meta modifier was pressed.
- Since:
- JavaFX 8.0
 
- 
KeyEventpublic KeyEvent(EventType<KeyEvent> eventType, String character, String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown) Constructs a newKeyEventevent from the specified parameters, with anullsource and target.- Parameters:
- eventType- The type of the event.
- character- The character or sequence of characters associated with the event
- text- A String describing the key code
- code- The integer key code
- shiftDown- true if shift modifier was pressed.
- controlDown- true if control modifier was pressed.
- altDown- true if alt modifier was pressed.
- metaDown- true if meta modifier was pressed.
- Since:
- JavaFX 8.0
 
 
- 
- 
Method Details- 
getCharacterThe 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, characterwill have the value "A" for a key typed event generated by pressing SHIFT + 'a'. For key pressed and key released events,characteris alwaysCHAR_UNDEFINED.- Returns:
- The Unicode character(s) associated with the key typed event
 
- 
getTextA String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events. For key typed events,textis always the empty string.- Returns:
- A String describing the key code
 
- 
getCodeThe key code associated with the key in this key pressed or key released event. For key typed events,codeis alwaysKeyCode.UNDEFINED.- Returns:
- The key code associated with the key in this event,
 KeyCode.UNDEFINEDfor key typed event
 
- 
isShiftDownpublic final boolean isShiftDown()Returns whether or not the Shift modifier is down on this event.- Returns:
- whether or not the Shift modifier is down on this event.
 
- 
isControlDownpublic final boolean isControlDown()Returns whether or not the Control modifier is down on this event.- Returns:
- whether or not the Control modifier is down on this event.
 
- 
isAltDownpublic final boolean isAltDown()Returns whether or not the Alt modifier is down on this event.- Returns:
- whether or not the Alt modifier is down on this event.
 
- 
isMetaDownpublic final boolean isMetaDown()Returns whether or not the Meta modifier is down on this event.- Returns:
- whether or not the Meta modifier is down on this event.
 
- 
isShortcutDownpublic 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 examplecontrolon Windows andmeta(command key) on Mac.- Returns:
- trueif the shortcut modifier is down,- falseotherwise
 
- 
toStringReturns a string representation of thisKeyEventobject.- Overrides:
- toStringin class- EventObject
- Returns:
- a string representation of this KeyEventobject.
 
- 
copyForDescription copied from class:EventCreates and returns a copy of this event with the specified event source and target. If the source or target is set tonull, it is replaced by theNULL_SOURCE_TARGETvalue.
- 
copyForCreates a copy of the given event with the given fields substituted.- Parameters:
- source- the new source of the copied event
- target- the new target of the copied event
- type- the new event type.
- Returns:
- the event copy with the fields substituted
- Since:
- JavaFX 8.0
 
- 
getEventTypeDescription copied from class:EventGets the event type of this event. Objects of the sameEventclass can have different event types. These event types further specify what kind of event occurred.- Overrides:
- getEventTypein class- Event
- Returns:
- the event type
 
 
-