keytrack
v0.1.2
Published
Track which keyboard keys are currently pressed in the browser, plus a DOM KeyboardEvent.code to legacy keyCode mapping.
Downloads
606
Maintainers
Readme
keytrack
Track which keyboard keys are currently pressed in the browser, plus a
KeyboardEvent.code → legacy numeric keyCode mapping.
Install
npm install keytrackUsage
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>— mapsKeyboardEvent.codevalues (e.g."KeyA","ArrowUp") to their legacy numerickeyCodeequivalents.logKeys(log = true)— start (true) or stop (false) tracking pressed keys viawindowkeydown/keyuplisteners. Stopping clears the tracked state.isKeyPressed(key: string): boolean— whether a key is currently pressed, matched againstKeyboardEvent.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 toisKeyPressed/isKeyCodePressedbased ondata.type.
License
MIT
