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.
This commit is contained in:
photonstorm 2014-05-27 04:26:37 +01:00
parent 68f1bbd9a5
commit 890d90af4d
2 changed files with 122 additions and 17 deletions

View file

@ -63,6 +63,12 @@ Version 2.0.6 - "Jornhill" - -in development-
* BitmapData.extract has a new parameter that lets you control if the destination BitmapData is resized before the pixels are copied.
* 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.
* BitmapData.load will take a game object or string and resize the BitmapData to match it and then draw the pixels in.
* 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.

View file

@ -29,20 +29,30 @@ Phaser.Keyboard = function (game) {
this.disabled = false;
/**
* @property {Object} event - The most recent DOM event. This is updated every time a new key is pressed or released.
* @property {Object} event - The most recent DOM event from keydown or keyup. This is updated every time a new key is pressed or released.
*/
this.event = null;
/**
* @property {Object} pressEvent - The most recent DOM event from keypress.
*/
this.pressEvent = null;
/**
* @property {Object} callbackContext - The context under which the callbacks are run.
*/
this.callbackContext = this;
/**
* @property {function} onDownCallback - This callback is invoked every time a key is pressed down.
* @property {function} onDownCallback - This callback is invoked every time a key is pressed down, including key repeats when a key is held down.
*/
this.onDownCallback = null;
/**
* @property {function} onPressCallback - This callback is invoked every time a DOM onkeypress event is raised, which is only for printable keys.
*/
this.onPressCallback = null;
/**
* @property {function} onUpCallback - This callback is invoked every time a key is released.
*/
@ -67,6 +77,13 @@ Phaser.Keyboard = function (game) {
*/
this._onKeyDown = null;
/**
* @property {function} _onKeyPress
* @private
* @default
*/
this._onKeyPress = null;
/**
* @property {function} _onKeyUp
* @private
@ -80,6 +97,12 @@ Phaser.Keyboard = function (game) {
*/
this._i = 0;
/**
* @property {number} _k - Internal cache var
* @private
*/
this._k = 0;
};
Phaser.Keyboard.prototype = {
@ -89,19 +112,29 @@ Phaser.Keyboard.prototype = {
*
* @method Phaser.Keyboard#addCallbacks
* @param {Object} context - The context under which the callbacks are run.
* @param {function} onDown - This callback is invoked every time a key is pressed down.
* @param {function} [onDown=null] - This callback is invoked every time a key is pressed down.
* @param {function} [onUp=null] - This callback is invoked every time a key is released.
* @param {function} [onPress=null] - This callback is invoked every time the onkeypress event is raised.
*/
addCallbacks: function (context, onDown, onUp) {
addCallbacks: function (context, onDown, onUp, onPress) {
this.callbackContext = context;
this.onDownCallback = onDown;
if (typeof onDown !== 'undefined')
{
this.onDownCallback = onDown;
}
if (typeof onUp !== 'undefined')
{
this.onUpCallback = onUp;
}
if (typeof onPress !== 'undefined')
{
this.onPressCallback = onPress;
}
},
/**
@ -183,13 +216,18 @@ Phaser.Keyboard.prototype = {
return _this.processKeyUp(event);
};
this._onKeyPress = function (event) {
return _this.processKeyPress(event);
};
window.addEventListener('keydown', this._onKeyDown, false);
window.addEventListener('keyup', this._onKeyUp, false);
window.addEventListener('keypress', this._onKeyPress, false);
},
/**
* Stops the Keyboard event listeners from running (keydown and keyup). They are removed from the window.
* Stops the Keyboard event listeners from running (keydown, keyup and keypress). They are removed from the window.
*
* @method Phaser.Keyboard#stop
*/
@ -197,9 +235,11 @@ Phaser.Keyboard.prototype = {
window.removeEventListener('keydown', this._onKeyDown);
window.removeEventListener('keyup', this._onKeyUp);
window.removeEventListener('keypress', this._onKeyPress);
this._onKeyDown = null;
this._onKeyUp = null;
this._onKeyPress = null;
},
@ -227,7 +267,7 @@ Phaser.Keyboard.prototype = {
* Pass in either a single keycode or an array/hash of keycodes.
*
* @method Phaser.Keyboard#addKeyCapture
* @param {Any} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].
* @param {number|array|object} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].
*/
addKeyCapture: function (keycode) {
@ -308,11 +348,6 @@ Phaser.Keyboard.prototype = {
event.preventDefault();
}
if (this.onDownCallback)
{
this.onDownCallback.call(this.callbackContext, event);
}
if (!this._keys[event.keyCode])
{
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
@ -320,6 +355,31 @@ Phaser.Keyboard.prototype = {
this._keys[event.keyCode].processKeyDown(event);
this._k = event.keyCode;
if (this.onDownCallback)
{
this.onDownCallback.call(this.callbackContext, event);
}
},
/**
* Process the keypress event.
*
* @method Phaser.Keyboard#processKeyPress
* @param {KeyboardEvent} event
* @protected
*/
processKeyPress: function (event) {
this.pressEvent = event;
if (this.onPressCallback)
{
this.onPressCallback.call(this.callbackContext, event, String.fromCharCode(event.charCode));
}
},
/**
@ -343,11 +403,6 @@ Phaser.Keyboard.prototype = {
event.preventDefault();
}
if (this.onUpCallback)
{
this.onUpCallback.call(this.callbackContext, event);
}
if (!this._keys[event.keyCode])
{
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
@ -355,6 +410,11 @@ Phaser.Keyboard.prototype = {
this._keys[event.keyCode].processKeyUp(event);
if (this.onUpCallback)
{
this.onUpCallback.call(this.callbackContext, event);
}
},
/**
@ -447,6 +507,45 @@ Phaser.Keyboard.prototype = {
};
/**
* Returns the string value of the most recently pressed key.
* @name Phaser.Keyboard#lastChar
* @property {string} lastChar - The string value of the most recently pressed key.
* @readonly
*/
Object.defineProperty(Phaser.Keyboard.prototype, "lastChar", {
get: function () {
if (this.event.charCode === 32)
{
return '';
}
else
{
return String.fromCharCode(this.pressEvent.charCode);
}
}
});
/**
* Returns the most recently pressed Key. This is a Phaser.Key object and it changes every time a key is pressed.
* @name Phaser.Keyboard#lastKey
* @property {Phaser.Key} lastKey - The most recently pressed Key.
* @readonly
*/
Object.defineProperty(Phaser.Keyboard.prototype, "lastKey", {
get: function () {
return this._keys[this._k];
}
});
Phaser.Keyboard.prototype.constructor = Phaser.Keyboard;
Phaser.Keyboard.A = "A".charCodeAt(0);