Keyboard Event Key Name Utility
keyname is a JavaScript utility designed to translate numeric keyboard event key codes into more human-readable string names. For instance, it can convert a key code of `27` to "esc" or `65` to "a". Released as version `0.1.0` and last updated approximately 11 years ago, this package originates from the 'component' ecosystem, which was a frontend package manager system that predated or competed with the widespread adoption of npm for browser-side modules. Consequently, the package is considered abandoned, with no active development, bug fixes, or new features. While it serves its original function of mapping legacy `KeyboardEvent.keyCode` values, its relevance has significantly diminished due to the broad browser support for the native `KeyboardEvent.key` and `KeyboardEvent.code` properties, which directly provide string representations of pressed keys, handling variations in keyboard layouts and modifier states. This means developers typically no longer need a utility like `keyname` for new projects or modern browser targets. It reports having zero runtime dependencies and a minimal number of downstream dependents (8 projects), reflecting its specialized and now largely superseded role.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('keyname')` in an ES Module context (e.g., a modern browser environment or Node.js module with `"type": "module"` in package.json) where `require` is not available.fixThis package is CJS-only. If you must use it, ensure your environment supports CommonJS modules. However, the recommended fix is to migrate away from `keyname` and use `KeyboardEvent.key` directly on the event object. -
event.keyCode is deprecated
cause While `keyname` processes `keyCode` values, the `keyCode` property itself on `KeyboardEvent` objects is deprecated by modern web standards and browsers.fixAccess `event.key` directly on the `KeyboardEvent` object instead of using `event.keyCode`. This provides a more robust and localized string representation of the pressed key.
Warnings
- deprecated The package `keyname` is abandoned and has not been updated in over a decade. Its functionality, which relies on `KeyboardEvent.keyCode`, is superseded by the widely supported `KeyboardEvent.key` and `KeyboardEvent.code` properties available natively in modern browsers.
- gotcha This package is CommonJS-only (uses `require()`) and does not provide ES Module (`import`) exports. Attempting to import it using `import` syntax in an ESM context will lead to runtime errors.
- gotcha The package is unmaintained, meaning it will not receive security updates, bug fixes, or new features. Relying on unmaintained code can introduce security vulnerabilities or compatibility issues with newer JavaScript runtimes or browser environments.
Install
-
npm install keyname -
yarn add keyname -
pnpm add keyname
Imports
- keyname
import keyname from 'keyname';
const keyname = require('keyname');
Quickstart
const keyname = require('keyname');
// Get name for Escape key code
console.log(keyname(27));
// => "esc"
// Get name for 'A' key code
console.log(keyname(65));
// => "a"
// Get name for '1' key code
console.log(keyname(49));
// => "1"
// Unknown key codes return undefined
console.log(keyname(23123123));
// => undefined
// In modern browsers, consider using KeyboardEvent.key directly:
// document.addEventListener('keydown', (event) => {
// console.log('Pressed key:', event.key);
// });