blob2d
Version:
Typed Modular 2D Game Engine for Web
40 lines (26 loc) • 843 B
Markdown
General utilities for user interactions.
- [Keyboard](
- [ScreenButton](
Proxy of keyboard events handling both `keyup` and `keydown` state.
```ts
const keyboard = new Keyboard<TKey>();
keyboard.on('ArrowRight', (pressed: boolean) => {
if (pressed) player.moveRight();
});
keyboard.off('ArrowRight');
keyboard.destroy(); // for a cleanup
```
Simulates clicking a physical keyboard.
```ts
const $node = document.querySelector<HTMLElement>('.button');
const button = new ScreenButton<TKey>('ArrowLeft', $node);
// extends button behavior [optional]
button.onKeydown = node => node.classList.add('clicked');
button.onKeyup = node => node.classList.remove('clicked');
// listens to the standard key event
keyboard.on('ArrowLeft', callback);
```