@samitouri / QOS-React-2 / commits / 15ce8ecfe9

Polyfill and normalize HTML5 "key", deprecates which and keyCode

Andreas Svensson committed Nov 25, 2013 at 20:24 UTC 15ce8ecfe9911bf4af96485c10f7ad2627d55b42
1 file changed +61 -2
src/event/synthetic/SyntheticKeyboardEvent.js
+61 -2
@@ -21,13 +21,71 @@
21
22 var SyntheticUIEvent = require('SyntheticUIEvent');
23
24 +/**
25 + * Normalization of deprecated HTML5 "key" values
26 + * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
27 + */
28 +
29 +var normalizeKey = {
30 + 'Esc': 'Escape',
31 + 'Spacebar': ' ',
32 + 'Left': 'ArrowLeft',
33 + 'Up': 'ArrowUp',
34 + 'Right': 'ArrowRight',
35 + 'Down': 'ArrowDown',
36 + 'Del': 'Delete',
37 + 'Win': 'OS',
38 + 'Menu': 'ContextMenu',
39 + 'Apps': 'ContextMenu',
40 + 'Scroll': 'ScrollLock',
41 + 'MozPrintableKey': 'Unidentified'
42 +};
43 +
44 +/**
45 + * Translation from legacy "which/keyCode" to HTML5 "key"
46 + * Only special keys supported, all others depend on keyboard layout or browser
47 + * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
48 + */
49 +
50 +var translateToKey = {
51 + 8: 'Backspace',
52 + 9: 'Tab',
53 + 12: 'Clear',
54 + 13: 'Enter',
55 + 16: 'Shift',
56 + 17: 'Control',
57 + 18: 'Alt',
58 + 19: 'Pause',
59 + 20: 'CapsLock',
60 + 27: 'Escape',
61 + 32: ' ',
62 + 33: 'PageUp',
63 + 34: 'PageDown',
64 + 35: 'End',
65 + 36: 'Home',
66 + 37: 'ArrowLeft',
67 + 38: 'ArrowUp',
68 + 39: 'ArrowRight',
69 + 40: 'ArrowDown',
70 + 45: 'Insert',
71 + 46: 'Delete',
72 + 112: 'F1', 113: 'F2', 114: 'F3', 115: 'F4', 116: 'F5', 117: 'F6',
73 + 118: 'F7', 119: 'F8', 120: 'F9', 121: 'F10', 122: 'F11', 123: 'F12',
74 + 144: 'NumLock',
75 + 145: 'ScrollLock',
76 + 224: 'Meta'
77 +};
78 +
79 /**
80 * @interface KeyboardEvent
81 * @see http://www.w3.org/TR/DOM-Level-3-Events/
82 */
83 var KeyboardEventInterface = {
29 - 'char': null,
30 - key: null,
84 + key: function(event) {
85 + return 'key' in event ?
86 + normalizeKey[event.key] || event.key :
87 + translateToKey[event.which || event.keyCode] || 'Unidentified';
88 + },
89 location: null,
90 ctrlKey: null,
91 shiftKey: null,
@@ -36,6 +94,7 @@ var KeyboardEventInterface = {
94 repeat: null,
95 locale: null,
96 // Legacy Interface
97 + 'char': null,
98 charCode: null,
99 keyCode: null,
100 which: null