@amatiasq/keyboard
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

KeyboardController

Simple class to track keyboard pressed keys.

Installation

Install with npm i --save @amatiasq/keyboard.

KeyboardController

Manages keyboard events

import { KeyboardController, KeyCode } from '@amatiasq/keyboard';

const keyboard = new KeyboardController();

keyboard.onKeyCodeDown(KeyCode.Enter, event => console.log('Enter key down'));
keyboard.onKeyCodeUp(KeyCode.Enter, event => console.log('Enter key up'));

keyboard.onKeyDown(event => console.log(`Key pressed ${event.code}`));
keyboard.onKeyUp(event => console.log(`Key pressed ${event.code}`));

keyboard.isPressed(KeyCode.Space); // false

KeyboardActions

Manages keyboard bindings

import { KeyboardActions, KeyCode } from '@amatiasq/keyboard';

enum MyActions {
  RUN,
  JUMP,
  SHOOT,
}

const keyboard = new KeyboardActions<MyActions>();

keyboard.setKeyMap(KeyCode.ArrowRight, Actions.RUN);
keyboard.setKeyMap(KeyCode.Space, Actions.JUMP);
keyboard.setKeyMap(KeyCode.ArrowUp, Actions.JUMP);

keyboard.onActivate(Actions.JUMP, () => console.log('Start jump'));
keyboard.onDectivate(Actions.JUMP, () => console.log('End jump'));
keyboard.onChange(actions => console.log('Active actions', actions));

keyboard.isActive(Actions.JUMP); // will be true while spacebar or arrow up is pressed

Readme

Keywords

none

Package Sidebar

Install

npm i @amatiasq/keyboard

Weekly Downloads

4

Version

1.0.1

License

MIT

Unpacked Size

227 kB

Total Files

48

Last publish

Collaborators

  • amatiasq