`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.
npm install keyboard-keyVerified import paths — ran on the pinned version, not inferred.
This code snippet demonstrates how to import and use `getKey()` and `getCode()` to normalize keyboard events, and how to use the provided key code constants for robust keyboard input handling.
For internationalized applications, prefer `getCode()` where consistent numeric codes are sufficient, or implement custom locale mapping logic for `key` values if string representation is critical. Thoroughly test on target locales.
Continue using `keyboard-key` for improved cross-browser consistency; however, ensure thorough testing across all target browser versions, especially if supporting older or less common browsers.
Prioritize `getKey()` for string representations of keys. If numeric identification is absolutely necessary for legacy compatibility or specific scenarios, use `getCode()` rather than accessing deprecated event properties directly.
Use named imports for TypeScript: `import { getKey, getCode, Escape } from 'keyboard-key';`For ESM, use named imports: `import { getKey } from 'keyboard-key';`. For CommonJS, use destructuring: `const { getKey } = require('keyboard-key');`No dependency data recorded yet.