Keyboard and gamepad focus
Every button can take keyboard and gamepad focus in every runtime. With nothing set, arrows and the d-pad move to the nearest button in that direction. Set neighbors when the layout needs something else.
"focus": {
"down": "<button id>", "up": "<button id>", "left": "…", "right": "…",
"next": "…", "prev": "…",
"initial": true,
"cancel": true,
"ring": { "color": "$ember", "width": 1, "offset": 0 }
}
- Arrows, d-pad, left stick go to
up,down,left,right, else to the nearest button by position. - Tab and Shift Tab follow
nextandprev, else the natural order. - initial focuses that button when the screen opens. Give every menu one; leave it off HUDs, where a focused button would take the game’s keys. On a screen without one nothing is focused until the player presses Tab (on the web, also a gamepad direction). An overlay’s initial button wins; closing the overlay returns focus to the button that opened it.
- cancel marks an overlay’s back button. Escape and gamepad B press it while the screen is open as an overlay, so its actions run (an emit, then the close). Without one they close the overlay. Like
initial, it isn’t inherited from a component. - ring draws an outline while a button has keyboard or gamepad focus, not after a mouse click. The hover look applies on focus too.
- A neighbor that’s disabled or hidden is skipped.
- On a focused slider or select, left and right (keys, d-pad, stick) change the value and don’t move focus, so they never use the
leftandrightneighbors. Up, down and Tab move focus as on any button. A disabled control lets left and right move focus. - While a text input has the keyboard, keys belong to the field. Focus doesn’t move, and Escape or gamepad B ends the typing without writing. A tooltip also shows while a button has focus.
- A button’s Focus sound plays when the player moves focus to it, not when your code focuses it.
Document checks flag a neighbor that doesn’t exist, isn’t a button or sits on another screen.
In the inspector
Select a button and open the Interact tab. The Gamepad focus section has:
- Focus first when the screen opens: the
initialbutton. - Esc / B press it (on an overlay): the
cancelbutton. - A compass with a list for each direction, plus Tab and Shift Tab. Auto follows position for the arrows and layer order for Tab. Pick a button to override it.
- Focus ring, with its color, width (W) and gap from the button (Gap).
On a component, mark the first focus and the Esc / B button on its instances, on each screen: a component doesn’t pass them on.
Set the order for a whole screen
Flows › Gamepad focus shows one screen at a time, with a number on each button in focus order.
- Drag rows in Focus order, or press Alt ↑ and Alt ↓ on a row, to change the order. Double-click a row to open the button in Design.
- D-pad picks Up / down or Left / right, and Wrap leads the last button back to the first.
- First focus picks the button that takes focus when the screen opens.
- Focus ring turns the ring on or off for every button in the list.
- Until you press Set this order, arrows follow positions and Tab follows layer order. Set this order writes explicit neighbors, and Follow positions removes them.
Try it in Preview
Press Ctrl Enter to preview, then use the arrow keys, Tab and Enter, or a gamepad, as a player would. Esc closes the top overlay first, pressing its cancel button, and then closes Preview.
Runtimes
- Web: keys work on the focused button;
ui.focusFirstButton()focuses the initial button, else the first one. Gamepads are read with the Gamepad API: d-pad or left stick move (held directions repeat), A presses, B and Escape close the top overlay through its cancel button. Passgamepad: falseorcancelClosesOverlay: falseto turn them off. If your game has its own Escape handler (a Pause toggle), skip events withe.defaultPrevented. - Godot: neighbors become
focus_neighbor_*,focus_nextandfocus_previous; unset directions use Godot’s own search.ui_acceptpresses andui_cancel(Escape, gamepad B) closes the top overlay through its cancel button. Callui.focus_first_button()to start navigation: it focuses the initial button, else the first one. With nothing focused, Tab lands there too.
Try it on the home page: click the menu and use the arrow keys.