Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule getEventKey
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var getEventCharCode = require('getEventCharCode');
16
17
/**
18
* Normalization of deprecated HTML5 `key` values
19
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
20
*/
21
var normalizeKey = {
22
'Esc': 'Escape',
23
'Spacebar': ' ',
24
'Left': 'ArrowLeft',
25
'Up': 'ArrowUp',
26
'Right': 'ArrowRight',
27
'Down': 'ArrowDown',
28
'Del': 'Delete',
29
'Win': 'OS',
30
'Menu': 'ContextMenu',
31
'Apps': 'ContextMenu',
32
'Scroll': 'ScrollLock',
33
'MozPrintableKey': 'Unidentified'
34
};
35
36
/**
37
* Translation from legacy `keyCode` to HTML5 `key`
38
* Only special keys supported, all others depend on keyboard layout or browser
39
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names
40
*/
41
var translateToKey = {
42
8: 'Backspace',
43
9: 'Tab',
44
12: 'Clear',
45
13: 'Enter',
46
16: 'Shift',
47
17: 'Control',
48
18: 'Alt',
49
19: 'Pause',
50
20: 'CapsLock',
51
27: 'Escape',
52
32: ' ',
53
33: 'PageUp',
54
34: 'PageDown',
55
35: 'End',
56
36: 'Home',
57
37: 'ArrowLeft',
58
38: 'ArrowUp',
59
39: 'ArrowRight',
60
40: 'ArrowDown',
61
45: 'Insert',
62
46: 'Delete',
63
112: 'F1', 113: 'F2', 114: 'F3', 115: 'F4', 116: 'F5', 117: 'F6',
64
118: 'F7', 119: 'F8', 120: 'F9', 121: 'F10', 122: 'F11', 123: 'F12',
65
144: 'NumLock',
66
145: 'ScrollLock',
67
224: 'Meta'
68
};
69
70
/**
71
* @param {object} nativeEvent Native browser event.
72
* @return {string} Normalized `key` property.
73
*/
74
function getEventKey(nativeEvent) {
75
if (nativeEvent.key) {
76
// Normalize inconsistent values reported by browsers due to
77
// implementations of a working draft specification.
78
79
// FireFox implements `key` but returns `MozPrintableKey` for all
80
// printable characters (normalized to `Unidentified`), ignore it.
81
var key = normalizeKey[nativeEvent.key] || nativeEvent.key;
82
if (key !== 'Unidentified') {
83
return key;
84
}
85
}
86
87
// Browser does not implement `key`, polyfill as much of it as we can.
88
if (nativeEvent.type === 'keypress') {
89
var charCode = getEventCharCode(nativeEvent);
90
91
// The enter-key is technically both printable and non-printable and can
92
// thus be captured by `keypress`, no other non-printable key should.
93
return charCode === 13 ? 'Enter' : String.fromCharCode(charCode);
94
}
95
if (nativeEvent.type === 'keydown' || nativeEvent.type === 'keyup') {
96
// While user keyboard layout determines the actual meaning of each
97
// `keyCode` value, almost all function keys have a universal value.
98
return translateToKey[nativeEvent.keyCode] || 'Unidentified';
99
}
100
return '';
101
}
102
103
module.exports = getEventKey;
104
105