Controller UX
Reading the controller is half the work. This page covers what players notice next: whether the game tells them
what to press, survives a dead battery, works in menus, and lets them move a button they do not like. Every snippet
uses hub and p1 from Start a new game.
Ask for a button press
Section titled “Ask for a button press”Browsers hide controllers from a page until the player presses a button on one. Until then, say so:
function renderHint() { const connected = p1.input.getState().connected; hint.textContent = connected ? '' : 'Press any button on your controller';}renderHint();hub.onCapabilitiesChange(renderHint);Keyboard players can ignore the hint, so keep it small and never block the game on it.
Show the player’s buttons
Section titled “Show the player’s buttons”The same position is A on Xbox, Cross on PlayStation and B on Nintendo. Name the button the player holds, and update the prompts when they switch controllers:
import { getButtonLabel } from '@gamepadly/sdk';
function renderPrompts() { const brand = p1.getCapabilities().identity.brand; jumpPrompt.textContent = `${getButtonLabel('south', brand)} Jump`; firePrompt.textContent = `${getButtonLabel('r2', brand)} Fire`; // RT, R2 or ZR}renderPrompts();hub.onCapabilitiesChange(renderPrompts);Pause when the controller disconnects
Section titled “Pause when the controller disconnects”A controller can drop at any time: a flat battery, a cable pulled, Bluetooth out of range. Pause the game rather than let the character run on:
let hadController = false;
hub.onCapabilitiesChange(({ slots }) => { if (slots.length > 0) { hadController = true; } else if (hadController) { pauseGame('Controller disconnected. Reconnect it, or keep playing on the keyboard.'); }});hadController keeps the game from pausing for players who never used a controller. With several players, check
each player instead: see Multiple players.
A menu needs its own action map, with directions that repeat while held. repeated() is true on the press, again
after 400 ms and then every 100 ms, so holding down scrolls through a list at a steady pace:
const menu = p1.createActions({ actions: { up: ['up', 'ly-', 'ArrowUp'], down: ['down', 'ly+', 'ArrowDown'], confirm: ['south'], // Enter and Space already click a focused button back: ['east', 'Escape'], },});const items = Array.from(document.querySelectorAll<HTMLButtonElement>('#menu button'));
function updateMenu() { // Start from the focused button, so Tab and the mouse stay in charge too. let index = Math.max(0, items.indexOf(document.activeElement as HTMLButtonElement)); if (menu.repeated('down')) index = (index + 1) % items.length; if (menu.repeated('up')) index = (index + items.length - 1) % items.length; if (index !== items.indexOf(document.activeElement as HTMLButtonElement)) items[index]?.focus();
if (menu.justPressed('confirm')) items[index]?.click(); if (menu.justPressed('back')) closeMenu();}Keep the menu as real <button> elements and move focus between them. The browser then draws the focus ring, screen
readers follow along, and the keyboard and mouse work without extra code. Leave Enter and Space out of confirm:
on a focused button the browser already turns them into a click, and binding them would click twice.
Going from the menu to the game
Section titled “Going from the menu to the game”The button that starts the game is often the same one that jumps. If the game reads its actions straight after the menu closes, it sees South held and jumps on the first frame. Two ways to avoid it:
-
Update every action map every frame, even the ones you are not reading. The press then counts as already held when the game starts reading:
function frame(now: number) {const state = p1.input.getState();menu.update(state, now);actions.update(state, now);if (menuOpen) updateMenu();else updateGame();requestAnimationFrame(frame);} -
Or, if you create the game’s action map when the game starts, pass
ignoreHeldOnStart: true. Actions whose inputs are held at that moment stay released until they are let go.
Let players remap buttons
Section titled “Let players remap buttons”Some players want jump on another button, or need it there. The action map can change its bindings while the game runs, and gives them back as plain data you can save:
import { getButtonLabel, isControllerBinding, type ActionBinding } from '@gamepadly/sdk';
type Action = 'jump' | 'fire';const SAVE_KEY = 'my-game:bindings';
const actions = p1.createActions<Action>({ actions: { jump: ['south', 'Space'], fire: ['r2', 'KeyJ'] },});const saved = localStorage.getItem(SAVE_KEY);if (saved) actions.setBindings(JSON.parse(saved));
let remapping: Action | null = null;let listening = false;
function startRemap(action: Action) { remapping = action; listening = false; remapPrompt.textContent = `Press a button or key for ${action}. Escape cancels.`;}
// Call once per frame, after actions.update(), instead of the menu and game code while remapping.function updateRemap() { if (!remapping) return; if (!listening) { listening = true; // skip one frame: the click, Enter or button press that opened this is not a choice return; } const input = actions.lastPressed(); if (!input || input.startsWith('Mouse')) return; if (input !== 'Escape') { // Replace only the controller side or only the keyboard side, so the other keeps working. const kept = actions.getBindings().actions[remapping].filter((b) => isControllerBinding(b) !== isControllerBinding(input)); actions.setBindings({ actions: { [remapping]: [input, ...kept] } }); localStorage.setItem(SAVE_KEY, JSON.stringify(actions.getBindings())); } remapping = null; remapPrompt.textContent = '';}
function bindingLabel(binding: ActionBinding): string { return getButtonLabel(binding, p1.getCapabilities().identity.brand); // 'Cross', 'RT'; keys stay 'KeyJ'}What the action map does for you:
lastPressed()reports what went down in the latestupdate(): a button by its position name ('south','east', …),'l2'/'r2', a stick direction ('lx-', …), a key code or a mouse button. The example skips the frame the remap screen opens on and ignores mouse buttons, so the click, Enter or button press that opened it is never taken as the new binding.- After
setBindings()the action stays released until the new button is let go, so the press that picked it does not also jump. setBindings()ignores actions the map does not declare, so bindings saved by an older version of your game load without errors. Renamed actions fall back to their defaults.
Remap movement the same way, with axes in setBindings(). Offer a “Reset to defaults” that removes the saved
bindings and recreates the action map.
Checklist
Section titled “Checklist”- A “Press any button” hint shows until a controller is detected, and never blocks keyboard players.
- Prompts use the label on the player’s controller and update when they switch.
- The game pauses when the controller disconnects mid-game.
- Menus work with the D-pad and the left stick, repeat while held, and keep keyboard and mouse working.
- Closing a menu does not trigger a game action.
- Players can remap actions, and the game remembers it.
- Haptics mark important moments, not every frame: see Haptics.