Input Forge - v0.3.1
    Preparing search index...

    Class InputManager

    Central manager for handling all input sources and routing inputs to commands.

    InputManager automatically listens to keyboard and gamepad inputs, maps them to commands defined in your input maps, and calls the appropriate command lifecycle methods (trigger, update, release).

    import { InputManager, Command, Inputs } from '@happy-pixels/input-forge';

    // 1. Create your commands
    class JumpCommand extends Command {
    trigger() { console.log('Jump!'); }
    }

    // 2. Define an input map
    const inputMap = {
    id: 'gameplay',
    singleInput: {
    jump: {
    keyboardInput: Inputs.KEYBOARD_SPACE,
    controllerInput: Inputs.CONTROLLER_FACE_BOTTOM,
    command: new JumpCommand()
    }
    }
    };

    // 3. Create the manager and set the input map
    const manager = new InputManager();
    manager.setInputMap(inputMap);

    // 4. Start the tick loop (required for update events and TickCommands)
    manager.startTick();

    // 5. Clean up when done
    manager.destroy();

    InputManager supports stacking input maps for context switching (e.g., gameplay vs menu):

    manager.setInputMap(gameplayMap);  // Base map
    manager.pushInputMap(pauseMenuMap); // Overlay - now active
    manager.popInputMap(); // Back to gameplay

    Trigger inputs programmatically (e.g., from touch controls):

    manager.triggerCustomInput('my_action');
    manager.triggerCustomAxesInput('virtual_stick', { x: 0.5, y: -0.5 });
    Index

    Constructors

    • Creates a new InputManager instance.

      Parameters

      • gamepadFps: number = 60

        Polling rate for gamepad inputs (default: 60, range: 1-120)

      • gamepadDeadZone: number = 0.1

        Dead zone threshold for gamepad sticks (default: 0.1, range: 0-1)

      • OptionalaxesThrottleMs: number

        Optional throttle for axis update events in milliseconds. When set, axis 'update' events will be throttled to reduce processing overhead. Trigger and release events are never throttled. (default: undefined/no throttle)

      Returns InputManager

    Properties

    activeInputDevice$: Observable<InputDeviceType | null> = ...

    Observable that emits when the active input device changes. Useful for switching button prompts between keyboard and controller styles.

    Emits:

    • 'keyboard' - When keyboard input is detected
    • 'xbox' - When an Xbox controller is used
    • 'playstation' - When a PlayStation controller is used
    • 'other_controller' - When another gamepad is used
    • null - Initial state before any input
    manager.activeInputDevice$.subscribe((device) => {
    if (device === 'keyboard') {
    showKeyboardPrompts();
    } else if (device === 'xbox') {
    showXboxPrompts();
    } else if (device === 'playstation') {
    showPlayStationPrompts();
    }
    });

    Methods

    • Gets the ID of the currently active input map.

      Returns string | null

      The active map's ID, or null if no map is set

    • Destroys the InputManager and releases all resources. Call this when you're done with the manager (e.g., when leaving a scene). After calling destroy, the manager should not be used again.

      Returns void

    • Gets all currently active commands (triggered but not yet released). Useful for debugging or displaying active input state.

      Returns Command<void>[]

      Array of currently active Command instances

      const active = manager.getActiveCommands();
      console.log(`Active commands: ${active.length}`);
    • Checks if an input map with the given ID exists anywhere in the stack.

      Parameters

      • id: string

        The input map ID to search for

      Returns boolean

      True if the map exists in the stack

    • Checks if a specific input key is currently active (pressed/held). Checks across all input sources (keyboard, gamepad).

      Parameters

      • key: string

        The input key to check (e.g., Inputs.KEYBOARD_SPACE, Inputs.CONTROLLER_FACE_BOTTOM)

      Returns boolean

      True if the input is currently active

      if (manager.isInputActive(Inputs.KEYBOARD_SHIFT)) {
      // Shift is being held
      }
    • Removes the top input map from the stack. The next map in the stack becomes active. Does nothing if the stack is empty.

      Returns void

    • Pushes an input map onto the stack, making it the active map. The previous map remains in the stack and will become active again when this map is popped. Use this for temporary contexts like pause menus or dialog boxes.

      Parameters

      • inputMap: InputMap

        The input map to push

      Returns void

    • Releases a custom axes input (sets axes to neutral/center).

      Parameters

      • name: string

        The custom axes input name to release

      Returns void

    • Removes a specific input map from anywhere in the stack by ID.

      Parameters

      • id: string

        The input map ID to remove

      Returns void

    • Replaces all input maps with a single new map. Use this to set the primary input map or switch between major game states.

      Parameters

      Returns void

    • Starts the tick loop for continuous updates. Required for:

      • update() events on held inputs
      • TickCommand.tick() calls

      Call stopTick() or destroy() to stop the loop.

      Returns void

    • Stops the tick loop. Commands will no longer receive update or tick events.

      Returns void

    • Triggers a custom axes input. Use this for virtual joysticks, touch controls, etc. The name must match a customAxesInput field in your input map.

      Parameters

      • name: string

        The custom axes input name

      • axes: AxesInput | [number, number]

        The axis values as {x, y} or [x, y]

      Returns void

    • Triggers a custom single input. Use this for virtual buttons, touch controls, etc. The input key must match a customInput field in your input map.

      Parameters

      • input: string

        The custom input key to trigger

      Returns void

    • Updates a custom axes input with new values.

      Parameters

      • name: string

        The custom axes input name

      • axes: AxesInput | [number, number]

        The new axis values as {x, y} or [x, y]

      Returns void