Separate Module for Key Normalization
This moves key normalization into its own module so that it can be re-used elsewhere.
Tim Yung committed
Jan 6, 2014 at 09:44 UTC
182a237fa77185ebb823e0f97ae3fa13abe4421a
2 files changed
+87
-59
src/dom/getEventKey.js
new
+85
@@ -0,0 +1,85 @@
1
+/**
2
+ * Copyright 2013 Facebook, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * @providesModule getEventKey
17
+ * @typechecks static-only
18
+ */
19
+
20
+"use strict";
21
+
22
+/**
23
+ * Normalization of deprecated HTML5 "key" values
24
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
25
+ */
26
+var normalizeKey = {
27
+ 'Esc': 'Escape',
28
+ 'Spacebar': ' ',
29
+ 'Left': 'ArrowLeft',
30
+ 'Up': 'ArrowUp',
31
+ 'Right': 'ArrowRight',
32
+ 'Down': 'ArrowDown',
33
+ 'Del': 'Delete',
34
+ 'Win': 'OS',
35
+ 'Menu': 'ContextMenu',
36
+ 'Apps': 'ContextMenu',
37
+ 'Scroll': 'ScrollLock',
38
+ 'MozPrintableKey': 'Unidentified'
39
+};
40
+
41
+/**
42
+ * Translation from legacy "which/keyCode" to HTML5 "key"
43
+ * Only special keys supported, all others depend on keyboard layout or browser
44
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
45
+ */
46
+var translateToKey = {
47
+ 8: 'Backspace',
48
+ 9: 'Tab',
49
+ 12: 'Clear',
50
+ 13: 'Enter',
51
+ 16: 'Shift',
52
+ 17: 'Control',
53
+ 18: 'Alt',
54
+ 19: 'Pause',
55
+ 20: 'CapsLock',
56
+ 27: 'Escape',
57
+ 32: ' ',
58
+ 33: 'PageUp',
59
+ 34: 'PageDown',
60
+ 35: 'End',
61
+ 36: 'Home',
62
+ 37: 'ArrowLeft',
63
+ 38: 'ArrowUp',
64
+ 39: 'ArrowRight',
65
+ 40: 'ArrowDown',
66
+ 45: 'Insert',
67
+ 46: 'Delete',
68
+ 112: 'F1', 113: 'F2', 114: 'F3', 115: 'F4', 116: 'F5', 117: 'F6',
69
+ 118: 'F7', 119: 'F8', 120: 'F9', 121: 'F10', 122: 'F11', 123: 'F12',
70
+ 144: 'NumLock',
71
+ 145: 'ScrollLock',
72
+ 224: 'Meta'
73
+};
74
+
75
+/**
76
+ * @param {object} nativeEvent Native browser event.
77
+ * @return {string} Normalized `key` property.
78
+ */
79
+function getEventKey(nativeEvent) {
80
+ return 'key' in nativeEvent ?
81
+ normalizeKey[nativeEvent.key] || nativeEvent.key :
82
+ translateToKey[nativeEvent.which || nativeEvent.keyCode] || 'Unidentified';
83
+}
84
+
85
+module.exports = getEventKey;
src/event/synthetic/SyntheticKeyboardEvent.js
+2
-59
@@ -21,71 +21,14 @@
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
-};
24
+var getEventKey = require('getEventKey');
25
26
/**
27
* @interface KeyboardEvent
28
* @see http://www.w3.org/TR/DOM-Level-3-Events/
29
*/
30
var KeyboardEventInterface = {
84
- key: function(event) {
85
- return 'key' in event ?
86
- normalizeKey[event.key] || event.key :
87
- translateToKey[event.which || event.keyCode] || 'Unidentified';
88
- },
31
+ key: getEventKey,
32
location: null,
33
ctrlKey: null,
34
shiftKey: null,