Skip to content

Commit 890d90a

Browse files
committed
Keyboard.addCallbacks now has a new parameter for keypress event capture.
Keyboard.pressEvent stores the most recent DOM keypress event. Keyboard.processKeyDown now runs the callback after all the objects have been created and/or updated. Keyboard.processKeyUp now runs the callback after all the objects have been created and/or updated. Phaser.Keyboard.lastChar will return the string value of the last key pressed. Phaser.Keyboard.lastKey will return the most recently pressed Key object.
1 parent 68f1bbd commit 890d90a

2 files changed

Lines changed: 121 additions & 16 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ Version 2.0.6 - "Jornhill" - -in development-
6363
* BitmapData.extract has a new parameter that lets you control if the destination BitmapData is resized before the pixels are copied.
6464
* BitmapData.extract has 4 new parameters: r2, g2, b2, a2 which let you re-color the extract pixels as they are drawn to the new BitmapData.
6565
* BitmapData.load will take a game object or string and resize the BitmapData to match it and then draw the pixels in.
66+
* Keyboard.addCallbacks now has a new parameter for keypress event capture.
67+
* Keyboard.pressEvent stores the most recent DOM keypress event.
68+
* Keyboard.processKeyDown now runs the callback after all the objects have been created and/or updated.
69+
* Keyboard.processKeyUp now runs the callback after all the objects have been created and/or updated.
70+
* Phaser.Keyboard.lastChar will return the string value of the last key pressed.
71+
* Phaser.Keyboard.lastKey will return the most recently pressed Key object.
6672

6773

6874

src/input/Keyboard.js

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,30 @@ Phaser.Keyboard = function (game) {
2929
this.disabled = false;
3030

3131
/**
32-
* @property {Object} event - The most recent DOM event. This is updated every time a new key is pressed or released.
32+
* @property {Object} event - The most recent DOM event from keydown or keyup. This is updated every time a new key is pressed or released.
3333
*/
3434
this.event = null;
3535

36+
/**
37+
* @property {Object} pressEvent - The most recent DOM event from keypress.
38+
*/
39+
this.pressEvent = null;
40+
3641
/**
3742
* @property {Object} callbackContext - The context under which the callbacks are run.
3843
*/
3944
this.callbackContext = this;
4045

4146
/**
42-
* @property {function} onDownCallback - This callback is invoked every time a key is pressed down.
47+
* @property {function} onDownCallback - This callback is invoked every time a key is pressed down, including key repeats when a key is held down.
4348
*/
4449
this.onDownCallback = null;
4550

51+
/**
52+
* @property {function} onPressCallback - This callback is invoked every time a DOM onkeypress event is raised, which is only for printable keys.
53+
*/
54+
this.onPressCallback = null;
55+
4656
/**
4757
* @property {function} onUpCallback - This callback is invoked every time a key is released.
4858
*/
@@ -67,6 +77,13 @@ Phaser.Keyboard = function (game) {
6777
*/
6878
this._onKeyDown = null;
6979

80+
/**
81+
* @property {function} _onKeyPress
82+
* @private
83+
* @default
84+
*/
85+
this._onKeyPress = null;
86+
7087
/**
7188
* @property {function} _onKeyUp
7289
* @private
@@ -80,6 +97,12 @@ Phaser.Keyboard = function (game) {
8097
*/
8198
this._i = 0;
8299

100+
/**
101+
* @property {number} _k - Internal cache var
102+
* @private
103+
*/
104+
this._k = 0;
105+
83106
};
84107

85108
Phaser.Keyboard.prototype = {
@@ -89,19 +112,29 @@ Phaser.Keyboard.prototype = {
89112
*
90113
* @method Phaser.Keyboard#addCallbacks
91114
* @param {Object} context - The context under which the callbacks are run.
92-
* @param {function} onDown - This callback is invoked every time a key is pressed down.
115+
* @param {function} [onDown=null] - This callback is invoked every time a key is pressed down.
93116
* @param {function} [onUp=null] - This callback is invoked every time a key is released.
117+
* @param {function} [onPress=null] - This callback is invoked every time the onkeypress event is raised.
94118
*/
95-
addCallbacks: function (context, onDown, onUp) {
119+
addCallbacks: function (context, onDown, onUp, onPress) {
96120

97121
this.callbackContext = context;
98-
this.onDownCallback = onDown;
122+
123+
if (typeof onDown !== 'undefined')
124+
{
125+
this.onDownCallback = onDown;
126+
}
99127

100128
if (typeof onUp !== 'undefined')
101129
{
102130
this.onUpCallback = onUp;
103131
}
104132

133+
if (typeof onPress !== 'undefined')
134+
{
135+
this.onPressCallback = onPress;
136+
}
137+
105138
},
106139

107140
/**
@@ -183,23 +216,30 @@ Phaser.Keyboard.prototype = {
183216
return _this.processKeyUp(event);
184217
};
185218

219+
this._onKeyPress = function (event) {
220+
return _this.processKeyPress(event);
221+
};
222+
186223
window.addEventListener('keydown', this._onKeyDown, false);
187224
window.addEventListener('keyup', this._onKeyUp, false);
225+
window.addEventListener('keypress', this._onKeyPress, false);
188226

189227
},
190228

191229
/**
192-
* Stops the Keyboard event listeners from running (keydown and keyup). They are removed from the window.
230+
* Stops the Keyboard event listeners from running (keydown, keyup and keypress). They are removed from the window.
193231
*
194232
* @method Phaser.Keyboard#stop
195233
*/
196234
stop: function () {
197235

198236
window.removeEventListener('keydown', this._onKeyDown);
199237
window.removeEventListener('keyup', this._onKeyUp);
238+
window.removeEventListener('keypress', this._onKeyPress);
200239

201240
this._onKeyDown = null;
202241
this._onKeyUp = null;
242+
this._onKeyPress = null;
203243

204244
},
205245

@@ -227,7 +267,7 @@ Phaser.Keyboard.prototype = {
227267
* Pass in either a single keycode or an array/hash of keycodes.
228268
*
229269
* @method Phaser.Keyboard#addKeyCapture
230-
* @param {Any} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].
270+
* @param {number|array|object} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].
231271
*/
232272
addKeyCapture: function (keycode) {
233273

@@ -308,18 +348,38 @@ Phaser.Keyboard.prototype = {
308348
event.preventDefault();
309349
}
310350

351+
if (!this._keys[event.keyCode])
352+
{
353+
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
354+
}
355+
356+
this._keys[event.keyCode].processKeyDown(event);
357+
358+
this._k = event.keyCode;
359+
311360
if (this.onDownCallback)
312361
{
313362
this.onDownCallback.call(this.callbackContext, event);
314363
}
315364

316-
if (!this._keys[event.keyCode])
365+
},
366+
367+
/**
368+
* Process the keypress event.
369+
*
370+
* @method Phaser.Keyboard#processKeyPress
371+
* @param {KeyboardEvent} event
372+
* @protected
373+
*/
374+
processKeyPress: function (event) {
375+
376+
this.pressEvent = event;
377+
378+
if (this.onPressCallback)
317379
{
318-
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
380+
this.onPressCallback.call(this.callbackContext, event, String.fromCharCode(event.charCode));
319381
}
320382

321-
this._keys[event.keyCode].processKeyDown(event);
322-
323383
},
324384

325385
/**
@@ -343,18 +403,18 @@ Phaser.Keyboard.prototype = {
343403
event.preventDefault();
344404
}
345405

346-
if (this.onUpCallback)
347-
{
348-
this.onUpCallback.call(this.callbackContext, event);
349-
}
350-
351406
if (!this._keys[event.keyCode])
352407
{
353408
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
354409
}
355410

356411
this._keys[event.keyCode].processKeyUp(event);
357412

413+
if (this.onUpCallback)
414+
{
415+
this.onUpCallback.call(this.callbackContext, event);
416+
}
417+
358418
},
359419

360420
/**
@@ -447,6 +507,45 @@ Phaser.Keyboard.prototype = {
447507

448508
};
449509

510+
/**
511+
* Returns the string value of the most recently pressed key.
512+
* @name Phaser.Keyboard#lastChar
513+
* @property {string} lastChar - The string value of the most recently pressed key.
514+
* @readonly
515+
*/
516+
Object.defineProperty(Phaser.Keyboard.prototype, "lastChar", {
517+
518+
get: function () {
519+
520+
if (this.event.charCode === 32)
521+
{
522+
return '';
523+
}
524+
else
525+
{
526+
return String.fromCharCode(this.pressEvent.charCode);
527+
}
528+
529+
}
530+
531+
});
532+
533+
/**
534+
* Returns the most recently pressed Key. This is a Phaser.Key object and it changes every time a key is pressed.
535+
* @name Phaser.Keyboard#lastKey
536+
* @property {Phaser.Key} lastKey - The most recently pressed Key.
537+
* @readonly
538+
*/
539+
Object.defineProperty(Phaser.Keyboard.prototype, "lastKey", {
540+
541+
get: function () {
542+
543+
return this._keys[this._k];
544+
545+
}
546+
547+
});
548+
450549
Phaser.Keyboard.prototype.constructor = Phaser.Keyboard;
451550

452551
Phaser.Keyboard.A = "A".charCodeAt(0);

0 commit comments

Comments
 (0)