-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.js
More file actions
319 lines (283 loc) · 7.9 KB
/
Copy pathInput.js
File metadata and controls
319 lines (283 loc) · 7.9 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* Utilities for keyboard and mouse input detection.
*
* @author Vicente G. (@SharkPool-SP)
*
* @version 2026.1.0.0
*/
class Input {
/** Keyboard */
static KEYBOARD_UTILS = class KeyboardUtils {
/**
* Converts a KeyboardEvent key to its proper english name.
*
* @param {KeyboardEvent} event keyboard event
* @returns proper key name
*/
static getKeyName(event) {
let name;
name = event.key.toLowerCase();
if (name.startsWith("arrow")) {
name = name.replace("arrow", "") + " arrow";
return name; // eg: left arrow
}
if (name.startsWith("page")) {
name = name.replace("page", "page ");
return name; // eg: page up
}
switch (name) {
case " ":
return "space";
case "printscreen":
return "print screen";
case "numlock":
return "num lock";
case "capslock":
return "caps lock";
case "audiovolumemute":
return "volume mute";
case "audiovolumedown":
return "volume down";
case "audiovolumeup":
return "volume up";
case "mediastop":
return "media stop";
case "mediaplaypause":
return "media playpause";
case "mediatrackprevious":
return "media previous";
case "mediatracknext":
return "media next";
default: {
if (name.length > 2) {
return name; // special char eg. shift or f12
}
if (Input.CAPS_LOCK_ENABLED !== event.shiftKey) {
name = name.toUpperCase();
}
return name;
}
}
}
};
static KEY_LISTENERS = [];
static KEYS_DOWN = new Map();
static CAPS_LOCK_ENABLED = false;
static _keyDownListener = window.addEventListener(
"keydown",
Input.keydownHandler,
);
static _keyUpListener = window.addEventListener("keyup", Input.keyupHandler);
/**
* Handler for key down events.
*
* @param {KeyboardEvent} event called by keydown
* @private
*/
static keydownHandler(event) {
const properName = Input.KEYBOARD_UTILS.getKeyName(event);
Input.KEYS_DOWN.set(properName, event);
if (properName === "caps lock") {
Input.CAPS_LOCK_ENABLED = event.getModifierState("CapsLock");
}
for (const func of Input.KEY_LISTENERS) func(event, "keydown");
}
/**
* Handler for key up events.
*
* @param {KeyboardEvent} event called by keyup
* @private
*/
static keyupHandler(event) {
const properName = Input.KEYBOARD_UTILS.getKeyName(event);
Input.KEYS_DOWN.delete(properName);
for (const func of Input.KEY_LISTENERS) func(event, "keyup");
}
/**
* Checks if a requested key is down.
*
* @param {String} keyName proper/inproper name of the key to be checked
* @param {Boolean} opt_ignoreCase (optional) if true, allows key 'a' and 'A' to return the same result
* @returns true if the requested key is down
*/
static isKeyDown(keyName, opt_ignoreCase) {
const properName = Input.KEYBOARD_UTILS.getKeyName({
key: keyName,
shiftKey: false,
});
return (
Input.KEYS_DOWN.has(keyName) ||
(opt_ignoreCase
? Input.KEYS_DOWN.has(properName) ||
Input.KEYS_DOWN.has(keyName.toUpperCase())
: false)
);
}
/**
* Returns all the keys currently pressed.
*
* @returns an array of keys currently down
*/
static getKeysDown() {
const keys = [];
Input.KEYS_DOWN.forEach((_, name) => {
keys.push(name);
});
return keys;
}
/**
* @callback KeyboardCallback
* @param {KeyboardEvent} event the keyboard event captured
* @param {string} type the keyboard event type
*/
/**
* Add a custom listener for keyboard events.
*
* @param {KeyboardCallback, Function} func function that is called on key down and key up
*/
static addKeyHandler(func) {
if (typeof func === "function") {
Input.KEY_LISTENERS.push(func);
}
}
/**
* Removes a custom key events listener.
*
* @param {KeyboardCallback, Function} func function to remove (used in 'addKeyHandler')
*/
static removeKeyHandler(func) {
if (typeof func === "function") {
const index = Input.KEY_LISTENERS.indexOf(func);
if (index > -1) Input.KEY_LISTENERS.splice(index, 1);
}
}
/** Mouse */
static MOUSE_LISTENERS = [];
static MOUSE_DOWN = false;
static MOUSE_LAST_BUTTON = null;
static MOUSE_TIMESTAMPS = {
downTime: 0,
upTime: 0,
};
static MOUSE_X = null;
static MOUSE_Y = null;
static _mouseDownListener = window.addEventListener(
"mousedown",
Input.mousedownHandler,
);
static _mouseUpListener = window.addEventListener(
"mouseup",
Input.mouseupHandler,
);
static _mouseMoveListener = window.addEventListener(
"mousemove",
Input.mousemoveHandler,
);
/**
* Handler for mouse down events.
*
* @param {MouseEvent} event called by mousedown
* @private
*/
static mousedownHandler(event) {
Input.MOUSE_DOWN = true;
Input.MOUSE_TIMESTAMPS.downTime = event.timeStamp;
Input.MOUSE_TIMESTAMPS.delta = 0;
Input.MOUSE_LAST_BUTTON = event.button;
Input.MOUSE_X = event.clientX;
Input.MOUSE_Y = event.clientY;
for (const func of Input.MOUSE_LISTENERS) func(event, "mousedown");
}
/**
* Handler for mouse up events.
*
* @param {MouseEvent} event called by mouseup
* @private
*/
static mouseupHandler(event) {
Input.MOUSE_DOWN = false;
Input.MOUSE_TIMESTAMPS.upTime = event.timeStamp;
Input.MOUSE_X = event.clientX;
Input.MOUSE_Y = event.clientY;
for (const func of Input.MOUSE_LISTENERS) func(event, "mouseup");
}
/**
* Handler for mouse move events.
*
* @param {MouseEvent} event called by mousemove
* @private
*/
static mousemoveHandler(event) {
Input.MOUSE_X = event.clientX;
Input.MOUSE_Y = event.clientY;
for (const func of Input.MOUSE_LISTENERS) func(event, "mousemove");
}
/**
* @callback MouseCallback
* @param {MouseEvent} event the mouse event captured
* @param {string} type the mouse event type
*/
/**
* Add a custom listener for mouse events.
*
* @param {MouseCallback, Function} func function that is called on mouse down, up, and move
*/
static addMouseHandler(func) {
if (typeof func === "function") {
Input.MOUSE_LISTENERS.push(func);
}
}
/**
* Removes a custom mouse events listener.
*
* @param {MouseCallback, Function} func function to remove (used in 'addMouseHandler')
*/
static removeMouseHandler(func) {
if (typeof func === "function") {
const index = Input.MOUSE_LISTENERS.indexOf(func);
if (index > -1) Input.MOUSE_LISTENERS.splice(index, 1);
}
}
/**
* Returns whether or not the mouse is pressed down.
*
* @returns true if the mouse is pressed down
*/
static isMouseDown() {
return Input.MOUSE_DOWN;
}
/**
* Returns whether or not the mouse is clicked.
*
* @param {Number} opt_bound (optional) range precision in milliseconds
* @returns true if the mouse is clicked
*/
static isMouseClicked(opt_bound) {
const delta = performance.now() - Input.MOUSE_TIMESTAMPS.downTime;
return Input.MOUSE_DOWN && delta < (opt_bound ?? 50);
}
/**
* Returns the last button pressed on the mouse (left, right, etc.)
*
* @returns Last button pressed on the mouse
*/
static getLastMouseButtonPressed() {
return Input.MOUSE_LAST_BUTTON;
}
/**
* Returns the mouse position.
*
* @param {Boolean} opt_useCenter if true, will make the returned value relative to the screen center
* @returns an array containing the mouse x and y
*/
static getMousePosition(opt_useCenter) {
if (opt_useCenter) {
return [
Input.MOUSE_X - window.innerWidth / 2,
-(Input.MOUSE_Y - window.innerHeight / 2),
];
}
return [Input.MOUSE_X, Input.MOUSE_Y];
}
}
export { Input };