Skip to content

Input API

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

InputManager

Constructor

typescript
new InputManager(options?: {
  logger?: LoggerInterface;
  gamepadIndex?: number;  // -1 = no gamepad (default for multiplayer slots)
})

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(options: {
  players: number;
  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 }) => { /* ... */ });

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