Utility·phaserthree·v1.0.0·MIT

Input Manager

Engine-agnostic input action mapper. Map logical actions ('jump', 'left') to keyboard bindings and query state without coupling gameplay code to the DOM keyboard API.

#input#keyboard#actions

Install

npx gamecn add input-manager

Source preview

Compatibility

Engines
phaser >=3.80.0 <4.0.0three >=0.160.0
Frameworksvanilla, react
Languagestypescript
Bundlersvite

Files installed

  • {src}/utilities/InputManager.ts
  • docs/gamecn/input-manager.md(docs)

Notes

Usage

import { InputManager } from "@/utilities/InputManager";

const input = new InputManager({
  bindings: {
    jump: "Space",
    left: ["ArrowLeft", "KeyA"],
    right: ["ArrowRight", "KeyD"],
    up: ["ArrowUp", "KeyW"],
    down: ["ArrowDown", "KeyS"],
  },
});

// In your update loop:
function update(): void {
  if (input.wasPressed("jump")) player.jump();
  const move = input.axis("left", "right", "up", "down");
  player.move(move.x, move.y);
  input.endFrame(); // clears edge events
}

// On scene/component teardown:
input.dispose();

API

Method Description
isDown(action) True while held.
wasPressed(action) True on the frame of the press (cleared by endFrame).
wasReleased(action) True on the frame of the release.
axis(left, right, up, down) 2D {x, y} in {-1, 0, 1}.
endFrame() Call once per tick.
dispose() Remove DOM listeners.

Notes

  • Uses KeyboardEvent.code (layout-independent), not key.
  • preventDefault is true by default — pass { preventDefault: false } to disable.
  • Cross-engine: works inside Phaser scenes, R3F components, vanilla Three, or DOM-only games.