Named constants for legacy KeyboardEvent.keyCode values. Import ENTER instead of comparing against 13 — six months later, you still know what the handler checks for.
- A codebase or library still branches on
event.keyCode/event.which. - You want shared, grep-friendly names across keyboard handlers (
ESCAPE,ARROW_DOWN, …). - You debug keyboard behaviour and want readable comparisons in DevTools or tests.
- New code should prefer
event.keyandevent.code. They are layout-aware and not deprecated. - You need every key on the keyboard. This package only exports the navigation and editing keys used most often in UI widgets.
pnpm add @19h47/keycodeAlso works with npm / yarn / bun.
import { ENTER, ESCAPE, ARROW_DOWN, ARROW_UP } from '@19h47/keycode';
document.addEventListener('keydown', (event) => {
if (event.keyCode === ESCAPE) {
closeDialog();
}
if (event.keyCode === ENTER) {
submitForm();
}
if (event.keyCode === ARROW_DOWN || event.keyCode === ARROW_UP) {
moveFocus(event.keyCode === ARROW_DOWN ? 1 : -1);
}
});Named imports only — no default export, no runtime logic.
<script src="node_modules/@19h47/keycode/dist/keycode.umd.cjs"></script>
<script>
document.addEventListener('keydown', (event) => {
if (event.keyCode === keycode.ESCAPE) {
// …
}
});
</script>| Constant | keyCode |
Typical event.key |
Typical event.code |
|---|---|---|---|
BACKSPACE |
8 | Backspace |
Backspace |
TAB |
9 | Tab |
Tab |
ENTER |
13 | Enter |
Enter |
SHIFT |
16 | Shift |
ShiftLeft |
ESCAPE |
27 | Escape |
Escape |
SPACE |
32 | |
Space |
PAGE_UP |
33 | PageUp |
PageUp |
PAGE_DOWN |
34 | PageDown |
PageDown |
END |
35 | End |
End |
HOME |
36 | Home |
Home |
ARROW_LEFT |
37 | ArrowLeft |
ArrowLeft |
ARROW_UP |
38 | ArrowUp |
ArrowUp |
ARROW_RIGHT |
39 | ArrowRight |
ArrowRight |
ARROW_DOWN |
40 | ArrowDown |
ArrowDown |
DELETE |
46 | Delete |
Delete |
pnpm install
pnpm dev # Vite playground
pnpm build # library build → dist/ + docs/Live example: 19h47.github.io/19h47-keycode · source
Press any key to inspect event.key, event.code, event.keyCode, and the matching @19h47/keycode constant.