import { WR_KEYS, type WrKey } from 'ngwr/utils';
if (event.key === WR_KEYS.ESCAPE) {
close();
}
function onArrow(key: WrKey) { /* … */ }WR_KEYS
KeyboardEvent.key constants, the WrKey union of their values, and WR_KEY_LABELS, what each key is called on a keycap.
Usage
Labels
WR_KEY_LABELS is keyed by WR_KEYS: Esc, Enter, Ctrl, an arrow as its glyph. English, as printed on a PC keycap. wrFormatChord in ngwr/hotkey prints a whole chord from it and swaps in Apple's glyphs on macOS; <wr-kbd> takes the words from the i18n catalog.
import { WR_KEYS, WR_KEY_LABELS } from 'ngwr/utils';
WR_KEY_LABELS[WR_KEYS.ESCAPE]; // 'Esc'
WR_KEY_LABELS[WR_KEYS.ARROW_UP]; // '↑'
WR_KEY_LABELS[event.key as WrKey] ?? event.key;API
| Name | Description | Type | Default |
|---|---|---|---|
WR_KEYS | KeyboardEvent.key values: ENTER, ESCAPE, ARROW_UP, ARROW_DOWN, TAB, SPACE, … and the modifiers CONTROL, SHIFT, ALT, META. | const record | — |
WrKey | Union of every value in WR_KEYS, for typed key parameters. | type alias | — |
WR_KEY_LABELS | What each key is called on a PC keycap, keyed by WR_KEYS: Esc, Ctrl, Page Up, ↑. | Readonly<Record<WrKey, string>> | - |
See also
Guide KeyboardBind shortcuts, show them as keycaps, and see which keys each component handles.Service WrHotkeyFor whole chords, and `wrFormatChord`.Util wrHasModifierTrue when Ctrl, Cmd, Alt, Shift or Meta is held during a KeyboardEvent.Util wrIsPrintableKeyTrue for a single printable character typed without Ctrl, Cmd or Alt (Shift is allowed).Util wrIsComposingTrue while an input method (IME) is composing, so the key belongs to it and not to your handler.