forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardManager.js
More file actions
438 lines (379 loc) · 13.4 KB
/
Copy pathKeyboardManager.js
File metadata and controls
438 lines (379 loc) · 13.4 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var ArrayRemove = require('../../utils/array/Remove');
var Class = require('../../utils/Class');
var GameEvents = require('../../core/events');
var InputEvents = require('../events');
var KeyCodes = require('../../input/keyboard/keys/KeyCodes');
var NOOP = require('../../utils/NOOP');
/**
* @classdesc
* The Keyboard Manager is a helper class that belongs to the global Input Manager.
*
* Its role is to listen for native DOM Keyboard Events and then store them for further processing by the Keyboard Plugin.
*
* You do not need to create this class directly, the Input Manager will create an instance of it automatically if keyboard
* input has been enabled in the Game Config.
*
* @class KeyboardManager
* @memberof Phaser.Input.Keyboard
* @constructor
* @since 3.16.0
*
* @param {Phaser.Input.InputManager} inputManager - A reference to the Input Manager.
*/
var KeyboardManager = new Class({
initialize:
function KeyboardManager (inputManager)
{
/**
* A reference to the Input Manager.
*
* @name Phaser.Input.Keyboard.KeyboardManager#manager
* @type {Phaser.Input.InputManager}
* @since 3.16.0
*/
this.manager = inputManager;
/**
* An internal event queue.
*
* @name Phaser.Input.Keyboard.KeyboardManager#queue
* @type {KeyboardEvent[]}
* @private
* @since 3.16.0
*/
this.queue = [];
/**
* A flag that controls if the non-modified keys, matching those stored in the `captures` array,
* have `preventDefault` called on them or not.
*
* A non-modified key is one that doesn't have a modifier key held down with it. The modifier keys are
* shift, control, alt and the meta key (Command on a Mac, the Windows Key on Windows).
* Therefore, if the user presses shift + r, it won't prevent this combination, because of the modifier.
* However, if the user presses just the r key on its own, it will have its event prevented.
*
* If you wish to stop capturing the keys, for example switching out to a DOM based element, then
* you can toggle this property at run-time.
*
* @name Phaser.Input.Keyboard.KeyboardManager#preventDefault
* @type {boolean}
* @since 3.16.0
*/
this.preventDefault = true;
/**
* An array of Key Code values that will automatically have `preventDefault` called on them,
* as long as the `KeyboardManager.preventDefault` boolean is set to `true`.
*
* By default the array is empty.
*
* The key must be non-modified when pressed in order to be captured.
*
* A non-modified key is one that doesn't have a modifier key held down with it. The modifier keys are
* shift, control, alt and the meta key (Command on a Mac, the Windows Key on Windows).
* Therefore, if the user presses shift + r, it won't prevent this combination, because of the modifier.
* However, if the user presses just the r key on its own, it will have its event prevented.
*
* If you wish to stop capturing the keys, for example switching out to a DOM based element, then
* you can toggle the `KeyboardManager.preventDefault` boolean at run-time.
*
* If you need more specific control, you can create Key objects and set the flag on each of those instead.
*
* This array can be populated via the Game Config by setting the `input.keyboard.capture` array, or you
* can call the `addCapture` method. See also `removeCapture` and `clearCaptures`.
*
* @name Phaser.Input.Keyboard.KeyboardManager#captures
* @type {integer[]}
* @since 3.16.0
*/
this.captures = [];
/**
* A boolean that controls if the Keyboard Manager is enabled or not.
* Can be toggled on the fly.
*
* @name Phaser.Input.Keyboard.KeyboardManager#enabled
* @type {boolean}
* @default false
* @since 3.16.0
*/
this.enabled = false;
/**
* The Keyboard Event target, as defined in the Game Config.
* Typically the window in which the game is rendering, but can be any interactive DOM element.
*
* @name Phaser.Input.Keyboard.KeyboardManager#target
* @type {any}
* @since 3.16.0
*/
this.target;
/**
* The Key Down Event handler.
* This function is sent the native DOM KeyEvent.
* Initially empty and bound in the `startListeners` method.
*
* @name Phaser.Input.Keyboard.KeyboardManager#onKeyDown
* @type {function}
* @since 3.16.00
*/
this.onKeyDown = NOOP;
/**
* The Key Up Event handler.
* This function is sent the native DOM KeyEvent.
* Initially empty and bound in the `startListeners` method.
*
* @name Phaser.Input.Keyboard.KeyboardManager#onKeyUp
* @type {function}
* @since 3.16.00
*/
this.onKeyUp = NOOP;
inputManager.events.once(InputEvents.MANAGER_BOOT, this.boot, this);
},
/**
* The Keyboard Manager boot process.
*
* @method Phaser.Input.Keyboard.KeyboardManager#boot
* @private
* @since 3.16.0
*/
boot: function ()
{
var config = this.manager.config;
this.enabled = config.inputKeyboard;
this.target = config.inputKeyboardEventTarget;
this.addCapture(config.inputKeyboardCapture);
if (!this.target && window)
{
this.target = window;
}
if (this.enabled && this.target)
{
this.startListeners();
}
this.manager.game.events.on(GameEvents.POST_STEP, this.postUpdate, this);
},
/**
* Starts the Keyboard Event listeners running.
* This is called automatically and does not need to be manually invoked.
*
* @method Phaser.Input.Keyboard.KeyboardManager#startListeners
* @since 3.16.0
*/
startListeners: function ()
{
var _this = this;
this.onKeyDown = function (event)
{
if (event.defaultPrevented || !_this.enabled || !_this.manager)
{
// Do nothing if event already handled
return;
}
_this.queue.push(event);
_this.manager.events.emit(InputEvents.MANAGER_PROCESS);
var modified = (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey);
if (_this.preventDefault && !modified && _this.captures.indexOf(event.keyCode) > -1)
{
event.preventDefault();
}
};
this.onKeyUp = function (event)
{
if (event.defaultPrevented || !_this.enabled || !_this.manager)
{
// Do nothing if event already handled
return;
}
_this.queue.push(event);
_this.manager.events.emit(InputEvents.MANAGER_PROCESS);
var modified = (event.altKey || event.ctrlKey || event.shiftKey || event.metaKey);
if (_this.preventDefault && !modified && _this.captures.indexOf(event.keyCode) > -1)
{
event.preventDefault();
}
};
var target = this.target;
if (target)
{
target.addEventListener('keydown', this.onKeyDown, false);
target.addEventListener('keyup', this.onKeyUp, false);
this.enabled = true;
}
},
/**
* Stops the Key Event listeners.
* This is called automatically and does not need to be manually invoked.
*
* @method Phaser.Input.Keyboard.KeyboardManager#stopListeners
* @since 3.16.0
*/
stopListeners: function ()
{
var target = this.target;
target.removeEventListener('keydown', this.onKeyDown, false);
target.removeEventListener('keyup', this.onKeyUp, false);
this.enabled = false;
},
/**
* Clears the event queue.
* Called automatically by the Input Manager.
*
* @method Phaser.Input.Keyboard.KeyboardManager#postUpdate
* @private
* @since 3.16.0
*/
postUpdate: function ()
{
this.queue = [];
},
/**
* By default when a key is pressed Phaser will not stop the event from propagating up to the browser.
* There are some keys this can be annoying for, like the arrow keys or space bar, which make the browser window scroll.
*
* This `addCapture` method enables consuming keyboard event for specific keys so it doesn't bubble up to the the browser
* and cause the default browser behavior.
*
* Please note that keyboard captures are global. This means that if you call this method from within a Scene, to say prevent
* the SPACE BAR from triggering a page scroll, then it will prevent it for any Scene in your game, not just the calling one.
*
* You can pass in a single key code value, or an array of key codes, or a string:
*
* ```javascript
* this.input.keyboard.addCapture(62);
* ```
*
* An array of key codes:
*
* ```javascript
* this.input.keyboard.addCapture([ 62, 63, 64 ]);
* ```
*
* Or a string:
*
* ```javascript
* this.input.keyboard.addCapture('W,S,A,D');
* ```
*
* To use non-alpha numeric keys, use a string, such as 'UP', 'SPACE' or 'LEFT'.
*
* You can also provide an array mixing both strings and key code integers.
*
* If there are active captures after calling this method, the `preventDefault` property is set to `true`.
*
* @method Phaser.Input.Keyboard.KeyboardManager#addCapture
* @since 3.16.0
*
* @param {(string|integer|integer[]|any[])} keycode - The Key Codes to enable capture for, preventing them reaching the browser.
*/
addCapture: function (keycode)
{
if (typeof keycode === 'string')
{
keycode = keycode.split(',');
}
if (!Array.isArray(keycode))
{
keycode = [ keycode ];
}
var captures = this.captures;
for (var i = 0; i < keycode.length; i++)
{
var code = keycode[i];
if (typeof code === 'string')
{
code = KeyCodes[code.trim().toUpperCase()];
}
if (captures.indexOf(code) === -1)
{
captures.push(code);
}
}
this.preventDefault = captures.length > 0;
},
/**
* Removes an existing key capture.
*
* Please note that keyboard captures are global. This means that if you call this method from within a Scene, to remove
* the capture of a key, then it will remove it for any Scene in your game, not just the calling one.
*
* You can pass in a single key code value, or an array of key codes, or a string:
*
* ```javascript
* this.input.keyboard.removeCapture(62);
* ```
*
* An array of key codes:
*
* ```javascript
* this.input.keyboard.removeCapture([ 62, 63, 64 ]);
* ```
*
* Or a string:
*
* ```javascript
* this.input.keyboard.removeCapture('W,S,A,D');
* ```
*
* To use non-alpha numeric keys, use a string, such as 'UP', 'SPACE' or 'LEFT'.
*
* You can also provide an array mixing both strings and key code integers.
*
* If there are no captures left after calling this method, the `preventDefault` property is set to `false`.
*
* @method Phaser.Input.Keyboard.KeyboardManager#removeCapture
* @since 3.16.0
*
* @param {(string|integer|integer[]|any[])} keycode - The Key Codes to disable capture for, allowing them reaching the browser again.
*/
removeCapture: function (keycode)
{
if (typeof keycode === 'string')
{
keycode = keycode.split(',');
}
if (!Array.isArray(keycode))
{
keycode = [ keycode ];
}
var captures = this.captures;
for (var i = 0; i < keycode.length; i++)
{
var code = keycode[i];
if (typeof code === 'string')
{
code = KeyCodes[code.toUpperCase()];
}
ArrayRemove(captures, code);
}
this.preventDefault = captures.length > 0;
},
/**
* Removes all keyboard captures and sets the `preventDefault` property to `false`.
*
* @method Phaser.Input.Keyboard.KeyboardManager#clearCaptures
* @since 3.16.0
*/
clearCaptures: function ()
{
this.captures = [];
this.preventDefault = false;
},
/**
* Destroys this Keyboard Manager instance.
*
* @method Phaser.Input.Keyboard.KeyboardManager#destroy
* @since 3.16.0
*/
destroy: function ()
{
this.stopListeners();
this.clearCaptures();
this.queue = [];
this.manager.game.events.off(GameEvents.POST_RENDER, this.postUpdate, this);
this.target = null;
this.enabled = false;
this.manager = null;
}
});
module.exports = KeyboardManager;