Create

GamepadInput

Gamepad-style unified input that reads from keyboard. Virtual peripherals (dpad, joystick, buttons) can be polled separately and combined with this in the game loop.

GamepadInput

Gamepad-style unified input that reads from keyboard. Virtual peripherals (dpad, joystick, buttons) can be polled separately and combined with this in the game loop.

Examples

const input = new GamepadInput({
  up:    { keys: ["ArrowUp", "KeyW"] },
  down:  { keys: ["ArrowDown", "KeyS"] },
  left:  { keys: ["ArrowLeft", "KeyA"] },
  right: { keys: ["ArrowRight", "KeyD"] },
  jump:  { keys: ["Space"] },
  shoot: { keys: ["KeyZ", "KeyJ"] },
});
 
function gameLoop() {
  input.update();
  if (input.isDown("left")) player.moveLeft();
  if (input.wasPressed("jump")) player.jump();
  requestAnimationFrame(gameLoop);
}

Members

update

update(): void

Call once per frame before reading input

isDown

isDown(action: string): boolean

True while any mapped key for this action is held

wasPressed

wasPressed(action: string): boolean

True only on the first frame any mapped key for this action is pressed

wasReleased

wasReleased(action: string): boolean

True only on the frame any mapped key for this action is released

getAxis

getAxis(negativeAction: string, positiveAction: string): number

Get a horizontal axis value: -1 (left), 0, or 1 (right)

destroy

destroy(): void

Removes keyboard listeners.