Giter Site home page Giter Site logo

itzhangxi / ts-keycode-enum Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nfriend/ts-keycode-enum

0.0 1.0 0.0 35 KB

A TypeScript enum definition that maps human-friendly key names to JavaScript key codes

License: MIT License

TypeScript 100.00%

ts-keycode-enum's Introduction

ts-keycode-enum

A TypeScript enum definition that maps human-friendly key names to JavaScript key codes

Important note

event.which and event.keyCode have been deprecated. It is recommended to instead use event.key in combination with this very similar module: ts-key-enum.

Install

npm install ts-keycode-enum --save

Purpose

Tired of referencing JavaScript keycodes by number?

onKeyPress = (ev) => {

    // gross
    if (ev.which === 13) {
        ...
    }
}

Me too. With this module, you can do this instead (in a TypeScript file):

onKeyPress = (ev) => {

    // much better
    if (ev.which === Key.Enter) {
        ...
    }
}

Usage

To use this module, import the Key enum at the top of your TypeScript file using the enum:

import { Key } from 'ts-keycode-enum';

You can now use a readable enum value in place of any raw keycodes throughout the file:

if (ev.which === Key.Escape) { ... }

See Key.enum.ts for a complete list of available keys.

In addition, to aid with readability, a number of enum values have aliases. For example:

// this  true - these values are equal
Key.One === Key.ExclamationMark

Use whichever value makes more sense for readability.

Building

To build this module yourself, first install its dependencies using

npm install

Then, run the build using

npm run build

Build output is placed in the dist directory.

Contributions

This module only contains some of the most commonly-used key codes. If it's missing keycodes that you need, feel free to request the codes via an issue or a pull request.

License

MIT

ts-keycode-enum's People

Contributors

nfriend avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.