npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

keyboard-code-enum

v0.0.2

Published

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

Downloads

63

Readme

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.

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.