The Input class handles action mapping and user input event listening.
Getting started
The following code demonstrates using arrows and WASD keys for entity movement:
import { Input } from "com.hydroper.webinputaction"; Input.input.setActions({ "moveLeft": [ { key: "a" }, { key: "leftArrow" }, ], "moveRight": [ { key: "d" }, { key: "rightArrow" }, ], "moveUp": [ { key: "w" }, { key: "upArrow" }, ], "moveDown": [ { key: "s" }, { key: "downArrow" }, ], }); Input.input.addEventListener("inputPressed", () => { const shouldMoveRight = Input.input.isPressed("moveRight"); });
Built-in actions
The following actions are pre-defined in every action map and can be overriden:
escape- Used for escaping out in the user interface.navigateLeft— Used for focusing the left neighbor of an user interface control.navigateRight— Used for focusing the right neighbor of an user interface control.navigateUp— Used for focusing the top neighbor of an user interface control.navigateDown— Used for focusing the bottom neighbor of an user interface control.
Events
This class extends EventTarget and may dispatch the following events:
// Dispatched when user input starts being pressed or // is continuously pressed. inputPressed: Event; // Dispatched when any user input is released. inputReleased: Event; // Dispatched when the actions map is updated. actionsUpdated: Event;
private
mMap: Record<string, InputActionAtom[]>
getActions(): Record<string, InputActionAtom[]>
Returns the current action map in read-only mode.
justPressed(name: string): boolean
Determines whether an action has been just pressed right now.
setActions(map: Record<string, InputActionAtom[]>): void
Updates the action map.
private
readonly
mPressedStatePoolKeys: Map<InputActionKeyName, PressedState>
private
builtin(): Record<string, InputActionAtom[]>