You're reading the 2.x docs. quantum-forge 3.0 is out: this page in 3.x / migration guide

Skip to content

Input API ​

typescript
import {
  InputManager,
  LocalMultiplayerManager,
  GamepadButtons,
  GamepadAxes,
} from "quantum-forge-engine/input";

The subpath also exports the devices behind the manager, for games that want them directly: KeyboardDevice, MouseDevice, TouchDevice, GamepadDevice, VirtualJoystick, GestureRecognizer.

InputManager ​

Constructor ​

typescript
new InputManager(config?: {
  bindings?: Binding[];        // initial bindings
  deadZone?: number;           // gamepad analog dead zone, 0-1 (default: 0.15)
  preventDefaults?: boolean;   // call preventDefault on browser input events (default: false)
  gamepadIndex?: number;       // which gamepad to read (default: 0, the first connected)
  logger?: LoggerInterface;
})

LocalMultiplayerManager passes -1 as a sentinel meaning "no gamepad yet" and assigns real indices as pads connect. Games constructing an InputManager themselves should leave gamepadIndex alone or set a real index.

Binding ​

MethodDescription
bind(action, ...sources)Map one or more sources to an action
bindKey(code, action)Shorthand for key binding
bindButton(index, action)Shorthand for gamepad button
bindAxis(index, direction, action)Shorthand for gamepad axis
bindGesture(gesture, action)Shorthand for gesture binding
bindJoystick(name, axis, direction, action)Shorthand for virtual joystick

Querying ​

MethodReturnsDescription
poll()voidMust call every frame before queries
isActionDown(action)booleanHeld down (continuous)
isActionJustPressed(action)booleanTrue only on press frame
isActionJustReleased(action)booleanTrue only on release frame
getActionValue(action)number0–1 analog value
getActiveDevice()string"keyboard", "mouse", "touch", "gamepad"
getPointerPosition(){x, y}Client coordinates
getCanvasPointerPosition(canvas){x, y}Canvas-relative coordinates

Events ​

typescript
const unsub = input.on(action, handler);  // fires on press edge
unsub();  // unsubscribe

Touch ​

MethodDescription
addTouchZone(zone)Register a named zone { name, x, y, w, h } (0–1 normalized)
addJoystick(config)Register virtual joystick { name, zone, radius?, deadZone?, dynamic? }
getJoystickState(name)Returns { active, x, y, magnitude } or null
getGestures()Returns array of recognized gestures this frame
getPinchState()Returns { active, scale, rotation }
configureGestures(options)Set thresholds for gesture recognition

Cleanup ​

typescript
input.destroy();  // removes all event listeners

LocalMultiplayerManager ​

Constructor ​

typescript
new LocalMultiplayerManager(config: {
  players: number;                            // number of player slots
  playerConfigs?: Partial<InputManagerConfig>[];  // per-slot overrides, by player index
  autoAssign?: boolean;                       // auto-assign gamepads as they connect (default: true)
  deadZone?: number;                          // dead zone for every slot (default: 0.15)
  logger?: LoggerInterface;
})

Methods ​

MethodDescription
getPlayer(index)Returns InputManager for player (or null)
getPlayers()Returns all { index, input } slots
bindAll(action, ...sources)Bind to all players' InputManagers
pollAll()Call poll() on all players
assignGamepad(playerIndex, gamepadIndex)Manual gamepad assignment
unassignGamepad(playerIndex)Remove gamepad from player
destroy()Cleanup all InputManagers

Events ​

typescript
mp.on("gamepad-assigned", ({ playerIndex, gamepadId }) => { /* ... */ });
mp.on("gamepad-disconnected", ({ playerIndex }) => { /* ... */ });

Event types: "gamepad-connected", "gamepad-disconnected", "gamepad-assigned", "gamepad-unassigned". In the current implementation a connection emits only "gamepad-assigned"; "gamepad-connected" is declared in the type union but not yet emitted. Every handler receives { type, playerIndex, gamepadIndex, gamepadId }. on() returns an unsubscribe function.

GamepadButtons ​

A, B, X, Y, LB, RB, LT, RT, Back, Start,
LeftStick, RightStick, DpadUp, DpadDown, DpadLeft, DpadRight

GamepadAxes ​

LeftStickX, LeftStickY, RightStickX, RightStickY

Powered by Quantum Forge