public final class KeyEvent extends InputEvent
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.
For triggering context menus see the ContextMenuEvent
.
Modifier and Type | Field and Description |
---|---|
private boolean |
altDown
Returns whether or not the Alt modifier is down on this event.
|
static <any> |
ANY
Common supertype for all key event types.
|
static java.lang.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 java.lang.String |
character
The Unicode character or sequence of characters associated with the key
typed event.
|
private KeyCode |
code
The integer key code associated with the key in this key
pressed or key released event.
|
private boolean |
controlDown
Returns whether or not the Control modifier is down on this event.
|
static <any> |
KEY_PRESSED
This event occurs when a key has been pressed.
|
static <any> |
KEY_RELEASED
This event occurs when a key has been released.
|
static <any> |
KEY_TYPED
This event occurs when a character-generating key was typed
(pressed and released).
|
private boolean |
metaDown
Returns whether or not the Meta modifier is down on this event.
|
private static long |
serialVersionUID |
private boolean |
shiftDown
Returns whether or not the Shift modifier is down on this event.
|
private java.lang.String |
text
A String describing the key code, such as "HOME", "F1" or "A",
for key pressed and key released events.
|
Constructor and Description |
---|
KeyEvent(<any> eventType,
java.lang.String character,
java.lang.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. |
KeyEvent(java.lang.Object source,
EventTarget target,
<any> eventType,
java.lang.String character,
java.lang.String text,
KeyCode code,
boolean shiftDown,
boolean controlDown,
boolean altDown,
boolean metaDown)
Constructs a new
KeyEvent event from the specified parameters. |
Modifier and Type | Method and Description |
---|---|
KeyEvent |
copyFor(java.lang.Object newSource,
EventTarget newTarget) |
KeyEvent |
copyFor(java.lang.Object source,
EventTarget target,
<any> type)
Creates a copy of the given event with the given fields substituted.
|
java.lang.String |
getCharacter()
The Unicode character or sequence of characters associated with the key
typed event.
|
KeyCode |
getCode()
The key code associated with the key in this key pressed or key released
event.
|
<any> |
getEventType() |
java.lang.String |
getText()
A String describing the key code, such as "HOME", "F1" or "A",
for key pressed and key released events.
|
boolean |
isAltDown()
Returns whether or not the Alt modifier is down on this event.
|
boolean |
isControlDown()
Returns whether or not the Control modifier is down on this event.
|
boolean |
isMetaDown()
Returns whether or not the Meta modifier is down on this event.
|
boolean |
isShiftDown()
Returns whether or not the Shift modifier is down on this event.
|
boolean |
isShortcutDown()
Returns whether or not the host platform common shortcut modifier is
down on this event.
|
java.lang.String |
toString()
Returns a string representation of this
KeyEvent object. |
private static final long serialVersionUID
public static final <any> ANY
public static final <any> KEY_PRESSED
public static final <any> KEY_RELEASED
public static final <any> KEY_TYPED
character
field containing the typed string, the code
and text
fields are not used.public static final java.lang.String CHAR_UNDEFINED
private final java.lang.String character
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
.
private final java.lang.String text
text
is always the empty string.private final KeyCode code
code
is always KeyCode.UNDEFINED
.private final boolean shiftDown
private final boolean controlDown
private final boolean altDown
private final boolean metaDown
public KeyEvent(java.lang.Object source, EventTarget target, <any> eventType, java.lang.String character, java.lang.String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown)
KeyEvent
event from the specified 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 eventtext
- A String describing the key codecode
- The integer key codeshiftDown
- 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.public KeyEvent(<any> eventType, java.lang.String character, java.lang.String text, KeyCode code, boolean shiftDown, boolean controlDown, boolean altDown, boolean metaDown)
KeyEvent
event from the specified parameters,
with a null
source and target.eventType
- The type of the event.character
- The character or sequence of characters associated with the eventtext
- A String describing the key codecode
- The integer key codeshiftDown
- 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.public final java.lang.String getCharacter()
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
.
public final java.lang.String getText()
text
is always the empty string.public final KeyCode getCode()
code
is always KeyCode.UNDEFINED
.KeyCode.UNDEFINED
for key typed eventpublic final boolean isShiftDown()
public final boolean isControlDown()
public final boolean isAltDown()
public final boolean isMetaDown()
public final boolean isShortcutDown()
control
on Windows and meta
(command key) on Mac.true
if the shortcut modifier is down, false
otherwisepublic java.lang.String toString()
KeyEvent
object.KeyEvent
object.public KeyEvent copyFor(java.lang.Object newSource, EventTarget newTarget)
public KeyEvent copyFor(java.lang.Object source, EventTarget target, <any> type)
source
- the new source of the copied eventtarget
- the new target of the copied eventtype
- the new event type.public <any> getEventType()
getEventType
in class InputEvent