voxolith

makeActions()

Bind named actions to keys, gamepad and on-screen controls. Sources are strings: key:<KeyboardEvent.code>, pad:<PadButton> (0..1), pad:<PadAxis> (-1..1), touch:<id> (a makeTouchControls control; a joystick adds <id>.x and <id>.y), and a trailing + or - for one half of a signed source. Screen and...

function makeActions<B extends Bindings>(input: Input, defaults: B): Actions<B>;

Defined in: engine/src/input/actions.ts:105

Bind named actions to keys, gamepad and on-screen controls. Sources are strings: key:<KeyboardEvent.code>, pad:<PadButton> (0..1), pad:<PadAxis> (-1..1), touch:<id> (a makeTouchControls control; a joystick adds <id>.x and <id>.y), and a trailing + or - for one half of a signed source. Screen and pad Y are down-positive, so "stick pushed up" is pad:LeftY-.

Every bound key has its default action prevented, so bound keys do not scroll the page.

Type Parameters

Type Parameter
B extends Bindings

Parameters

ParameterTypeDescription
inputInputThe surface's input. Call input.update() once per frame before reading.
defaultsBThe action map; it is copied, and bindings() returns the current one.

Returns

Actions<B>

The actions, typed by the names in defaults.

Example

const actions = makeActions(input, {
  strafe: axis({ negative: ["key:KeyA"], positive: ["key:KeyD"], analog: ["pad:LeftX", "touch:move.x"] }),
  // Up is negative on a stick, so each half goes to the opposite side.
  walk: axis({ negative: ["key:KeyS", "pad:LeftY+", "touch:move.y+"], positive: ["key:KeyW", "pad:LeftY-", "touch:move.y-"] }),
  jump: button("key:Space", "pad:A", "touch:jump"),
});
// per frame, after input.update():
const [x, z] = actions.vector("strafe", "walk");
if (actions.pressed("jump")) jump();

On this page