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
React to a key
Section titled “React to a key”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()))));hello: each key goes down once, and each character arrives as a code point.
UI.handleKeyPressedandNode.handleKeyPressedreceive each key that goes down, as aKey.UI.handleKeyReleasedandNode.handleKeyReleasedreceive it when it goes up.UI.handleCharTypedandNode.handleCharTypedreceive each typed character as a code point, with the layout and the Shift key applied; control characters, such as the one of Enter, never arrive there.
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).
Know who receives the keyboard
Section titled “Know who receives the keyboard”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:
- the shortcuts set with
overrideFocus(true)(Bind a shortcut); - 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;
- the
handlelisteners of the UI; - the other shortcuts;
- 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.
Tell a letter from its place
Section titled “Tell a letter from its place”A key event holds two keys:
NodeKeyEvent.getKeyis the key that the layout of the user names: on a French AZERTY keyboard, the key at the place ofQisKey.A;NodeKeyEvent.getPhysicalKeyis the place of the key on a US QWERTY keyboard, whatever the layout: that same key isKey.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.
Act while a key is held
Section titled “Act while a key is held”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);Key.isDowntests a key by its name in the layout, likegetKey().Key.isPhysicalDowntests a key by its place, likegetPhysicalKey().UI.isShiftKeyDown,UI.isCtrlKeyDownandUI.isAltKeyDowntest both keys of a modifier.
Text fields repeat a held key on their own (FieldNode).
Bind a shortcut
Section titled “Bind a shortcut”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);Shortcut.ctrl,Shortcut.altandShortcut.shiftgive the modifiers, and they must match exactly:Shortcut.create(Key.S).ctrl(true)does not run on Ctrl + Shift + S.- A shortcut matches
NodeKeyEvent.getKey, the key that the layout names. Shortcut.onTriggerreceives aShortcutEventand consumes the key: the nodes and the listeners after it never see it.Shortcut.overrideFocus(true)runs the shortcut before the nodes, so that it works while a text field takes the keys.
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).
Use the key bindings of your application
Section titled “Use the key bindings of your application”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,handleKeyReleasedandhandleCharTypedreceive 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()orisPhysicalDown()in an update. - A
Shortcutruns on its key with its exact modifiers;overrideFocus(true)makes it work while a field has the focus.
Try it
Section titled “Try it”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);