-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
101 lines (86 loc) · 1.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var defaultOptions = {
cmd: 'Cmd',
ctrl: 'Ctrl',
alt: 'Alt',
shift: 'Shift',
joinWith: ' + '
}
var options = {}
var keyMap = {
8: 'Backspace',
9: 'Tab',
13: 'Enter',
27: 'Escape',
32: 'Space',
36: 'Home',
33: 'Page Up',
34: 'Page Down',
35: 'End',
37: 'Left',
38: 'Up',
39: 'Right',
40: 'Down',
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',
186: ';',
187: '=',
188: ',',
189: '-',
190: '.',
192: '`',
222: "'"
}
function buildKeyMap (e) {
var isOnlyModifier = [16, 17, 18, 91, 93, 224].indexOf(e.keyCode) !== -1
var character = isOnlyModifier ? null : keyMap[e.keyCode] || String.fromCharCode(e.keyCode)
return {
character: character,
modifiers: {
cmd: e.metaKey,
ctrl: e.ctrlKey,
alt: e.altKey,
shift: e.shiftKey
}
}
}
function buildKeyArray (e) {
var map = buildKeyMap(e)
var modifiers = map.modifiers
var result = []
if (modifiers.cmd) result.push(options.cmd)
if (modifiers.ctrl) result.push(options.ctrl)
if (modifiers.alt) result.push(options.alt)
if (modifiers.shift) result.push(options.shift)
if (map.character) result.push(map.character)
return result
}
function event2string (e) {
return buildKeyArray(e).join(options.joinWith)
}
function details (e) {
var map = buildKeyMap(e)
var mods = map.modifiers
var hasModifier = mods.cmd || mods.ctrl || mods.alt || mods.shift
var result = {
hasKey: map.character != null,
hasModifier: hasModifier,
map: map
}
return result
}
module.exports = function (userOptions) {
options = Object.assign(defaultOptions, userOptions)
return event2string
}
module.exports.details = details