The Input class is a high-level wrapper around a low-level input system. It manages keyboard state tracking, including key presses and held states, while optionally filtering out “blocked” keys (e.g., arrow keys, space, and specific action keys). It provides a simplified API for querying input state in game loops or interactive applications.
Creation → Update → Destruction
InputState is created via createInputState(blockedKeys).attachInput.InputState is updated implicitly via event listeners.isDown and isPressed.constructor(blockedKeys?: string[])Creates a new input handler instance.
blockedKeys (optional): Array of key identifiers to ignore in input processing.
["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", " ", "e"]isDown(key: string): booleanChecks whether a key is currently being held down.
true if the key is currently pressedfalse otherwiseisPressed(key: string): booleanChecks whether a key was pressed during the current update cycle.
true if the key transitioned from up → down in the current framefalse otherwiseclearPressed(): voidResets all “pressed” states.
getState(): InputStateReturns the underlying input state object.
InputState instance used for tracking input../input) that provides:
InputState typecreateInputState(blockedKeys) for initializationattachInput(state) for registering event listenersisKeyDown(state, key) for continuous input checksisKeyPressed(state, key) for edge-triggered input detectionclearPressed(state) for resetting per-frame press flagsblockedKeys are passed at initialization and used by the underlying system to filter unwanted or reserved inputs.
The class itself does not directly listen to events; it delegates all input tracking to the shared InputState.
import { Input } from "./Input";
const input = new Input();
// Game loop
function update() {
if (input.isDown("ArrowLeft")) {
console.log("Moving left continuously");
}
if (input.isPressed(" ")) {
console.log("Jump triggered");
}
// Reset per-frame press states
input.clearPressed();
}
InputState – Internal structure storing key statescreateInputState – Initializes input tracking stateattachInput – Hooks keyboard event listenersisKeyDown – Checks continuous key stateisKeyPressed – Checks edge-triggered key stateclearPressed – Resets per-frame key press flags