Skip to content

Repository files navigation

keytrack

Track which keyboard keys are currently pressed in the browser, plus a KeyboardEvent.code → legacy numeric keyCode mapping.

Install

npm install keytrack

Usage

import { logKeys, isKeyPressed, isKeyCodePressed } from "keytrack";

// start tracking keydown/keyup on window
logKeys(true);

isKeyPressed("a"); // true while the "a" key is held down
isKeyCodePressed(65); // same, using the legacy keyCode for "a"

// stop tracking and clear state
logKeys(false);

API

  • codeToKeyCodeMapping: Record<string, number> — maps KeyboardEvent.code values (e.g. "KeyA", "ArrowUp") to their legacy numeric keyCode equivalents.
  • logKeys(log = true) — start (true) or stop (false) tracking pressed keys via window keydown/keyup listeners. Stopping clears the tracked state.
  • isKeyPressed(key: string): boolean — whether a key is currently pressed, matched against KeyboardEvent.key.
  • isKeyCodePressed(keyCode: number): boolean — whether a key is currently pressed, matched against the legacy numeric keyCode.
  • handleKeyboardQuery(data: { type: "key" | "keyCode"; key: unknown }): boolean | undefined — dispatches to isKeyPressed/isKeyCodePressed based on data.type.

License

MIT

About

a library to synchronously query for keys instead of registering listeners

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages