/
index.js
245 lines (210 loc) · 5.13 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var aliases = {
"left": "ArrowLeft",
"right": "ArrowRight",
"up": "ArrowUp",
"down": "ArrowDown",
"bs": "Backspace",
"menu": "ContextMenu",
"apps": "ContextMenu",
"del": "Delete",
"return": "Enter",
"cr": "Enter",
"esc": "Escape",
"pgup": "PageUp",
"pgdn": "PageDown",
"lt": "<",
"less": "<",
"lesser": "<",
"gt": ">",
"greater": ">"
}
var enUsTranslations = {
"Backquote": ["`", "~"],
"Digit1": ["1", "!"],
"Digit2": ["2", "@"],
"Digit3": ["3", "#"],
"Digit4": ["4", "$"],
"Digit5": ["5", "%"],
"Digit6": ["6", "^"],
"Digit7": ["7", "&"],
"Digit8": ["8", "*"],
"Digit9": ["9", "("],
"Digit0": ["0", ")"],
"Minus": ["-", "_"],
"Equal": ["=", "+"],
"Backslash": ["\\", "|"],
"BracketLeft": ["[", "{"],
"BracketRight": ["]", "}"],
"Semicolon": [";", ":"],
"Quote": ["'", '"'],
"Comma": [",", "<"],
"Period": [".", ">"],
"Slash": ["/", "?"]
}
var modifierMap = {
"a": "altKey",
"c": "ctrlKey",
"m": "metaKey",
"s": "shiftKey"
}
var specialCases = {
"<": "lt",
">": "gt"
}
var ignored = /^($|Unidentified$|Dead$|Alt|Control|Hyper|Meta|Shift|Super|OS)/
function alias(key) {
var keyLower = key.toLowerCase()
if (keyLower in aliases) {
return aliases[keyLower]
}
return key
}
function translate(translations, key, shift) {
var translation = translations[key]
if (Array.isArray(translation)) {
translation = translation[shift ? 1 : 0]
}
if (typeof translation !== "string") {
throw error({
id: "bad_translation",
context: key,
subject: translation,
message: "Bad translation value"
})
}
return translation
}
function codeToEnUsQwerty(code, shift) {
var key = code
if (/^Key/.test(code)) {
key = code.slice(3)
if (!shift) {
key = key.toLowerCase()
}
} else if (code in enUsTranslations) {
key = translate(enUsTranslations, code, shift)
}
return key
}
function error(props) {
var err = new Error(
("context" in props ? props.context + ": " : "") +
props.message + ": " + props.subject
)
err.id = props.id
err.context = props.context
err.subject = props.subject
return err
}
function stringify(event, options) {
options = options || {}
var alt = event.altKey
var ctrl = event.ctrlKey
var meta = event.metaKey
var shift = event.shiftKey
var key = event.key || "Unidentified"
var code = event.code || ""
if (options.translations && code in options.translations) {
key = alias(translate(options.translations, code, shift))
} else if (
(options.ignoreKeyboardLayout && !/^Numpad/.test(code)) ||
key === "Unidentified"
) {
key = codeToEnUsQwerty(code, shift)
} else {
key = alias(key)
if (key === " ") {
key = "Space"
}
}
if (ignored.test(key)) {
return ""
}
if (key.length === 1) {
shift = false
} else {
key = key.toLowerCase()
}
var modifiers = ""
if (alt) modifiers += "a-"
if (ctrl) modifiers += "c-"
if (meta) modifiers += "m-"
if (shift) modifiers += "s-"
if (options.ignoreCtrlAlt && modifiers === "a-c-" && key.length === 1) {
modifiers = ""
}
if (key in specialCases) {
key = specialCases[key]
}
if (modifiers || key.length > 1) {
return "<" + modifiers + key + ">"
}
return key
}
function parse(keyString) {
if (keyString.length === 1) {
if (/\s/.test(keyString)) {
throw error({
id: "invalid_key",
subject: keyString,
message: "Invalid key"
})
}
return {
key: keyString
}
}
var match = keyString.match(/^<((?:[a-z]-)*)([a-z\d]+|[^<>\s])>$/i)
if (!match) {
throw error({
id: "invalid_key",
subject: keyString,
message: "Invalid key"
})
}
var modifiers = match[1]
var key = match[2]
var obj = {
key: alias(key)
}
modifiers.split("-").slice(0, -1).forEach(function(modifier) {
var modifierLower = modifier.toLowerCase()
if (!(modifierLower in modifierMap)) {
throw error({
id: "unknown_modifier",
context: keyString,
subject: modifier,
message: "Unknown modifier"
})
}
var modifierName = modifierMap[modifierLower]
if (modifierName in obj) {
throw error({
id: "duplicate_modifier",
context: keyString,
subject: modifier,
message: "Duplicate modifier"
})
}
obj[modifierName] = true
if (obj.key.length === 1 && obj.shiftKey) {
throw error({
id: "disallowed_modifier",
context: keyString,
subject: modifier,
message: "Unusable modifier with single-character keys"
})
}
})
return obj
}
function normalize(keyString) {
return stringify(parse(keyString))
}
function parseSequence(keySequence) {
return keySequence.match(/<[^<>\s]+>|[\s\S]|^$/g)
}
exports.stringify = stringify
exports.parse = parse
exports.normalize = normalize
exports.parseSequence = parseSequence