Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Last active April 30, 2016 04:24
Show Gist options
  • Save TravisMullen/e0d7b18a1f40c06878851d1e049c3254 to your computer and use it in GitHub Desktop.
Save TravisMullen/e0d7b18a1f40c06878851d1e049c3254 to your computer and use it in GitHub Desktop.
Create Keyboard Code (Key to Code) as Object
// http://jsfiddle.net/vWx8V/
// use `keyCodes` for complete object of keyboard codes
'use strict';
var keyCodes = ( function() {
var codes = {
'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,
'command' : 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,
'-' : 189,
'.' : 190,
'/' : 191,
'`' : 192,
'[' : 219,
'\\' : 220,
']' : 221,
'\'' : 222
},
i;
// lower case chars
for ( i = 97; i < 123; i++ ) { codes[ String.fromCharCode( i ) ] = i - 32; }
// numbers
for ( i = 48; i < 58; i++ ) { codes[ i - 48 ] = i; }
// function keys
for ( i = 1; i < 13; i++ ) { codes[ 'f' + i ] = i + 111; }
// numpad keys
for ( i = 0; i < 10; i++ ) { codes[ 'numpad ' + i ] = i + 96; }
return codes;
}() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment