Top Description Inners Fields Constructors Methods
jfx.incubator.scene.control.input

public Class KeyBinding

extends Object
Class Inheritance
Imports
java.util.EnumSet, .Objects, javafx.event.EventType, javafx.scene.input.KeyCode, .KeyEvent, com.sun.javafx.PlatformUtil

This immutable class represents a combination of keys which are used in key mappings. A key combination consists of a main key and a set of modifier keys. The main key can be specified by its key code or key character, the latter must match values returned by KeyEvent#getCharacter(). A modifier key is shift, control, alt, meta or shortcut.

This class also provides a set of convenience methods for refering to keys found on macOS platform.

Since
24

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
KeyBinding.Builder

A builder for KeyBinding objects.

private static enum
KeyBinding.KCondition

Condition used to build input key mappings.

Field Summary

Modifier and TypeField and Description
private final Object
private final EnumSet<KeyBinding.KCondition>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static KeyBinding

Returns:

the KeyBinding
alt
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the alt key modifier (option on macOS).

public static KeyBinding.Builder

Returns:

the Builder instance
builder
(KeyCode
the key code
code
)

Creates a Builder with the specified KeyCode.

public static KeyBinding.Builder

Returns:

the Builder instance
builder
(String
the character
character
)

Creates a Builder with the specified character.

public static KeyBinding

Returns:

the KeyBinding, or null
command
(KeyCode
the key code
code
)

This utility method creates a KeyBinding which corresponds to the key press with the specified KeyCode and the macOS ⌘ command key modifier.

public static KeyBinding

Returns:

the KeyBinding
control
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the ctrl key modifier (control on macOS).

public static KeyBinding

Returns:

the KeyBinding
controlShift
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift + ctrl key modifier (control on macOS).

private static KeyBinding
public boolean
equals(Object x)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

pack-priv static KeyBinding

Returns:

the key binding, or null
from
(KeyEvent
the key event
ev
)

Creates a KeyBinding from a KeyEvent, or a null if the event does not correspond to a valid KeyBinding.

public KeyCode

Returns:

key code, or null
getKeyCode
()

Returns the KeyCode, or null if the key binding is not for a key code.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

true if this key binding if for the key press event
isKeyPressed
()

Determines whether this key binding if for the key pressed event.

public boolean

Returns:

true if this key binding if for the key release event
isKeyReleased
()

Determines whether this key binding if for the key released event.

public boolean

Returns:

true if this key binding if for the key typed event
isKeyTyped
()

Determines whether this key binding if for the key typed event.

public static KeyBinding

Returns:

the KeyBinding
of
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode.

public static KeyBinding

Returns:

the KeyBinding, or null
option
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the option key modifier on macOS.

public static KeyBinding

Returns:

the KeyBinding
shift
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift key modifier.

public static KeyBinding

Returns:

the KeyBinding, or null
shiftOption
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift + option key modifiers on macOS.

public static KeyBinding

Returns:

the KeyBinding, or null
shiftShortcut
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift + shortcut key modifier (⌘ command on macOS, ctrl elsewhere).

public static KeyBinding

Returns:

the KeyBinding
shortcut
(KeyCode
the key code
code
)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shortcut key modifier (⌘ command on macOS, ctrl elsewhere).

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public KeyBinding

Returns:

the KeyBinding
withNewKeyCode
(KeyCode
the key code
newCode
)

Creates a new instance of KeyBinding with the new KeyCode and the same set of the modifiers.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

keyback to summary
private final Object key
modifiersback to summary
private final EnumSet<KeyBinding.KCondition> modifiers

Constructor Detail

KeyBindingback to summary
private KeyBinding(Object key, EnumSet<KeyBinding.KCondition> modifiers)

Method Detail

altback to summary
public static KeyBinding alt(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the alt key modifier (option on macOS).

This method is equivalent to option(KeyCode) on macOS.

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

builderback to summary
public static KeyBinding.Builder builder(KeyCode code)

Creates a Builder with the specified KeyCode.

Parameters
code:KeyCode

the key code

Returns:KeyBinding.Builder

the Builder instance

builderback to summary
public static KeyBinding.Builder builder(String character)

Creates a Builder with the specified character. The string must correspond to the value returned by KeyEvent#getCharacter().

Parameters
character:String

the character

Returns:KeyBinding.Builder

the Builder instance

commandback to summary
public static KeyBinding command(KeyCode code)

This utility method creates a KeyBinding which corresponds to the key press with the specified KeyCode and the macOS ⌘ command key modifier.

This method returns null on non-macOS platforms.

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding, or null

controlback to summary
public static KeyBinding control(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the ctrl key modifier (control on macOS).

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

controlShiftback to summary
public static KeyBinding controlShift(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift + ctrl key modifier (control on macOS).

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

createback to summary
private static KeyBinding create(Object key, KeyBinding.KCondition... mods)
equalsback to summary
public boolean equals(Object x)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@Override
fromback to summary
pack-priv static KeyBinding from(KeyEvent ev)

Creates a KeyBinding from a KeyEvent, or a null if the event does not correspond to a valid KeyBinding.

Parameters
ev:KeyEvent

the key event

Returns:KeyBinding

the key binding, or null

getKeyCodeback to summary
public KeyCode getKeyCode()

Returns the KeyCode, or null if the key binding is not for a key code.

Returns:KeyCode

key code, or null

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
isKeyPressedback to summary
public boolean isKeyPressed()

Determines whether this key binding if for the key pressed event.

Returns:boolean

true if this key binding if for the key press event

isKeyReleasedback to summary
public boolean isKeyReleased()

Determines whether this key binding if for the key released event.

Returns:boolean

true if this key binding if for the key release event

isKeyTypedback to summary
public boolean isKeyTyped()

Determines whether this key binding if for the key typed event.

Returns:boolean

true if this key binding if for the key typed event

ofback to summary
public static KeyBinding of(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode.

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

optionback to summary
public static KeyBinding option(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the option key modifier on macOS.

This method returns null on non-macOS platforms. On macOS, it is equivalent to calling alt(KeyCode).

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding, or null

shiftback to summary
public static KeyBinding shift(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift key modifier.

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

shiftOptionback to summary
public static KeyBinding shiftOption(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift + option key modifiers on macOS.

This method returns null on non-macOS platforms.

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding, or null

shiftShortcutback to summary
public static KeyBinding shiftShortcut(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shift + shortcut key modifier (⌘ command on macOS, ctrl elsewhere).

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding, or null

shortcutback to summary
public static KeyBinding shortcut(KeyCode code)

Creates a KeyBinding which corresponds to the key press with the specified KeyCode and the shortcut key modifier (⌘ command on macOS, ctrl elsewhere).

Parameters
code:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
withNewKeyCodeback to summary
public KeyBinding withNewKeyCode(KeyCode newCode)

Creates a new instance of KeyBinding with the new KeyCode and the same set of the modifiers.

Parameters
newCode:KeyCode

the key code

Returns:KeyBinding

the KeyBinding

jfx.incubator.scene.control.input back to summary

public Class KeyBinding.Builder

extends Object
Class Inheritance

A builder for KeyBinding objects.

By default, its build method creates a key KEY_PRESSED binding. This can be changed by calling either keyReleased() or keyTyped().

The builder pattern can be used when convenience methods such as KeyBinding#control(KeyCode) or KeyBinding#shiftShortcut(KeyCode) are not sufficient.

Example: KeyBinding.builder(KeyCode.TAB).control().option().shift().build()

Field Summary

Modifier and TypeField and Description
private final Object
private final EnumSet<KeyBinding.KCondition>
m

Constructor Summary

AccessConstructor and Description
pack-priv
Builder(Object key)

Constructs a Builder

Method Summary

Modifier and TypeMethod and Description
public KeyBinding.Builder

Returns:

this Builder
alt
()

Sets the alt key down condition (the option key on macOS).

public KeyBinding

Returns:

a new key binding instance.
build
()

Creates a new KeyBinding instance from the current settings.

public KeyBinding.Builder

Returns:

this Builder
command
()

Sets the command key down condition on macOS.

public KeyBinding.Builder

Returns:

this Builder
control
()

Sets the control key down condition.

private KeyBinding.Builder
public KeyBinding.Builder

Returns:

the Builder instance
keyReleased
()

Sets the KEY_RELEASED condition, clearing KEY_PRESSED and KEY_TYPED.

public KeyBinding.Builder

Returns:

the Builder instance
keyTyped
()

Sets the KEY_TYPED condition, clearing KEY_PRESSED and KEY_RELEASED.

public KeyBinding.Builder

Returns:

this Builder
meta
()

Sets the meta key down condition.

public KeyBinding.Builder

Returns:

this Builder
option
()

Sets the option key down condition on macOS.

private void
public KeyBinding.Builder

Returns:

this Builder
shift
()

Sets the shift key down condition.

public KeyBinding.Builder

Returns:

this Builder
shortcut
()

Sets the shortcut key down condition.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

keyback to summary
private final Object key
mback to summary
private final EnumSet<KeyBinding.KCondition> m

Constructor Detail

Builderback to summary
pack-priv Builder(Object key)

Constructs a Builder

Method Detail

altback to summary
public KeyBinding.Builder alt()

Sets the alt key down condition (the option key on macOS).

Returns:KeyBinding.Builder

this Builder

buildback to summary
public KeyBinding build()

Creates a new KeyBinding instance from the current settings.

Returns:KeyBinding

a new key binding instance.

commandback to summary
public KeyBinding.Builder command()

Sets the command key down condition on macOS.

Setting this condition on non-macOS platforms will result in the build method returning null.

Returns:KeyBinding.Builder

this Builder

controlback to summary
public KeyBinding.Builder control()

Sets the control key down condition.

Returns:KeyBinding.Builder

this Builder

initback to summary
private KeyBinding.Builder init(KeyBinding.KCondition... mods)
keyReleasedback to summary
public KeyBinding.Builder keyReleased()

Sets the KEY_RELEASED condition, clearing KEY_PRESSED and KEY_TYPED.

Returns:KeyBinding.Builder

the Builder instance

keyTypedback to summary
public KeyBinding.Builder keyTyped()

Sets the KEY_TYPED condition, clearing KEY_PRESSED and KEY_RELEASED.

Returns:KeyBinding.Builder

the Builder instance

metaback to summary
public KeyBinding.Builder meta()

Sets the meta key down condition.

Returns:KeyBinding.Builder

this Builder

optionback to summary
public KeyBinding.Builder option()

Sets the option key down condition on macOS.

Setting this condition on non-macOS platforms will result in the build method returning null.

Returns:KeyBinding.Builder

this Builder

replaceback to summary
private void replace(KeyBinding.KCondition c, KeyBinding.KCondition replaceWith)
shiftback to summary
public KeyBinding.Builder shift()

Sets the shift key down condition.

Returns:KeyBinding.Builder

this Builder

shortcutback to summary
public KeyBinding.Builder shortcut()

Sets the shortcut key down condition.

Returns:KeyBinding.Builder

this Builder

jfx.incubator.scene.control.input back to summary

private final Enum KeyBinding.KCondition

extends Enum<KeyBinding.KCondition>
Class Inheritance

Condition used to build input key mappings.

The KCondition values are used as keys in a hash table, so when the platform sends a key event with multiple modifiers, some modifiers are dropped in order to make the final key binding to function lookup unambiguous.

The mapping is as follows:

KCondition    Mac         Windows/Linux
ALT           OPTION      ALT
COMMAND       COMMAND     (ignored)
CTRL          CTRL        CTRL
META          COMMAND     META
OPTION        OPTION      (ignored)
SHIFT         SHIFT       SHIFT
SHORTCUT      COMMAND     CTRL
WINDOWS       (ignored)   META

Field Summary

Modifier and TypeField and Description
public static final KeyBinding.KCondition
ALT

ALT modifier, mapped to OPTION on Mac, ALT on Windows/Linux

public static final KeyBinding.KCondition
COMMAND

COMMAND modifier, mapped to COMMAND on Mac only

public static final KeyBinding.KCondition
CTRL

CTRL modifier

public static final KeyBinding.KCondition
KEY_PRESSED

a key pressed event

public static final KeyBinding.KCondition
KEY_RELEASED

a key released event

public static final KeyBinding.KCondition
KEY_TYPED

a key typed event

public static final KeyBinding.KCondition
META

META modifier, mapped to COMMAND on Mac, META on Windows/Linux

public static final KeyBinding.KCondition
OPTION

OPTION modifier, mapped to OPTION on Mac only

public static final KeyBinding.KCondition
SHIFT

SHIFT modifier

public static final KeyBinding.KCondition
SHORTCUT

SHORTCUT modifier, mapped to COMMAND on Mac, CTRL on Windows/Linux

public static final KeyBinding.KCondition
WINDOWS

Windows key modifier (⊞), mapped to WINDOWS on Windows only

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static KeyBinding.KCondition
public static KeyBinding.KCondition[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ALTback to summary
public static final KeyBinding.KCondition ALT

ALT modifier, mapped to OPTION on Mac, ALT on Windows/Linux

COMMANDback to summary
public static final KeyBinding.KCondition COMMAND

COMMAND modifier, mapped to COMMAND on Mac only

CTRLback to summary
public static final KeyBinding.KCondition CTRL

CTRL modifier

KEY_PRESSEDback to summary
public static final KeyBinding.KCondition KEY_PRESSED

a key pressed event

KEY_RELEASEDback to summary
public static final KeyBinding.KCondition KEY_RELEASED

a key released event

KEY_TYPEDback to summary
public static final KeyBinding.KCondition KEY_TYPED

a key typed event

METAback to summary
public static final KeyBinding.KCondition META

META modifier, mapped to COMMAND on Mac, META on Windows/Linux

OPTIONback to summary
public static final KeyBinding.KCondition OPTION

OPTION modifier, mapped to OPTION on Mac only

SHIFTback to summary
public static final KeyBinding.KCondition SHIFT

SHIFT modifier

SHORTCUTback to summary
public static final KeyBinding.KCondition SHORTCUT

SHORTCUT modifier, mapped to COMMAND on Mac, CTRL on Windows/Linux

WINDOWSback to summary
public static final KeyBinding.KCondition WINDOWS

Windows key modifier (⊞), mapped to WINDOWS on Windows only

Constructor Detail

KConditionback to summary
private KCondition()

Method Detail

valueOfback to summary
public static KeyBinding.KCondition valueOf(String name)
valuesback to summary
public static KeyBinding.KCondition[] values()