npm.io
0.1.2 • Published 5 years ago

@arcade2d/controls

Licence
Version
0.1.2
Deps
1
Size
14 kB
Vulns
0
Weekly
0

Controls

Input detection components for browser-based game controls.

Installation

$ yarn add @arcade2d/controls

Examples

Keyboard Input
import {
  KeyboardInput,
  KeyboardInputEvent,
  KeyboardState,
} from '@arcade2d/controls';

// Define keyboard input manager instance.
const keyboard = new KeyboardInput(document);

// Handle key press and release handlers.
keyboard.onKeyPress((event: KeyboardInputEvent) =>
  console.log('press', event.key),
);
keyboard.onKeyRelease((event: KeyboardInputEvent) =>
  console.log('release', event.key),
);

// Check keyboard state during a loop.
setInterval(() => {
  const state: KeyboardState = keyboard.getState();

  if (state.isDown('a')) {
    console.log('a is held down');
  }
}, 16);