
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
keyboard-code-enum
Advanced tools
A TypeScript enum for handling keyboard event codes (in favor of keyCode or key).
Code Enum Library is a TypeScript library that provides an exhaustive enum for keyboard event codes (KeyboardEvent.code
). It allows for easy and consistent handling of keyboard inputs based on physical key positions across different layouts, alongside helpful utility functions to manage keyboard events.
This library was inspired by the following projects, which offer similar functionality for key codes and keyboard event handling:
KeyboardEvent.keyCode
values.KeyboardEvent.key
, allowing developers to handle characters associated with key events in a standardized way.Install the package via npm:
npm install keyboard-code-enum
Import the Code
enum and/or the CodeUtils
utilities as needed:
import { Code, CodeUtils } from 'keyboard-code-enum';
document.addEventListener('keydown', (event) => {
const code = event.code as Code;
if (CodeUtils.isLetterKey(code)) {
console.log('A letter key was pressed');
} else if (CodeUtils.isFunctionKey(code)) {
console.log('A function key was pressed');
} else if (CodeUtils.isModifierKey(code)) {
console.log('A modifier key was pressed');
} else if (CodeUtils.isMediaKey(code)) {
console.log('A media key was pressed');
} else {
console.log('Other key:', code);
}
});
Code
EnumThe Code
enum contains all standard keyboard event code
values (e.g., KeyA
, ArrowUp
, Enter
, F1
, etc.), based on the MDN documentation.
import { Code } from 'keyboard-code-enum';
console.log(Code.KeyA); // Outputs: "KeyA"
CodeUtils
NamespaceCodeUtils
provides various helper functions to categorize and validate KeyboardEvent.code
values.
CodeUtils.isLetterKey(code: Code): boolean
Checks if the code
is a letter key (A-Z).
CodeUtils.isLetterKey(Code.KeyA); // true
CodeUtils.isLetterKey(Code.Digit1); // false
CodeUtils.isDigitKey(code: Code): boolean
Checks if the code
is a numeric key (0-9 on main keyboard or numpad).
CodeUtils.isDigitKey(Code.Digit1); // true
CodeUtils.isDigitKey(Code.Numpad0); // true
CodeUtils.isFunctionKey(code: Code): boolean
Checks if the code
is a function key (F1-F24).
CodeUtils.isFunctionKey(Code.F1); // true
CodeUtils.isNavigationKey(code: Code): boolean
Checks if the code
is a navigation key (arrows, home, end, page up/down).
CodeUtils.isNavigationKey(Code.ArrowLeft); // true
CodeUtils.isModifierKey(code: Code): boolean
Checks if the code
is a modifier key (Shift, Ctrl, Alt, Meta).
CodeUtils.isModifierKey(Code.ControlLeft); // true
CodeUtils.isModifierKey(Code.AltRight); // true
CodeUtils.isMediaKey(code: Code): boolean
Checks if the code
is a media control key (volume, play, pause, etc.).
CodeUtils.isMediaKey(Code.MediaPlayPause); // true
CodeUtils.isMediaKey(Code.AudioVolumeMute); // true
CodeUtils.isValidCode(value: string): boolean
Validates if a given string is a valid Code
value.
CodeUtils.isValidCode('KeyA'); // true
CodeUtils.isValidCode('InvalidKey'); // false
Contributions are welcome! If you’d like to improve this library, please fork the repository, make your changes, and submit a pull request.
This project is licensed under the MIT License.
FAQs
A TypeScript enum for handling keyboard event codes (in favor of keyCode or key).
We found that keyboard-code-enum demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.