Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

keyboard-code-enum

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keyboard-code-enum

A TypeScript enum for handling keyboard event codes (in favor of keyCode or key).

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-97.06%
Maintainers
0
Weekly downloads
 
Created
Source

Code Enum Library

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.

  • Code values source: MDN KeyboardEvent Code Values
  • Data last updated: 2024-08-13

Inspired By

This library was inspired by the following projects, which offer similar functionality for key codes and keyboard event handling:

  • ts-keycode-enum: Provides TypeScript enums for KeyboardEvent.keyCode values.
  • ts-key-enum: Offers enums for KeyboardEvent.key, allowing developers to handle characters associated with key events in a standardized way.

Installation

Install the package via npm:

npm install keyboard-code-enum

Usage

Import the Code enum and/or the CodeUtils utilities as needed:

import { Code, CodeUtils } from 'keyboard-code-enum';

Example: Handling Keyboard Events

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);
    }
});

API

Code Enum

The 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 Namespace

CodeUtils 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

Contributing

Contributions are welcome! If you’d like to improve this library, please fork the repository, make your changes, and submit a pull request.

License

This project is licensed under the MIT License.

Keywords

FAQs

Package last updated on 13 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc