forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboardEvent.js
More file actions
291 lines (271 loc) · 6.84 KB
/
keyboardEvent.js
File metadata and controls
291 lines (271 loc) · 6.84 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* @typedef {object} KeyEvent
* @property {'keydown' | 'keypress' | 'keyup'} type the type of the event
* @property {boolean} bubbles whether the event bubbles up through the DOM or not
* @property {boolean} cancelable whether the event is cancelable or not
* @property {number} which the key code of the key pressed
* @property {number} keyCode the key code of the key pressed
* @property {string} key the key pressed
* @property {boolean} ctrlKey whether the ctrl key was pressed or not
* @property {boolean} shiftKey whether the shift key was pressed or not
* @property {boolean} altKey whether the alt key was pressed or not
* @property {boolean} metaKey whether the meta key was pressed or not
*/
const keys = {
// arrow keys
37: "ArrowLeft",
38: "ArrowUp",
39: "ArrowRight",
40: "ArrowDown",
// special keys
8: "Backspace",
9: "Tab",
13: "Enter",
16: "ShiftLeft",
17: "ControlLeft",
18: "AltLeft",
19: "Pause",
20: "CapsLock",
27: "Escape",
32: " ",
33: "PageUp",
34: "PageDown",
35: "End",
36: "Home",
45: "Insert",
46: "Delete",
};
let initKeyboardEventType = (function (event) {
try {
event.initKeyboardEvent(
"keyup", // in DOMString typeArg
false, // in boolean canBubbleArg
false, // in boolean cancelableArg
window, // in views::AbstractView viewArg
"+", // [test]in DOMString keyIdentifierArg | webkit event.keyIdentifier | IE9 event.key
3, // [test]in unsigned long keyLocationArg | webkit event.keyIdentifier | IE9 event.location
true, // [test]in boolean ctrlKeyArg | webkit event.shiftKey | old webkit event.ctrlKey | IE9 event.modifiersList
false, // [test]shift | alt
true, // [test]shift | alt
false, // meta
false, // altGraphKey
);
return (
((((event["keyIdentifier"] || event["key"]) === "+" &&
event["location"]) ||
event["keyLocation"] === 3) &&
(event.ctrlKey
? event.altKey
? // webkit
1
: 3
: event.shiftKey
? 2
: // webkit
4)) || // IE9
9
); // FireFox|w3c
} catch (error) {
initKeyboardEventType = 0;
}
})(document.createEvent("KeyboardEvent"));
const keyboardEventPropertiesDictionary = {
char: "",
key: "",
location: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
repeat: false,
locale: "",
detail: 0,
bubbles: false,
cancelable: false,
//legacy properties
keyCode: 0,
charCode: 0,
which: 0,
};
const own = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
const ObjectDefineProperty =
Object.defineProperty ||
function (obj, prop, val) {
if ("value" in val) {
obj[prop] = val["value"];
}
};
/**
* Creates a keyboard event
* @param {'keydown' | 'keyup'} type type of the event
* @param {KeyEvent} dict
* @returns
*/
export default function KeyboardEvent(type, dict) {
let event;
if (initKeyboardEventType) {
event = document.createEvent("KeyboardEvent");
} else {
event = document.createEvent("Event");
}
let propName;
let localDict = {};
if (!dict.key && (dict.keyCode || dict.which)) {
let key = keys[dict.keyCode || dict.which];
if (!key) key = String.fromCharCode(dict.keyCode || dict.which);
dict.key = key;
} else if (dict.key && !dict.which && !dict.keyCode) {
let keyCode = Object.keys(keys).find((key) => keys[key] === dict.key);
if (!keyCode) keyCode = dict.key.charCodeAt(0);
dict.keyCode = keyCode;
dict.which = keyCode;
}
for (propName in keyboardEventPropertiesDictionary)
if (own(keyboardEventPropertiesDictionary, propName)) {
localDict[propName] = ((own(dict, propName) && dict) ||
keyboardEventPropertiesDictionary)[propName];
}
const ctrlKey = localDict["ctrlKey"];
const shiftKey = localDict["shiftKey"];
const altKey = localDict["altKey"];
const metaKey = localDict["metaKey"];
const altGraphKey = localDict["altGraphKey"];
const modifiersListArg =
initKeyboardEventType > 3
? (
(ctrlKey ? "Control" : "") +
(shiftKey ? " Shift" : "") +
(altKey ? " Alt" : "") +
(metaKey ? " Meta" : "") +
(altGraphKey ? " AltGraph" : "")
).trim()
: null;
const key = localDict["key"] + "";
const char = localDict["char"] + "";
const location = localDict["location"];
const keyCode =
localDict["keyCode"] ||
(localDict["keyCode"] = (key && key.charCodeAt(0)) || 0);
const charCode =
localDict["charCode"] ||
(localDict["charCode"] = (char && char.charCodeAt(0)) || 0);
const bubbles = localDict["bubbles"];
const cancelable = localDict["cancelable"];
const repeat = localDict["repeat"];
const locale = localDict["locale"];
const view = window;
localDict["which"] || (localDict["which"] = localDict["keyCode"]);
if ("initKeyEvent" in event) {
//FF
//https://developer.mozilla.org/en/DOM/event.initKeyEvent
event.initKeyEvent(
type,
bubbles,
cancelable,
view,
ctrlKey,
altKey,
shiftKey,
metaKey,
keyCode,
charCode,
);
} else if (initKeyboardEventType && "initKeyboardEvent" in event) {
//https://developer.mozilla.org/en/DOM/KeyboardEvent#initKeyboardEvent()
if (initKeyboardEventType === 1) {
// webkit
//http://stackoverflow.com/a/8490774/1437207
//https://bugs.webkit.org/show_bug.cgi?id=13368
event.initKeyboardEvent(
type,
bubbles,
cancelable,
view,
key,
location,
ctrlKey,
shiftKey,
altKey,
metaKey,
altGraphKey,
);
} else if (initKeyboardEventType === 2) {
// old webkit
//http://code.google.com/p/chromium/issues/detail?id=52408
event.initKeyboardEvent(
type,
bubbles,
cancelable,
view,
ctrlKey,
altKey,
shiftKey,
metaKey,
keyCode,
charCode,
);
} else if (initKeyboardEventType === 3) {
// webkit
event.initKeyboardEvent(
type,
bubbles,
cancelable,
view,
key,
location,
ctrlKey,
altKey,
shiftKey,
metaKey,
altGraphKey,
);
} else if (initKeyboardEventType === 4) {
// IE9
//http://msdn.microsoft.com/en-us/library/ie/ff975297(v=vs.85).aspx
event.initKeyboardEvent(
type,
bubbles,
cancelable,
view,
key,
location,
modifiersListArg,
repeat,
locale,
);
} else {
// FireFox|w3c
//http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent-initKeyboardEvent
//https://developer.mozilla.org/en/DOM/KeyboardEvent#initKeyboardEvent()
event.initKeyboardEvent(
type,
bubbles,
cancelable,
view,
char,
key,
location,
modifiersListArg,
repeat,
locale,
);
}
} else {
event.initEvent(type, bubbles, cancelable);
}
for (propName in keyboardEventPropertiesDictionary)
if (own(keyboardEventPropertiesDictionary, propName)) {
if (event[propName] !== localDict[propName]) {
try {
delete event[propName];
ObjectDefineProperty(event, propName, {
writable: true,
value: localDict[propName],
});
} catch (error) {
//Some properties is read-only
}
}
}
return event;
}