Skip to content

Commit

Permalink
Merge pull request #5 from jkroso/master
Browse files Browse the repository at this point in the history
oops can't export name
  • Loading branch information
timoxley committed Dec 30, 2012
2 parents c893673 + 0d5bde7 commit a52e32c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 101 deletions.
17 changes: 11 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,29 @@ looking up a keycode or a keyname.

##keycode(keycode:Number)

Returns name for given numeric keycode.
Returns the lowercase title for given numeric keycode.

```js
keycode(13) // => 'Enter'
keycode(13) // => 'enter'
```

##keycode(name:String)
##keycode(title:String)

Returns numeric keycode for given key name.
Returns numeric keycode for given key title.

```js
keycode('Enter') // => 13
```
Letter casing is insensitive so the following will still work.

KeyCode and KeyName maps are available directly as `keycode.code` and `keycode.name` respectively.
```js
keycode('eNtEr') // => 13
```

KeyCode and KeyName maps are available directly as `keycode.code` and `keycode.title` respectively.

```js
keycode.name[13] // => 'Enter'
keycode.title[13] // => 'enter'
keycode.code['Enter'] // => 13
```
## Event Support
Expand Down
198 changes: 104 additions & 94 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @api public
*/
exports = module.exports = function (search) {
if (typeof search === 'string') return codes[search]
if (typeof search === 'string') return codes[search.toLowerCase()]
return names[search]
}

Expand All @@ -19,95 +19,37 @@ exports = module.exports = function (search) {
* exports.code['Enter'] // => 13
*/
var codes = exports.code = {
'Backspace': 8,
'Tab': 9,
'Enter': 13,
'Shift': 16,
'Ctrl': 17,
'Alt': 18,
'Pause/Break': 19,
'Caps Lock': 20,
'Esc': 27,
'Space': 32,
'Page Up': 33,
'Page Down': 34,
'End': 35,
'Home': 36,
'Left': 37,
'Up': 38,
'Right': 39,
'Down': 40,
'Insert': 45,
'Delete': 46,
'0': 48,
'1': 49,
'2': 50,
'3': 51,
'4': 52,
'5': 53,
'6': 54,
'7': 55,
'8': 56,
'9': 57,
'A': 65,
'B': 66,
'C': 67,
'D': 68,
'E': 69,
'F': 70,
'G': 71,
'H': 72,
'I': 73,
'J': 74,
'K': 75,
'L': 76,
'M': 77,
'N': 78,
'O': 79,
'P': 80,
'Q': 81,
'R': 82,
'S': 83,
'T': 84,
'U': 85,
'V': 86,
'W': 87,
'X': 88,
'Y': 89,
'Z': 90,
'Windows': 91,
'Right Click': 93,
'Numpad 0': 96,
'Numpad 1': 97,
'Numpad 2': 98,
'Numpad 3': 99,
'Numpad 4': 100,
'Numpad 5': 101,
'Numpad 6': 102,
'Numpad 7': 103,
'Numpad 8': 104,
'Numpad 9': 105,
'Numpad *': 106,
'Numpad +': 107,
'Numpad -': 109,
'Numpad .': 110,
'Numpad /': 111,
'F1': 112,
'F2': 113,
'F3': 114,
'F4': 115,
'F5': 116,
'F6': 117,
'F7': 118,
'F8': 119,
'F9': 120,
'F10': 121,
'F11': 122,
'F12': 123,
'Num Lock': 144,
'Scroll Lock': 145,
'My Computer': 182,
'My Calculator': 183,
'backspace': 8,
'tab': 9,
'enter': 13,
'shift': 16,
'ctrl': 17,
'alt': 18,
'pause/break': 19,
'caps lock': 20,
'esc': 27,
'space': 32,
'page up': 33,
'page down': 34,
'end': 35,
'home': 36,
'left': 37,
'up': 38,
'right': 39,
'down': 40,
'insert': 45,
'delete': 46,
'windows': 91,
'right click': 93,
'numpad *': 106,
'numpad +': 107,
'numpad -': 109,
'numpad .': 110,
'numpad /': 111,
'num lock': 144,
'scroll lock': 145,
'my computer': 182,
'my calculator': 183,
';': 186,
'=': 187,
',': 188,
Expand All @@ -121,13 +63,81 @@ var codes = exports.code = {
"'": 222
}

/*!
* Programatically add the following
*/
for (var i = 48; i < 58; i++) codes[i - 48] = i
// '0': 48,
// '1': 49,
// '2': 50,
// '3': 51,
// '4': 52,
// '5': 53,
// '6': 54,
// '7': 55,
// '8': 56,
// '9': 57,

for (i = 97; i < 123; i++) codes[String.fromCharCode(i)] = i - 32
// 'a': 65,
// 'b': 66,
// 'c': 67,
// 'd': 68,
// 'e': 69,
// 'f': 70,
// 'g': 71,
// 'h': 72,
// 'i': 73,
// 'j': 74,
// 'k': 75,
// 'l': 76,
// 'm': 77,
// 'n': 78,
// 'o': 79,
// 'p': 80,
// 'q': 81,
// 'r': 82,
// 's': 83,
// 't': 84,
// 'u': 85,
// 'v': 86,
// 'w': 87,
// 'x': 88,
// 'y': 89,
// 'z': 90,

for (i = 1; i < 13; i++) codes['f'+i] = i + 111
// 'f1': 112,
// 'f2': 113,
// 'f3': 114,
// 'f4': 115,
// 'f5': 116,
// 'f6': 117,
// 'f7': 118,
// 'f8': 119,
// 'f9': 120,
// 'f10': 121,
// 'f11': 122,
// 'f12': 123,

for (i = 0; i < 10; i++) codes['numpad '+i] = i + 96
// 'numpad 0': 96,
// 'numpad 1': 97,
// 'numpad 2': 98,
// 'numpad 3': 99,
// 'numpad 4': 100,
// 'numpad 5': 101,
// 'numpad 6': 102,
// 'numpad 7': 103,
// 'numpad 8': 104,
// 'numpad 9': 105,

/**
* Get by code
*
* exports.name[13] // => 'Enter'
*/
var names = exports.name = {}
var names = exports.title = {}

Object.keys(codes).forEach(function (name) {
names[codes[name]] = name
})
// Create reverse mapping
for (i in codes) names[codes[i]] = i
10 changes: 9 additions & 1 deletion test/keycode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ var assert = require('timoxley-assert')
it('is commutative', function() {
var count = 0
for (var key in keycode.code) {
assert.strictEqual(keycode(key), keycode(keycode(keycode(key))))
assert.strictEqual(key, keycode(keycode(key)))
count++
}
console.debug('Tested %d keys', count)
})
it('can expose maps', function () {
var count = 0
for (var name in keycode.code) {
assert.strictEqual(name, keycode.title[keycode.code[name]])
count++
}
console.debug('Tested %d keys', count)
})

0 comments on commit a52e32c

Please sign in to comment.