Skip to content

Keyboard and shortcuts

The keyboard reaches a UI in two forms: the keys that go down and up, and the characters that they type. This page shows who receives them, how to tell a letter from a place on the keyboard, and how to bind a shortcut.

You will learn

  • How to react to a key and to a typed character
  • Which nodes and UIs receive the keyboard
  • How to tell the letter of a key from its place
  • How to act while a key is held
  • How to bind a shortcut to a UI or a node

The keyboard uses the handle events of Events, on a node or, here, on the UI:

super.handleKeyPressed(event -> this.key.set(event.getKey().name()));
super.handleCharTyped(event -> this.typed.append(new String(Character.toChars(event.getCodepoint()))));

Full file: KeysUI.java

The user types hello: each key goes down once, and each character arrives as a code point.

A key event is a NodeKeyEvent on a node and a UIKeyEvent on a UI; a character event is a NodeCharEvent or a UICharEvent. The release always reaches every node, even hidden or disabled (Let the pointer through a decoration).

The keyboard has no pointer: every visible node of the UI receives each key, wherever the pointer is. For one key that goes down, the order is:

  1. the shortcuts set with overrideFocus(true) (Bind a shortcut);
  2. the nodes, from the front to the back, as for a press (Follow an input through the tree); a focused text field consumes every key;
  3. the handle listeners of the UI;
  4. the other shortcuts;
  5. the shortcuts of dev mode (zoom, reload, inspector), in dev mode only.

Each step runs only when the key is not consumed yet.

Escape closes the top UI when it is closeable, is not an overlay, and no node or listener consumed the key; UI.beforeClose can still keep it open. A focused text field consumes Escape to lose the focus, so the first Escape leaves the field and the second one closes the UI. A key that the top screen does not consume goes on to the UI below it, except under a popup (How UIs stack).

JOID has no focus of its own outside the text fields: a node that needs the keys tests its own state in its listener, and consumes what it handles. Text fields explains the focus of a field.

A key event holds two keys:

  • NodeKeyEvent.getKey is the key that the layout of the user names: on a French AZERTY keyboard, the key at the place of Q is Key.A;
  • NodeKeyEvent.getPhysicalKey is the place of the key on a US QWERTY keyboard, whatever the layout: that same key is Key.Q.

Use getKey() for a shortcut, so that Ctrl + Z is the key that reads Z. Use getPhysicalKey() for a movement, so that W, A, S and D stay under the left hand on every layout. Only the letters and the punctuation depend on the layout; the digits, the arrows, the modifiers and the function keys are the same in both.

A held key sends one event when it goes down and one when it goes up, never a repeat. To act on every frame while it is held, read its state in an update:

RectNode
.create(0, 60, 80, 80)
.color(WalkUI.ACCENT)
.x(this.walkX)
.onUpdate(event -> {
final double direction = (Key.D.isPhysicalDown() ? 1D : 0D) - (Key.A.isPhysicalDown() ? 1D : 0D);
this.walkX.set(Math.max(40D, Math.min(480D, this.walkX.peek() + direction * 300D * super.getFrameTime() / 1000D)));
})
.attach(card);

Full file: WalkUI.java

The square moves 300 design pixels per second while D is held, at any frame rate.

Text fields repeat a held key on their own (FieldNode).

A Shortcut runs a listener when its key goes down with its modifiers:

Shortcut.create(Key.S).ctrl(true).onTrigger(event -> this.saves.increment()).attach(this);
Shortcut.create(Key.F1).overrideFocus(true).onTrigger(event -> this.helps.increment()).attach(this);

Full file: ShortcutUI.java

Ctrl + S counts once before the field gains the focus; F1 counts twice, the second time while the user types in the field.

attach(ui) and attach(node) add the shortcut to a UI or a node, like UI.addShortcut and Node.addShortcut; Shortcut.detach removes it. A shortcut of a node works only while the node is visible and enabled, and the shortcuts of the nodes in front run before the others. A shortcut lives like a listener (Open and close a UI).

Shortcut.create(...) also takes a binding of your own, such as the key that a player chose in the settings of your application. An IKeyResolver tells JOID which Key such a binding means, and JOID asks it again at each key, so a new choice applies at once:

KeyResolver.register(new SettingsKeyResolver());
Shortcut.create(this.settings.getJumpBinding()).onTrigger(event -> this.jump()).attach(this);

The resolver implements supports(binding) and resolve(binding); register it with KeyResolver.register before the first key.

  • handleKeyPressed, handleKeyReleased and handleCharTyped receive the keyboard on a node or a UI.
  • Every visible node receives each key, wherever the pointer is; then the UI listeners, the shortcuts and the keys of the UI, until a step consumes it.
  • Escape closes the top closeable UI when nothing consumed it; a focused field takes the first Escape.
  • getKey() names the key in the layout of the user, for shortcuts; getPhysicalKey() gives its place, for movements.
  • A held key sends no repeat: read isDown() or isPhysicalDown() in an update.
  • A Shortcut runs on its key with its exact modifiers; overrideFocus(true) makes it work while a field has the focus.

Pause with Space. In WalkUI, stop the square while Space is held down.

Solution

Read the key at the start of the same update:

if (Key.SPACE.isDown()) {
return;
}

Save with Ctrl + Shift + S too. In ShortcutUI, make Ctrl + Shift + S count as a save as well.

Solution

The modifiers must match exactly, so add a second shortcut:

Shortcut.create(Key.S).ctrl(true).shift(true).onTrigger(event -> this.saves.increment()).attach(this);

Report a problem with this page