Skip to content

Commit 9eec666

Browse files
committed
Keyboard Manager updates
* KeyboardManager.handler has been removed as it's no longer used internally. * The KeyboardManager.captures property has been removed as it can be more effectively handled by polling the `keys` object instead. * The Keyboard Manager will no longer process key down or up events if its `enabled` property is set to false. * The Keyboard Manager will now call `event.preventDefault` on the native DOM event as long as the Key exists in the keys array and has its `preventDefault` property set to `true` (which is the default). This means you can now control specifically which key prevents default on the browser, where-as before every key added did so.
1 parent f280fb1 commit 9eec666

1 file changed

Lines changed: 99 additions & 58 deletions

File tree

src/input/keyboard/KeyboardManager.js

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,44 @@ var ProcessKeyUp = require('./keys/ProcessKeyUp');
2121

2222
/**
2323
* @classdesc
24-
* The Keyboard class monitors keyboard input and dispatches keyboard events.
24+
* The Keyboard Manager is a helper class that belongs to the Input Manager.
25+
*
26+
* Its role is to listen for native DOM Keyboard Events and then process them.
27+
*
28+
* You do not need to create this class directly, the Input Manager will create an instance of it automatically.
29+
*
30+
* You can access it from within a Scene using `this.input.keyboard`. For example, you can do:
2531
*
26-
* _Note_: many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
32+
* ```javascript
33+
* this.input.keyboard.on('keydown', callback, context);
34+
* ```
35+
*
36+
* Or, to listen for a specific key:
37+
*
38+
* ```javascript
39+
* this.input.keyboard.on('keydown_A', callback, context);
40+
* ```
41+
*
42+
* You can also create Key objects, which you can then poll in your game loop:
43+
*
44+
* ```javascript
45+
* var spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
46+
* ```
47+
*
48+
* _Note_: Many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
2749
* See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
2850
*
2951
* Also please be aware that certain browser extensions can disable or override Phaser keyboard handling.
30-
* For example the Chrome extension vimium is known to disable Phaser from using the D key. And there are others.
31-
* So please check your extensions before opening Phaser issues.
52+
* For example the Chrome extension vimium is known to disable Phaser from using the D key, while EverNote disables the backtick key.
53+
* And there are others. So, please check your extensions before opening Phaser issues about keys that don't work.
3254
*
3355
* @class KeyboardManager
3456
* @extends Phaser.Events.EventEmitter
3557
* @memberOf Phaser.Input.Keyboard
3658
* @constructor
3759
* @since 3.0.0
3860
*
39-
* @param {Phaser.Input.InputManager} inputManager - [description]
61+
* @param {Phaser.Input.InputManager} inputManager - A reference to the Input Manager.
4062
*/
4163
var KeyboardManager = new Class({
4264

@@ -49,7 +71,7 @@ var KeyboardManager = new Class({
4971
EventEmitter.call(this);
5072

5173
/**
52-
* [description]
74+
* A reference to the Input Manager.
5375
*
5476
* @name Phaser.Input.Keyboard.KeyboardManager#manager
5577
* @type {Phaser.Input.InputManager}
@@ -58,7 +80,8 @@ var KeyboardManager = new Class({
5880
this.manager = inputManager;
5981

6082
/**
61-
* [description]
83+
* A boolean that controls if the Keyboard Manager is enabled or not.
84+
* Can be toggled on the fly.
6285
*
6386
* @name Phaser.Input.Keyboard.KeyboardManager#enabled
6487
* @type {boolean}
@@ -68,70 +91,51 @@ var KeyboardManager = new Class({
6891
this.enabled = false;
6992

7093
/**
71-
* [description]
94+
* The Keyboard Event target, as defined in the Game Config.
95+
* Typically the browser window, but can be any interactive DOM element.
7296
*
7397
* @name Phaser.Input.Keyboard.KeyboardManager#target
74-
* @type {?object}
98+
* @type {any}
7599
* @since 3.0.0
76100
*/
77101
this.target;
78102

79103
/**
80-
* [description]
104+
* An array of Key objects to process.
81105
*
82106
* @name Phaser.Input.Keyboard.KeyboardManager#keys
83107
* @type {Phaser.Input.Keyboard.Key[]}
84-
* @default []
85108
* @since 3.0.0
86109
*/
87110
this.keys = [];
88111

89112
/**
90-
* [description]
113+
* An array of KeyCombo objects to process.
91114
*
92115
* @name Phaser.Input.Keyboard.KeyboardManager#combos
93116
* @type {Phaser.Input.Keyboard.KeyCombo[]}
94-
* @default []
95117
* @since 3.0.0
96118
*/
97119
this.combos = [];
98120

99-
/**
100-
* [description]
101-
*
102-
* @name Phaser.Input.Keyboard.KeyboardManager#captures
103-
* @type {array}
104-
* @default []
105-
* @since 3.0.0
106-
*/
107-
this.captures = [];
108-
109121
/**
110122
* [description]
111123
*
112124
* @name Phaser.Input.Keyboard.KeyboardManager#queue
113125
* @type {KeyboardEvent[]}
114-
* @default []
126+
* @private
115127
* @since 3.0.0
116128
*/
117129
this.queue = [];
118130

119-
/**
120-
* [description]
121-
*
122-
* @name Phaser.Input.Keyboard.KeyboardManager#handler
123-
* @type {?KeyboardHandler}
124-
* @since 3.0.0
125-
*/
126-
this.handler;
127-
128131
inputManager.events.once('boot', this.boot, this);
129132
},
130133

131134
/**
132135
* The Boot handler is called by Phaser.Game when it first starts up.
133136
*
134137
* @method Phaser.Input.Keyboard.KeyboardManager#boot
138+
* @private
135139
* @since 3.0.0
136140
*/
137141
boot: function ()
@@ -148,36 +152,67 @@ var KeyboardManager = new Class({
148152
},
149153

150154
/**
151-
* [description]
155+
* The Keyboard Down Event Handler.
152156
*
153-
* @method Phaser.Input.Keyboard.KeyboardManager#startListeners
154-
* @since 3.0.0
157+
* @method Phaser.Input.Keyboard.KeyboardManager#onKeyDown
158+
* @since 3.10.0
159+
*
160+
* @param {KeyboardEvent} event - The native DOM Keyboard Event.
155161
*/
156-
startListeners: function ()
162+
onKeyDown: function (event)
157163
{
158-
var queue = this.queue;
159-
var captures = this.captures;
164+
if (event.defaultPrevented || !this.enabled)
165+
{
166+
// Do nothing if event already handled
167+
return;
168+
}
169+
170+
this.queue.push(event);
160171

161-
var handler = function (event)
172+
var key = this.keys[event.keyCode];
173+
174+
if (key && key.preventDefault)
162175
{
163-
if (event.defaultPrevented)
164-
{
165-
// Do nothing if event already handled
166-
return;
167-
}
176+
event.preventDefault();
177+
}
178+
},
168179

169-
queue.push(event);
180+
/**
181+
* The Keyboard Up Event Handler.
182+
*
183+
* @method Phaser.Input.Keyboard.KeyboardManager#onKeyUp
184+
* @since 3.10.0
185+
*
186+
* @param {KeyboardEvent} event - The native DOM Keyboard Event.
187+
*/
188+
onKeyUp: function (event)
189+
{
190+
if (event.defaultPrevented || !this.enabled)
191+
{
192+
// Do nothing if event already handled
193+
return;
194+
}
170195

171-
if (captures[event.keyCode])
172-
{
173-
event.preventDefault();
174-
}
175-
};
196+
this.queue.push(event);
176197

177-
this.handler = handler;
198+
var key = this.keys[event.keyCode];
178199

179-
this.target.addEventListener('keydown', handler, false);
180-
this.target.addEventListener('keyup', handler, false);
200+
if (key && key.preventDefault)
201+
{
202+
event.preventDefault();
203+
}
204+
},
205+
206+
/**
207+
* [description]
208+
*
209+
* @method Phaser.Input.Keyboard.KeyboardManager#startListeners
210+
* @since 3.0.0
211+
*/
212+
startListeners: function ()
213+
{
214+
this.target.addEventListener('keydown', this.onKeyDown.bind(this), false);
215+
this.target.addEventListener('keyup', this.onKeyUp.bind(this), false);
181216

182217
// Finally, listen for an update event from the Input Manager
183218
this.manager.events.on('update', this.update, this);
@@ -191,8 +226,8 @@ var KeyboardManager = new Class({
191226
*/
192227
stopListeners: function ()
193228
{
194-
this.target.removeEventListener('keydown', this.handler);
195-
this.target.removeEventListener('keyup', this.handler);
229+
this.target.removeEventListener('keydown', this.onKeyDown);
230+
this.target.removeEventListener('keyup', this.onKeyUp);
196231

197232
this.manager.events.off('update', this.update);
198233
},
@@ -271,10 +306,16 @@ var KeyboardManager = new Class({
271306
{
272307
var keys = this.keys;
273308

309+
if (typeof keyCode === 'string')
310+
{
311+
keyCode = keyCodes[keyCode.toUpperCase()];
312+
}
313+
274314
if (!keys[keyCode])
275315
{
276316
keys[keyCode] = new Key(keyCode);
277-
this.captures[keyCode] = true;
317+
318+
// this.captures[keyCode] = true;
278319
}
279320

280321
return keys[keyCode];
@@ -293,7 +334,7 @@ var KeyboardManager = new Class({
293334
if (this.keys[keyCode])
294335
{
295336
this.keys[keyCode] = undefined;
296-
this.captures[keyCode] = false;
337+
// this.captures[keyCode] = false;
297338
}
298339
},
299340

0 commit comments

Comments
 (0)