Game Component: Input Manager
A component for handling input events and managing input device state.
Installation
npm install @basementuniverse/input-manager
How to use
Initialise the input manager before use:
import InputManager from '@basementuniverse/input-manager';
InputManager.initialise();
Update the input manager state:
class Game {
public update(context: CanvasRenderingContext2D) {
InputManager.update();
}
}
Check the state of input devices:
InputManager.keyDown();
InputManager.keyDown(code?);
InputManager.keyPressed(code?);
InputManager.keyReleased(code?);
InputManager.mouseDown(button?);
InputManager.mousePressed(button?);
InputManager.mouseReleased(button?);
InputManager.mouseWheelUp();
InputManager.mouseWheelDown();
const position = InputManager.mousePosition;
See here for a reference of key codes.
Options
const options = { ... };
InputManager.initialise(options);
Option | Type | Default | Description |
---|
element | Window | HTMLElement | window | The element to listen for mouse input events on |
mouse | boolean | true | Enable mouse input |
mouseWheel | boolean | true | Enable mouse wheel input |
keyboard | boolean | true | Enable keyboard input |
preventContextMenu | boolean | false | Try to prevent the context menu from appearing on right-click |