Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tag: 0.2.3
Fetching contributors…

Cannot retrieve contributors at this time

67 lines (43 sloc) 1.449 kB

keycode

Simple map of keyboard codes.

Installation

$ component install timoxley/keycode

Example

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

API

The keycode method responds differently depending on whether you're looking up a keycode or a keyname.

keycode(keycode:Object)

Returns the keycode for the given object's .keyCode.

keycode(keycode:Number)

Returns the lowercase title for given numeric keycode.

keycode(13) // => 'enter'

keycode(title:String)

Returns numeric keycode for given key title.

keycode('Enter') // => 13

Letter casing is insensitive so the following will still work.

keycode('eNtEr') // => 13

KeyCode and KeyName maps are available directly as keycode.codes and keycode.names respectively.

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

Event Support

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

Credit

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

License

MIT

Jump to Line
Something went wrong with that request. Please try again.