Input and actions
Action maps
Section titled “Action maps”createActions() fuses controller, keyboard, and mouse into named actions, so game code asks “did the player
jump?” instead of checking three input devices.
const actions = p1.createActions({ actions: { jump: ['south', 'Space'], fire: ['r2', 'MouseLeft'], pause: ['start', 'Escape'], aimUp: ['ry-'], }, axes: { moveX: { stick: 'lx', negativeKeys: ['KeyA'], positiveKeys: ['KeyD'] }, moveY: { stick: 'ly', negativeButtons: ['up'], positiveButtons: ['down'], negativeKeys: ['KeyW'], positiveKeys: ['KeyS'] }, },});Bindings can be:
| Binding | Examples |
|---|---|
| Button names | 'south', 'east', 'l1', 'r3', 'up', 'start' (see Buttons and layouts) |
| Analog triggers | 'l2', 'r2' (pressed above analogThreshold, default 0.45) |
| Stick directions | 'lx+', 'lx-', 'ly+', 'ly-', 'rx+', 'rx-', 'ry+', 'ry-' |
| Keyboard | Any KeyboardEvent.code: 'Space', 'KeyW', 'ArrowLeft' |
| Mouse | 'MouseLeft', 'MouseRight', 'MouseMiddle' |
Call actions.update(p1.input.getState(), now) once per frame, then read:
actions.justPressed('jump'); // true on the first frame onlyactions.isPressed('fire');actions.justReleased('fire');actions.action('fire'); // { pressed, justPressed, justReleased, heldMs, repeated, value }, value is 0..1 (trigger depth)actions.repeated('jump'); // true on the press, then repeatedly while held: for menusactions.axis('moveX'); // -1..1, from the stick, buttons, or keys, whichever is strongestrepeated() fires 400 ms after the press and then every 100 ms; change it with
repeat: { delayMs, intervalMs } in the map. ignoreHeldOnStart: true keeps an action that is already held when the
map is created from firing until it is let go. Players can remap actions at runtime with getBindings(),
setBindings() and lastPressed(). Controller UX shows menus and a remapping
screen.
Axis sticks use a 0.08 deadzone unless you set deadzone on the axis, and invert: true flips the sign.
By default the tracker listens for keyboard and mouse events on window and calls preventDefault() on Space,
arrow keys, and Tab (outside form fields) so the page does not scroll. Pass attachWindowListeners: false,
mouseTarget, or preventDefaultKeys: false to change that.
Raw state
Section titled “Raw state”p1.input.getState() returns a UnifiedInputState snapshot, the same shape for every controller:
const s = p1.input.getState();s.connected; // false until the player presses a button on the pages.source; // 'webhid' | 'gamepad' | 'keyboard_mouse' | 'none's.lx; s.ly; s.rx; s.ry; // -1..1, raw, no deadzones.l2; s.r2; // 0..1s.buttons.cross; // SDK button names, see Buttons and layoutss.gyro; s.accel; s.touch; s.touch2; s.battery; // populated over WebHIDs.mapping; // 'standard' | 'nonstandard' | 'custom' | 'hid' | 'none'Sticks are raw: no software deadzone is applied, which is what a tester needs but rarely what a game wants. Smooth them yourself:
const x = p1.input.applyDeadzone(s.lx, 0.12);Several players
Section titled “Several players”hub.player(1) is P2, hub.player(2) P3, and so on. Each handle has its own input, haptics, triggers, and
createActions(). See Multiple players.