Skip to content

controls

Input controls (Screen, Mouse, Touchpad, Keyboard, Gamepad)

๐Ÿ’ƒ Getting Started โ€‹

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

bash
$ npm i @jolly-pixel/controls
# or
$ yarn add @jolly-pixel/controls

๐Ÿ‘€ Usage example โ€‹

ts
import { Input } from "@jolly-pixel/controls";

const canvas = document.querySelector("canvas");
if (!canvas) {
  throw new Error("No canvas element found");
}
const input = new Input(canvas);

input.connect();

function gameLoop() {
  input.update();

  if (input.keyboard.wasJustPressed("Space")) {
    console.log("Jump!");
  }
  if (input.mouse.isDown("left")) {
    const delta = input.mouse.viewportDelta(true);
    console.log("Dragging", delta.x, delta.y);
  }

  requestAnimationFrame(gameLoop);
}

gameLoop();

For advanced input combinations:

ts
import { InputCombination } from "@jolly-pixel/controls";

const dashCombo = InputCombination.all(
  InputCombination.key("ShiftLeft"),
  InputCombination.key("ArrowRight")
);
if (dashCombo.evaluate(input)) {
  console.log("dash!");
}

๐Ÿ“š API โ€‹

๐Ÿงช Benchmarks โ€‹

The suites cover device updates, state queries, input combinations, and DOM event dispatch. They use headless adapters, so event benchmarks report getBoundingClientRect() call counts instead of browser layout timings.

bash
npm run bench -w @jolly-pixel/controls

Use -- --list to inspect the suites. Filtering and measurement rules are documented by @jolly-pixel/bench.

โœจ Contributors guide โ€‹

If you are a developer looking to contribute to the project, you must first read the CONTRIBUTING guide.

Once you have finished your development, check that the tests (and linter) are still good by running the following script:

bash
$ npm run test
$ npm run lint

CAUTION

In case you introduce a new feature or fix a bug, make sure to include tests for it as well.

๐Ÿ“ƒ License โ€‹

MIT