Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
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).
The npm package keyboard-code-enum receives a total of 1 weekly downloads. As such, keyboard-code-enum popularity was classified as not popular.
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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.