Skip to content

Commit

Permalink
Merge pull request facebook#607 from syranide/html5key
Browse files Browse the repository at this point in the history
Polyfill and normalize HTML5 "key", deprecates which and keyCode
  • Loading branch information
yungsters committed Dec 23, 2013
2 parents 82a26ad + 15ce8ec commit 9e0987c
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/event/synthetic/SyntheticKeyboardEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,71 @@

var SyntheticUIEvent = require('SyntheticUIEvent');

/**
* Normalization of deprecated HTML5 "key" values
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
*/

var normalizeKey = {
'Esc': 'Escape',
'Spacebar': ' ',
'Left': 'ArrowLeft',
'Up': 'ArrowUp',
'Right': 'ArrowRight',
'Down': 'ArrowDown',
'Del': 'Delete',
'Win': 'OS',
'Menu': 'ContextMenu',
'Apps': 'ContextMenu',
'Scroll': 'ScrollLock',
'MozPrintableKey': 'Unidentified'
};

/**
* Translation from legacy "which/keyCode" to HTML5 "key"
* Only special keys supported, all others depend on keyboard layout or browser
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
*/

var translateToKey = {
8: 'Backspace',
9: 'Tab',
12: 'Clear',
13: 'Enter',
16: 'Shift',
17: 'Control',
18: 'Alt',
19: 'Pause',
20: 'CapsLock',
27: 'Escape',
32: ' ',
33: 'PageUp',
34: 'PageDown',
35: 'End',
36: 'Home',
37: 'ArrowLeft',
38: 'ArrowUp',
39: 'ArrowRight',
40: 'ArrowDown',
45: 'Insert',
46: 'Delete',
112: 'F1', 113: 'F2', 114: 'F3', 115: 'F4', 116: 'F5', 117: 'F6',
118: 'F7', 119: 'F8', 120: 'F9', 121: 'F10', 122: 'F11', 123: 'F12',
144: 'NumLock',
145: 'ScrollLock',
224: 'Meta'
};

/**
* @interface KeyboardEvent
* @see http://www.w3.org/TR/DOM-Level-3-Events/
*/
var KeyboardEventInterface = {
'char': null,
key: null,
key: function(event) {
return 'key' in event ?
normalizeKey[event.key] || event.key :
translateToKey[event.which || event.keyCode] || 'Unidentified';
},
location: null,
ctrlKey: null,
shiftKey: null,
Expand All @@ -36,6 +94,7 @@ var KeyboardEventInterface = {
repeat: null,
locale: null,
// Legacy Interface
'char': null,
charCode: null,
keyCode: null,
which: null
Expand Down

0 comments on commit 9e0987c

Please sign in to comment.