keyboard
Keyboard event helpers โ key and modifier checks, printable-key detection, multi-key shortcuts, and preventing default for a specific key.
Import: @rtorcato/browser-common/keyboard
๐ MDN: KeyboardEvent
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
KeyboardEvent.key | โ 51+ [1] | โ 12+ | โ 23+ | โ 10.1+ | โ 10.3+ | Widely available |
- Chrome: When selecting an autocomplete suggestion, a keydown event fires where the event's key property is undefined. See bug 41425904.
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: KeyboardEvent.key
Exampleโ
import { onShortcut, isKey } from '@rtorcato/browser-common/keyboard'
const off = onShortcut(['Control', 's'], (event) => {
event.preventDefault()
save()
})
input.addEventListener('keydown', (e) => {
if (isKey(e, 'Enter')) submit()
})
off()
Exportsโ
isKey(event, key)โ case-insensitive match againstevent.keyisModifierKey(event)โ true for Shift, Control, Alt, or MetaisPrintableKey(event)โ true for a single printable character (no Ctrl/Meta)onShortcut(keys, callback, target?)โ fires when allkeysare held together; returns an unsubscribe functionpreventKeyDefault(key, target?)โ callspreventDefault()wheneverkeyis pressed; returns an unsubscribe function
See the API reference for full signatures.