KeyboardEvent Key Normalizer
`keyboard-key` is a lightweight utility library designed to provide a consistent and reliable way to determine the `KeyboardEvent.key` property from keyboard events across different browsers, addressing inconsistencies in native browser implementations. Released as version 1.1.0, this package aims to normalize keyboard event key values, stepping in where `KeyboardEvent.key` lacked full cross-browser support or where older, deprecated properties like `keyCode` and `which` were still in use. It differentiates itself by attempting to infer key values, including interpreting shift key presses, which, while helpful for `en-US` layouts, introduces a locale-specific caveat. The library also offers `getCode()` for obtaining normalized `keyCode` values and provides constants for common key codes, promoting the use of modern `KeyboardEvent.key` semantics while providing a fallback.
Common errors
-
TypeScript error: Cannot find name 'keyboardKey'.
cause Attempting to use `keyboardKey` as a default import or global variable when `keyboard-key` primarily exports named functions.fixUse named imports for TypeScript: `import { getKey, getCode, Escape } from 'keyboard-key';` -
ReferenceError: keyboardKey is not defined
cause Attempting to use `keyboardKey` as a global variable or accessing its members without proper ES module named imports or CommonJS destructuring.fixFor ESM, use named imports: `import { getKey } from 'keyboard-key';`. For CommonJS, use destructuring: `const { getKey } = require('keyboard-key');`
Warnings
- gotcha The `getKey()` function's logic for inferring `key` values, especially with `shift` key presses (e.g., `shift` + `/` resulting in `?`), assumes an `en-US` keyboard layout. Using different locales (e.g., German, French) will lead to incorrect `key` values for certain shifted characters or special keys.
- gotcha While `keyboard-key` provides a normalized `key` property, the underlying browser support for the native `KeyboardEvent.key` has historically been inconsistent across different browsers and versions. This library aims to mitigate these inconsistencies, but developers should remain aware that edge cases might exist in very old or niche browser environments.
- deprecated Direct reliance on deprecated `KeyboardEvent` properties such as `keyCode`, `charCode`, `which`, or `keyIdentifier` is highly discouraged in modern web development. While `keyboard-key` provides `getCode()` for numeric values, its primary purpose is to normalize and promote the use of the `KeyboardEvent.key` property.
Install
-
npm install keyboard-key -
yarn add keyboard-key -
pnpm add keyboard-key
Imports
- getKey
import keyboardKey from 'keyboard-key'; keyboardKey.getKey(event);
import { getKey } from 'keyboard-key' - getCode
const code = keyboardKey.getCode(event);
import { getCode } from 'keyboard-key' - Escape
import { escape } from 'keyboard-key'import { Escape } from 'keyboard-key'
Quickstart
import { getKey, getCode, Escape } from 'keyboard-key';
document.addEventListener('keydown', (event: KeyboardEvent) => {
const key: string = getKey(event);
const code: number = getCode(event);
console.log(`Key pressed: ${key}, Code: ${code}`);
switch (key) {
case 'Escape':
console.log('Escape key detected via getKey!');
break;
case 'Enter':
console.log('Enter key detected via getKey!');
break;
default:
break;
}
// Using getCode with named constant
if (code === Escape) {
console.log('Escape key detected via getCode and constant!');
}
});
// Simulate keydown events for demonstration purposes in a browser-like environment.
// In a real application, these would come from user interaction.
setTimeout(() => {
const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape', keyCode: 27, which: 27 });
document.dispatchEvent(escapeEvent);
}, 100);
setTimeout(() => {
const enterEvent = new KeyboardEvent('keydown', { key: 'Enter', keyCode: 13, which: 13 });
document.dispatchEvent(enterEvent);
}, 200);