browser-keyboard

Tracks which keys are currently held down, from the document's keydown / keyup events. Exposes the raw pressed keys plus selectors for codes, modifiers, and per-key checks.

Live example

Loading demo…

Press and hold keys — the on-screen keyboard reflects pressedCodesSelector live.

Install

bun add @valdres/browser-keyboard

Usage

import { useValue } from "valdres-react"
import { pressedCodesSelector, isCodePressedSelector } from "@valdres/browser-keyboard"

function Keys() {
    const codes = useValue(pressedCodesSelector) // KeyboardCode[]
    const shift = useValue(isCodePressedSelector("ShiftLeft"))
    return <span>{codes.join(" + ")}</span>
}

Exports

ExportKindType
pressedKeysAtomatom (read-only)PressedKey[]
pressedCodesSelectorselectorKeyboardCode[]
pressedKeyValuesSelectorselectorstring[]
modifierSelectorselectorModifier[]
isCodePressedSelector(code)selector familyboolean
isKeyPressedSelector(key)selector familyboolean
toggleKeyAtom(key)atom family (settable)boolean

Cross-framework

Global atoms and selectors — works in every framework. For higher-level "press this combination → run this callback" hotkeys (rather than raw key state), use @valdres/hotkeys.