Coder Social home page Coder Social logo

keycode's Introduction

keycode

Simple map of keyboard codes.

Build Status

Installation

npm

$ npm install keycode

component

$ component install timoxley/keycode

Example

var keycode = require('keycode');
document.addEventListener('keydown', function(e) {
  console.log("You pressed", keycode(e))
})

API

keycode tries to make an intelligent guess as to what you're trying to discover based on the type of argument you supply.

keycode(keycode:Event)

Returns the name of the key associated with this event.

document.body.addEventListener('keyup', function(e) {
  console.log(keycode(e)) // prints name of key
})

Due to the keypress event being weird,keycode currently does not support the keypress event, but this should not be an issue as keydown and keyup work perfectly fine.

keycode(keycode:Number)

Returns the lowercase name of a given numeric keycode.

keycode(13) // => 'enter'

keycode(name:String)

Returns the numeric keycode for given key name.

keycode('Enter') // => 13

// keycode is not case sensitive
keycode('eNtEr') // => 13

Name Aliases

Common aliases are also supplied:

> for (var alias in keycode.aliases) { console.log(alias, keycode(keycode(alias))) }
ctl ctrl
pause pause/break
break pause/break
caps caps lock
escape esc
pgup page up
pgdn page down
ins insert
del delete
spc space

keycode.isEventKey(event: Event, nameOrCode: String | Number)

Tests if an keyboard event against a given name or keycode. Will return true if the event matches the given name or keycode, false otherwise.

// assume event is an keydown event with key 'enter'
keycode.isEventKey(event, 'enter') // => true
keycode.isEventKey(event, 'down') // => false

keycode.isEventKey(event, 13) // => true
keycode.isEventKey(event, 40) // => false

Maps

Key code/name maps are available directly as keycode.codes and keycode.names respectively.

keycode.names[13] // => 'enter'
keycode.codes['enter'] // => 13

Credit

 project  : keycode
 repo age : 3 years, 8 months
 active   : 29 days
 commits  : 66
 files    : 13
 authors  :
    49	Tim Oxley        74.3%
     4	jkroso           6.1%
     3	Amir Abu Shareb  4.5%
     1	Greg Reimer      1.5%
     1	Kenan Yildirim   1.5%
     1	Abel Toledano    1.5%
     1	Sam              1.5%
     1	TJ Holowaychuk   1.5%
     1	Yoshua Wuyts     1.5%
     1	Nathan Zadoks    1.5%
     1	Brenton Simpson  1.5%
     1	Brian Noguchi    1.5%
     1	Gilad Peleg      1.5%

Original key mappings lifted from http://jsfiddle.net/vWx8V/ via http://stackoverflow.com/questions/5603195/full-list-of-javascript-keycodes

License

MIT

keycode's People

Contributors

aaronbeall avatar appsforartists avatar artfuldev avatar atabel avatar bnoguchi avatar cmj91uk avatar dotcs avatar edef1c avatar eps1lon avatar ffxsam avatar finico avatar greim avatar hodavidhara avatar jkroso avatar karlhorky avatar kenany avatar nminhnguyen avatar pgilad avatar timoxley avatar tj avatar yoshuawuyts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keycode's Issues

`⌘` on Firefox returns undefined

On FF 64 and OSX 10.13.6, both the left and right command key returns undefined. For on Firefox, event.keyCode is 224 and event.metaKey is true.

screenshot 2018-09-19 10 39 41

Update version on npm

Hi,

I noticed an inconsistency between the repo and the version on npm. Both are v2.2.0 but the one on npm still has the wrong typings file referenced in package.json (./keycode.d.ts instead of ./index.d.ts).

VSCode always bugs me about it.

Thanks!

No lock file found in the keycode project on Tag: 2.2.0

Issue: There is no package-lock.json or npm-shrinkwrap.json file uploaded to the GitHub repository https://github.com/timoxley/keycode

Questions: We are conducting a research study on the lock files used in JS projects. We were curious:

  1. Will you upload any lock files to GitHub as recommended above? (Yes/No), and why?:
  2. Do you have any additional comments? (If so, please write it down):

For any publication or research report based on this study, we will share all responses from developers in an anonymous way. Both your projects and personal information will be kept confidential.

Rationale: NPM introduced package-lock.json and npm-shrimpwrap.json files to capture the exact dependency tree installed at any point in time. When package.json defines the required dependencies and their respective versions using semantic versioning (e.g., “express”: “^4.16.4”), npm always downloads the latest version of packages to satisfy the specified version ranges (e.g., 4.17.1)[1]. If the latest version of any package keeps changing and has backward incompatibility issues with previous versions, the project may have an inconsistent running environment and get intermittent failures. In such scenarios, it can be very difficult for developers to debug programs and settle down the software environment [2].

List of Risks:

  • Nondeterministic package downloads and installations across different environments [3]
  • Produce irreproducible builds [4]
  • Inconsistent results of program execution [5]

Suggested Solution: Please fixate the dependencies by either specifying the exact library version in the package.json file or by uploading the package-lock.json or npm-shrinkwrap.json file to GitHub.

References:
https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json
https://blog.logrocket.com/why-you-should-use-package-lock-json/
2019. 10 npm Security Best Practices. https://snyk.io/blog/ten-npm-security-best-practices/.
Pronnoy Goswami, Saksham Gupta, Zhiyuan Li, Na Meng, and Daphne Yao. 2020. Investigating The Reproducibility of NPM Packages. In2020 IEEE International
2021. Npm Security Best Practices. https://bytesafe.dev/posts/npm-security-best-practices/#no9-deterministic-results.

everything should be in the maps

it looks like you have added support for capital letters which is cool but without them being available in the maps it feels a bit error prone; now you can't just throw whatever at the maps.
If we are going to advertise the maps are available to use then they should work in all the cases the function works. BTW the maps are all I ever use

keycode(16) returns up-arrow symbol instead of 'shift'

A workaround is possible but code like this is no longer possible:

var pressed = keycode(ev);
if ('shift' === pressed) {
  // do something
}

I can provide a PR later if needed. Re-ordering the codes object literal ought to fix it.

case sensitivity

Should keycode('a') return something different to keycode('A')?

Support other keyboard languages (?)

Hi!

it would be great if this library supports other keyboard languages than English.

For example keycode returns undefined for the key "ß" (German letter Wikipedia) on a German Keyboard (also the mapping of other keys is not compatible with the German keyboard layout QWERTZ).

Cheers,
Gregor

Exported External Package Typings File

Hi,

When importing the module via the ES6 import syntax using Visual Studio Code an error is displayed about the typings file.

"Exported external package typings file '/../node_modules/keycode/keycode.d.ts' is not a module. Please contact the package author to update the package definition"

It doesn't stop the compilation via Babel and just appears as an error within VSCode. However it would be nice to not have the error so that the handy Warnings and Errors panel can display 0 and 0 again.

Thanks in advance.

Which, KeyCode, and CharCode are all deprecated

Problem:

The event properties which, keyCode, and charCode are all removed from the Web standards. See the deprecation notice on MDN below:

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/charCode

While they are supported now still in the major browsers, they may not be at some point in the future.

The preferred direction is to use event.key. The one problem is that IE and older versions of Edge use older versions of the "key" spec, and may return different values. The "Example" listed here shows some of the different values:

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key

This shim is an example of how to re-mediate some of the differences: https://github.com/shvaikalesh/shim-keyboard-event-key/blob/master/index.js

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.