# Input and actions

> Read raw controller state or map controller, keyboard, and mouse inputs to named actions and axes.

Source: https://gamepadly.com/sdk/guides/input-and-actions

## 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.

```ts
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](https://gamepadly.com/sdk/guides/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:

```ts
actions.justPressed('jump'); // true on the first frame only
actions.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 menus
actions.axis('moveX'); // -1..1, from the stick, buttons, or keys, whichever is strongest
```

`repeated()` 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](https://gamepadly.com/sdk/guides/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

`p1.input.getState()` returns a `UnifiedInputState` snapshot, the same shape for every controller:

```ts
const s = p1.input.getState();
s.connected; // false until the player presses a button on the page
s.source; // 'webhid' | 'gamepad' | 'keyboard_mouse' | 'none'
s.lx; s.ly; s.rx; s.ry; // -1..1, raw, no deadzone
s.l2; s.r2; // 0..1
s.buttons.cross; // SDK button names, see Buttons and layouts
s.gyro; s.accel; s.touch; s.touch2; s.battery; // populated over WebHID
s.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:

```ts
const x = p1.input.applyDeadzone(s.lx, 0.12);
```

## 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](https://gamepadly.com/sdk/guides/multiplayer).
