Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/basementuniverse/input-manager
A component for handling input events and managing input device state
https://github.com/basementuniverse/input-manager
Last synced: 23 days ago
JSON representation
A component for handling input events and managing input device state
- Host: GitHub
- URL: https://github.com/basementuniverse/input-manager
- Owner: basementuniverse
- License: mit
- Created: 2022-12-28T17:16:01.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-11-04T23:39:46.000Z (2 months ago)
- Last Synced: 2024-12-02T14:05:02.686Z (about 1 month ago)
- Language: TypeScript
- Size: 255 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Game Component: Input Manager
A component for handling input events and managing input device state.
## Installation
```bash
npm install @basementuniverse/input-manager
```## How to use
Initialise the input manager before use:
```ts
import InputManager from '@basementuniverse/input-manager';InputManager.initialise();
```Update the input manager state:
```ts
class Game {
// ...public update(context: CanvasRenderingContext2D) {
InputManager.update();
}
}
```Check the state of input devices:
```ts
// Check if any key is currently down
InputManager.keyDown();// Check if a specific key is currently down
InputManager.keyDown(code?);// Check if a key was pressed
InputManager.keyPressed(code?);// Check if a key was released
InputManager.keyReleased(code?);// Check if the main mouse button is currently down
InputManager.mouseDown(button?);// Check if the main mouse button was pressed
InputManager.mousePressed(button?);// Check if the main mouse button was released
InputManager.mouseReleased(button?);// Check if the mouse wheel was scrolled up
InputManager.mouseWheelUp();// Check if the mouse wheel was scrolled down
InputManager.mouseWheelDown();// Get the current mouse position in screen-space
const position = InputManager.mousePosition;
```See [here](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code) for a reference of key codes.
## Options
```ts
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 |