2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2015-02-25 03:36:23 +00:00
|
|
|
* @copyright 2015 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-10-27 00:25:03 +00:00
|
|
|
* The Keyboard class monitors keyboard input and dispatches keyboard events.
|
|
|
|
*
|
|
|
|
* _Be aware_ that many keyboards are unable to process certain combinations of keys due to hardware
|
2014-04-20 00:57:23 +00:00
|
|
|
* limitations known as ghosting. Full details here: http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/
|
2013-10-01 12:54:29 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Keyboard
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.Keyboard = function (game) {
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
|
|
*/
|
|
|
|
this.game = game;
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
2014-10-27 00:25:03 +00:00
|
|
|
* Keyboard input will only be processed if enabled.
|
2014-10-29 04:46:33 +00:00
|
|
|
* @property {boolean} enabled
|
2013-10-02 12:18:58 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2014-10-27 00:25:03 +00:00
|
|
|
this.enabled = true;
|
2013-10-02 12:18:58 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-30 11:10:52 +00:00
|
|
|
* @property {object} event - The most recent DOM event from keydown or keyup. This is updated every time a new key is pressed or released.
|
2013-10-02 12:18:58 +00:00
|
|
|
*/
|
2014-03-03 11:18:56 +00:00
|
|
|
this.event = null;
|
2013-10-02 12:18:58 +00:00
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
/**
|
2014-11-30 11:10:52 +00:00
|
|
|
* @property {object} pressEvent - The most recent DOM event from keypress.
|
2014-05-27 03:26:37 +00:00
|
|
|
*/
|
|
|
|
this.pressEvent = null;
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
2014-11-30 11:10:52 +00:00
|
|
|
* @property {object} callbackContext - The context under which the callbacks are run.
|
2013-10-02 12:18:58 +00:00
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
this.callbackContext = this;
|
2013-10-02 12:18:58 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-27 03:26:37 +00:00
|
|
|
* @property {function} onDownCallback - This callback is invoked every time a key is pressed down, including key repeats when a key is held down.
|
2013-10-02 12:18:58 +00:00
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
this.onDownCallback = null;
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
/**
|
|
|
|
* @property {function} onPressCallback - This callback is invoked every time a DOM onkeypress event is raised, which is only for printable keys.
|
|
|
|
*/
|
|
|
|
this.onPressCallback = null;
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
|
|
|
* @property {function} onUpCallback - This callback is invoked every time a key is released.
|
|
|
|
*/
|
|
|
|
this.onUpCallback = null;
|
2014-03-03 11:18:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array<Phaser.Key>} _keys - The array the Phaser.Key objects are stored in.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._keys = [];
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-03 11:18:56 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _capture - The array the key capture values are stored in.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._capture = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {function} _onKeyDown
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._onKeyDown = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
/**
|
|
|
|
* @property {function} _onKeyPress
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._onKeyPress = null;
|
|
|
|
|
2014-03-03 11:18:56 +00:00
|
|
|
/**
|
|
|
|
* @property {function} _onKeyUp
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._onKeyUp = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-28 01:42:49 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _i - Internal cache var
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._i = 0;
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _k - Internal cache var
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._k = 0;
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
};
|
|
|
|
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.Keyboard.prototype = {
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-25 02:59:24 +00:00
|
|
|
* Add callbacks to the Keyboard handler so that each time a key is pressed down or released the callbacks are activated.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-10-02 12:18:58 +00:00
|
|
|
* @method Phaser.Keyboard#addCallbacks
|
2014-11-30 11:10:52 +00:00
|
|
|
* @param {object} context - The context under which the callbacks are run.
|
2014-05-27 03:26:37 +00:00
|
|
|
* @param {function} [onDown=null] - This callback is invoked every time a key is pressed down.
|
2013-10-02 12:18:58 +00:00
|
|
|
* @param {function} [onUp=null] - This callback is invoked every time a key is released.
|
2014-05-27 03:26:37 +00:00
|
|
|
* @param {function} [onPress=null] - This callback is invoked every time the onkeypress event is raised.
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
2014-05-27 03:26:37 +00:00
|
|
|
addCallbacks: function (context, onDown, onUp, onPress) {
|
2013-10-01 15:15:45 +00:00
|
|
|
|
|
|
|
this.callbackContext = context;
|
2014-05-27 03:26:37 +00:00
|
|
|
|
2015-09-23 22:18:36 +00:00
|
|
|
if (onDown !== undefined && onDown !== null)
|
2014-05-27 03:26:37 +00:00
|
|
|
{
|
|
|
|
this.onDownCallback = onDown;
|
|
|
|
}
|
2013-10-01 15:15:45 +00:00
|
|
|
|
2015-09-23 22:18:36 +00:00
|
|
|
if (onUp !== undefined && onUp !== null)
|
2013-10-01 15:15:45 +00:00
|
|
|
{
|
|
|
|
this.onUpCallback = onUp;
|
|
|
|
}
|
|
|
|
|
2015-09-23 22:18:36 +00:00
|
|
|
if (onPress !== undefined && onPress !== null)
|
2014-05-27 03:26:37 +00:00
|
|
|
{
|
|
|
|
this.onPressCallback = onPress;
|
|
|
|
}
|
|
|
|
|
2013-10-01 15:15:45 +00:00
|
|
|
},
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
|
|
|
* If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
|
|
|
|
* The Key object can then be polled, have events attached to it, etc.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#addKey
|
2014-01-09 01:23:23 +00:00
|
|
|
* @param {number} keycode - The keycode of the key, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
2013-10-02 12:18:58 +00:00
|
|
|
* @return {Phaser.Key} The Key object which you can store locally and reference directly.
|
|
|
|
*/
|
2013-10-01 15:15:45 +00:00
|
|
|
addKey: function (keycode) {
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (!this._keys[keycode])
|
|
|
|
{
|
|
|
|
this._keys[keycode] = new Phaser.Key(this.game, keycode);
|
2013-10-01 15:15:45 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
this.addKeyCapture(keycode);
|
|
|
|
}
|
2013-10-01 15:15:45 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
return this._keys[keycode];
|
2013-10-01 15:15:45 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-06-14 20:35:38 +00:00
|
|
|
/**
|
|
|
|
* A practical way to create an object containing user selected hotkeys.
|
|
|
|
*
|
2015-07-09 13:28:58 +00:00
|
|
|
* For example: `addKeys( { 'up': Phaser.Keyboard.W, 'down': Phaser.Keyboard.S, 'left': Phaser.Keyboard.A, 'right': Phaser.Keyboard.D } );`
|
2015-07-02 13:15:06 +00:00
|
|
|
*
|
|
|
|
* Would return an object containing the properties `up`, `down`, `left` and `right` that you could poll just like a Phaser.Key object.
|
|
|
|
*
|
2015-06-14 20:35:38 +00:00
|
|
|
* @method Phaser.Keyboard#addKeys
|
2015-07-09 13:28:58 +00:00
|
|
|
* @param {object} keys - A key mapping object, i.e. `{ 'up': Phaser.Keyboard.W, 'down': Phaser.Keyboard.S }` or `{ 'up': 52, 'down': 53 }`.
|
2015-06-14 20:35:38 +00:00
|
|
|
* @return {object} An object containing user selected properties
|
|
|
|
*/
|
2015-07-09 13:28:58 +00:00
|
|
|
addKeys: function (keys) {
|
2015-06-14 20:35:38 +00:00
|
|
|
|
2015-07-02 13:15:06 +00:00
|
|
|
var output = {};
|
|
|
|
|
2015-07-09 13:28:58 +00:00
|
|
|
for (var key in keys)
|
2015-07-02 13:15:06 +00:00
|
|
|
{
|
2015-07-09 13:28:58 +00:00
|
|
|
output[key] = this.addKey(keys[key]);
|
2015-07-02 13:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
},
|
2015-06-14 20:35:38 +00:00
|
|
|
|
2014-03-12 18:29:32 +00:00
|
|
|
/**
|
|
|
|
* Removes a Key object from the Keyboard manager.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#removeKey
|
|
|
|
* @param {number} keycode - The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
|
|
|
*/
|
|
|
|
removeKey: function (keycode) {
|
|
|
|
|
|
|
|
if (this._keys[keycode])
|
|
|
|
{
|
2014-03-13 07:29:23 +00:00
|
|
|
this._keys[keycode] = null;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-12 18:29:32 +00:00
|
|
|
this.removeKeyCapture(keycode);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-03 22:20:24 +00:00
|
|
|
/**
|
|
|
|
* Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#createCursorKeys
|
|
|
|
* @return {object} An object containing properties: up, down, left and right. Which can be polled like any other Phaser.Key object.
|
|
|
|
*/
|
|
|
|
createCursorKeys: function () {
|
|
|
|
|
2015-07-09 13:28:58 +00:00
|
|
|
return this.addKeys({ 'up': Phaser.Keyboard.UP, 'down': Phaser.Keyboard.DOWN, 'left': Phaser.Keyboard.LEFT, 'right': Phaser.Keyboard.RIGHT });
|
2013-10-03 22:20:24 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
2014-03-28 01:42:49 +00:00
|
|
|
* Starts the Keyboard event listeners running (keydown and keyup). They are attached to the window.
|
2013-10-02 12:18:58 +00:00
|
|
|
* This is called automatically by Phaser.Input and should not normally be invoked directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#start
|
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
start: function () {
|
|
|
|
|
2014-05-29 21:23:33 +00:00
|
|
|
if (this.game.device.cocoonJS)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-19 13:22:04 +00:00
|
|
|
if (this._onKeyDown !== null)
|
|
|
|
{
|
|
|
|
// Avoid setting multiple listeners
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
this._onKeyDown = function (event) {
|
2013-10-01 00:18:29 +00:00
|
|
|
return _this.processKeyDown(event);
|
2013-08-31 12:54:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this._onKeyUp = function (event) {
|
2013-10-01 00:18:29 +00:00
|
|
|
return _this.processKeyUp(event);
|
2013-08-31 12:54:59 +00:00
|
|
|
};
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
this._onKeyPress = function (event) {
|
|
|
|
return _this.processKeyPress(event);
|
|
|
|
};
|
|
|
|
|
2013-11-25 13:12:03 +00:00
|
|
|
window.addEventListener('keydown', this._onKeyDown, false);
|
|
|
|
window.addEventListener('keyup', this._onKeyUp, false);
|
2014-05-27 03:26:37 +00:00
|
|
|
window.addEventListener('keypress', this._onKeyPress, false);
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
2014-05-27 03:26:37 +00:00
|
|
|
* Stops the Keyboard event listeners from running (keydown, keyup and keypress). They are removed from the window.
|
2013-10-02 12:18:58 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#stop
|
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
stop: function () {
|
|
|
|
|
2013-11-25 13:12:03 +00:00
|
|
|
window.removeEventListener('keydown', this._onKeyDown);
|
|
|
|
window.removeEventListener('keyup', this._onKeyUp);
|
2014-05-27 03:26:37 +00:00
|
|
|
window.removeEventListener('keypress', this._onKeyPress);
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2014-04-07 11:01:51 +00:00
|
|
|
this._onKeyDown = null;
|
|
|
|
this._onKeyUp = null;
|
2014-05-27 03:26:37 +00:00
|
|
|
this._onKeyPress = null;
|
2014-04-07 11:01:51 +00:00
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
},
|
|
|
|
|
2014-03-28 01:42:49 +00:00
|
|
|
/**
|
|
|
|
* Stops the Keyboard event listeners from running (keydown and keyup). They are removed from the window.
|
|
|
|
* Also clears all key captures and currently created Key objects.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#destroy
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this.stop();
|
|
|
|
|
|
|
|
this.clearCaptures();
|
|
|
|
|
|
|
|
this._keys.length = 0;
|
|
|
|
this._i = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2013-08-31 12:54:59 +00:00
|
|
|
* 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.
|
|
|
|
* You can use addKeyCapture to consume the keyboard event for specific keys so it doesn't bubble up to the the browser.
|
|
|
|
* Pass in either a single keycode or an array/hash of keycodes.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-10-02 12:18:58 +00:00
|
|
|
* @method Phaser.Keyboard#addKeyCapture
|
2014-05-27 03:26:37 +00:00
|
|
|
* @param {number|array|object} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
|
|
|
addKeyCapture: function (keycode) {
|
|
|
|
|
|
|
|
if (typeof keycode === 'object')
|
|
|
|
{
|
|
|
|
for (var key in keycode)
|
|
|
|
{
|
|
|
|
this._capture[keycode[key]] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._capture[keycode] = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Removes an existing key capture.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.Keyboard#removeKeyCapture
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {number} keycode
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
|
|
|
removeKeyCapture: function (keycode) {
|
|
|
|
|
|
|
|
delete this._capture[keycode];
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Clear all set key captures.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.Keyboard#clearCaptures
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-31 12:54:59 +00:00
|
|
|
clearCaptures: function () {
|
|
|
|
|
|
|
|
this._capture = {};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
/**
|
|
|
|
* Updates all currently defined keys.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#update
|
|
|
|
*/
|
|
|
|
update: function () {
|
|
|
|
|
2014-03-28 01:42:49 +00:00
|
|
|
this._i = this._keys.length;
|
2014-02-24 15:58:02 +00:00
|
|
|
|
2014-03-28 01:42:49 +00:00
|
|
|
while (this._i--)
|
2014-02-24 15:58:02 +00:00
|
|
|
{
|
2014-03-28 01:42:49 +00:00
|
|
|
if (this._keys[this._i])
|
2014-02-24 15:58:02 +00:00
|
|
|
{
|
2014-03-28 01:42:49 +00:00
|
|
|
this._keys[this._i].update();
|
2014-02-24 15:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Process the keydown event.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.Keyboard#processKeyDown
|
2013-08-31 12:54:59 +00:00
|
|
|
* @param {KeyboardEvent} event
|
2013-10-02 12:18:58 +00:00
|
|
|
* @protected
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
processKeyDown: function (event) {
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2014-03-03 11:18:56 +00:00
|
|
|
this.event = event;
|
|
|
|
|
2014-10-27 00:25:03 +00:00
|
|
|
if (!this.game.input.enabled || !this.enabled)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
// The event is being captured but another hotkey may need it
|
2013-08-31 12:54:59 +00:00
|
|
|
if (this._capture[event.keyCode])
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
if (!this._keys[event.keyCode])
|
|
|
|
{
|
|
|
|
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._keys[event.keyCode].processKeyDown(event);
|
|
|
|
|
|
|
|
this._k = event.keyCode;
|
|
|
|
|
2013-10-01 00:18:29 +00:00
|
|
|
if (this.onDownCallback)
|
|
|
|
{
|
|
|
|
this.onDownCallback.call(this.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process the keypress event.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#processKeyPress
|
|
|
|
* @param {KeyboardEvent} event
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
processKeyPress: function (event) {
|
|
|
|
|
|
|
|
this.pressEvent = event;
|
|
|
|
|
2014-10-27 00:25:03 +00:00
|
|
|
if (!this.game.input.enabled || !this.enabled)
|
2014-07-15 23:12:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
if (this.onPressCallback)
|
2013-10-01 00:18:29 +00:00
|
|
|
{
|
2014-05-27 10:32:18 +00:00
|
|
|
this.onPressCallback.call(this.callbackContext, String.fromCharCode(event.charCode), event);
|
2013-08-31 12:54:59 +00:00
|
|
|
}
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* Process the keyup event.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.Keyboard#processKeyUp
|
2013-08-31 12:54:59 +00:00
|
|
|
* @param {KeyboardEvent} event
|
2013-10-02 12:18:58 +00:00
|
|
|
* @protected
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
2013-10-01 00:18:29 +00:00
|
|
|
processKeyUp: function (event) {
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2014-03-03 11:18:56 +00:00
|
|
|
this.event = event;
|
|
|
|
|
2014-10-27 00:25:03 +00:00
|
|
|
if (!this.game.input.enabled || !this.enabled)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._capture[event.keyCode])
|
|
|
|
{
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (!this._keys[event.keyCode])
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
2014-02-24 15:58:02 +00:00
|
|
|
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
|
2013-08-31 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
this._keys[event.keyCode].processKeyUp(event);
|
2013-10-01 00:18:29 +00:00
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
if (this.onUpCallback)
|
|
|
|
{
|
|
|
|
this.onUpCallback.call(this.callbackContext, event);
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:54:59 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
2014-02-24 15:58:02 +00:00
|
|
|
* Resets all Keys.
|
|
|
|
*
|
2013-11-25 03:13:04 +00:00
|
|
|
* @method Phaser.Keyboard#reset
|
2014-04-14 20:53:08 +00:00
|
|
|
* @param {boolean} [hard=true] - A soft reset won't reset any events or callbacks that are bound to the Keys. A hard reset will.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2014-04-14 20:53:08 +00:00
|
|
|
reset: function (hard) {
|
|
|
|
|
2015-07-22 09:37:15 +00:00
|
|
|
if (hard === undefined) { hard = true; }
|
2013-08-31 12:54:59 +00:00
|
|
|
|
2014-03-03 11:18:56 +00:00
|
|
|
this.event = null;
|
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
var i = this._keys.length;
|
|
|
|
|
|
|
|
while (i--)
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
2014-02-24 15:58:02 +00:00
|
|
|
if (this._keys[i])
|
|
|
|
{
|
2014-04-14 20:53:08 +00:00
|
|
|
this._keys[i].reset(hard);
|
2014-02-24 15:58:02 +00:00
|
|
|
}
|
2013-08-31 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-02 12:18:58 +00:00
|
|
|
/**
|
2014-11-20 20:55:51 +00:00
|
|
|
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` if it either isn't down,
|
|
|
|
* or was pressed down longer ago than then given duration.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#downDuration
|
|
|
|
* @param {number} keycode - The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
|
|
|
* @param {number} [duration=50] - The duration within which the key is considered as being just pressed. Given in ms.
|
|
|
|
* @return {boolean} True if the key was pressed down within the given duration, false if not or null if the Key wasn't found.
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
2014-11-20 21:19:09 +00:00
|
|
|
downDuration: function (keycode, duration) {
|
2014-05-13 23:22:08 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (this._keys[keycode])
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
2014-11-20 19:23:43 +00:00
|
|
|
return this._keys[keycode].downDuration(duration);
|
2014-02-24 15:58:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-20 20:55:51 +00:00
|
|
|
return null;
|
2013-08-31 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-20 20:55:51 +00:00
|
|
|
* Returns `true` if the Key was pressed down within the `duration` value given, or `false` if it either isn't down,
|
|
|
|
* or was pressed down longer ago than then given duration.
|
|
|
|
*
|
|
|
|
* @method Phaser.Keyboard#upDuration
|
|
|
|
* @param {number} keycode - The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
|
|
|
* @param {number} [duration=50] - The duration within which the key is considered as being just released. Given in ms.
|
|
|
|
* @return {boolean} True if the key was released within the given duration, false if not or null if the Key wasn't found.
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
2014-11-20 20:55:51 +00:00
|
|
|
upDuration: function (keycode, duration) {
|
2014-05-13 23:22:08 +00:00
|
|
|
|
2014-02-24 15:58:02 +00:00
|
|
|
if (this._keys[keycode])
|
2013-08-31 12:54:59 +00:00
|
|
|
{
|
2014-11-20 19:23:43 +00:00
|
|
|
return this._keys[keycode].upDuration(duration);
|
2014-02-24 15:58:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-20 20:55:51 +00:00
|
|
|
return null;
|
2013-08-31 12:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 12:18:58 +00:00
|
|
|
* Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser.
|
2014-02-24 15:58:02 +00:00
|
|
|
*
|
2013-10-02 12:18:58 +00:00
|
|
|
* @method Phaser.Keyboard#isDown
|
2014-11-20 20:55:51 +00:00
|
|
|
* @param {number} keycode - The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
|
|
|
|
* @return {boolean} True if the key is currently down, false if not or null if the Key wasn't found.
|
2013-08-31 12:54:59 +00:00
|
|
|
*/
|
|
|
|
isDown: function (keycode) {
|
|
|
|
|
|
|
|
if (this._keys[keycode])
|
|
|
|
{
|
|
|
|
return this._keys[keycode].isDown;
|
|
|
|
}
|
2014-11-20 20:55:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-08-31 12:54:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-05-27 03:26:37 +00:00
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.Keyboard.prototype.constructor = Phaser.Keyboard;
|
|
|
|
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.Keyboard.A = "A".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.B = "B".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.C = "C".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.D = "D".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.E = "E".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.F = "F".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.G = "G".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.H = "H".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.I = "I".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.J = "J".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.K = "K".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.L = "L".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.M = "M".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.N = "N".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.O = "O".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.P = "P".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.Q = "Q".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.R = "R".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.S = "S".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.T = "T".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.U = "U".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.V = "V".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.W = "W".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.X = "X".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.Y = "Y".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.Z = "Z".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.ZERO = "0".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.ONE = "1".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.TWO = "2".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.THREE = "3".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.FOUR = "4".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.FIVE = "5".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.SIX = "6".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.SEVEN = "7".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.EIGHT = "8".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.NINE = "9".charCodeAt(0);
|
|
|
|
Phaser.Keyboard.NUMPAD_0 = 96;
|
|
|
|
Phaser.Keyboard.NUMPAD_1 = 97;
|
|
|
|
Phaser.Keyboard.NUMPAD_2 = 98;
|
|
|
|
Phaser.Keyboard.NUMPAD_3 = 99;
|
|
|
|
Phaser.Keyboard.NUMPAD_4 = 100;
|
|
|
|
Phaser.Keyboard.NUMPAD_5 = 101;
|
|
|
|
Phaser.Keyboard.NUMPAD_6 = 102;
|
|
|
|
Phaser.Keyboard.NUMPAD_7 = 103;
|
|
|
|
Phaser.Keyboard.NUMPAD_8 = 104;
|
|
|
|
Phaser.Keyboard.NUMPAD_9 = 105;
|
|
|
|
Phaser.Keyboard.NUMPAD_MULTIPLY = 106;
|
|
|
|
Phaser.Keyboard.NUMPAD_ADD = 107;
|
|
|
|
Phaser.Keyboard.NUMPAD_ENTER = 108;
|
|
|
|
Phaser.Keyboard.NUMPAD_SUBTRACT = 109;
|
|
|
|
Phaser.Keyboard.NUMPAD_DECIMAL = 110;
|
|
|
|
Phaser.Keyboard.NUMPAD_DIVIDE = 111;
|
|
|
|
Phaser.Keyboard.F1 = 112;
|
|
|
|
Phaser.Keyboard.F2 = 113;
|
|
|
|
Phaser.Keyboard.F3 = 114;
|
|
|
|
Phaser.Keyboard.F4 = 115;
|
|
|
|
Phaser.Keyboard.F5 = 116;
|
|
|
|
Phaser.Keyboard.F6 = 117;
|
|
|
|
Phaser.Keyboard.F7 = 118;
|
|
|
|
Phaser.Keyboard.F8 = 119;
|
|
|
|
Phaser.Keyboard.F9 = 120;
|
|
|
|
Phaser.Keyboard.F10 = 121;
|
|
|
|
Phaser.Keyboard.F11 = 122;
|
|
|
|
Phaser.Keyboard.F12 = 123;
|
|
|
|
Phaser.Keyboard.F13 = 124;
|
|
|
|
Phaser.Keyboard.F14 = 125;
|
|
|
|
Phaser.Keyboard.F15 = 126;
|
|
|
|
Phaser.Keyboard.COLON = 186;
|
|
|
|
Phaser.Keyboard.EQUALS = 187;
|
2015-07-09 13:28:58 +00:00
|
|
|
Phaser.Keyboard.COMMA = 188;
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.Keyboard.UNDERSCORE = 189;
|
2015-07-09 13:28:58 +00:00
|
|
|
Phaser.Keyboard.PERIOD = 190;
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.Keyboard.QUESTION_MARK = 191;
|
|
|
|
Phaser.Keyboard.TILDE = 192;
|
|
|
|
Phaser.Keyboard.OPEN_BRACKET = 219;
|
|
|
|
Phaser.Keyboard.BACKWARD_SLASH = 220;
|
|
|
|
Phaser.Keyboard.CLOSED_BRACKET = 221;
|
|
|
|
Phaser.Keyboard.QUOTES = 222;
|
|
|
|
Phaser.Keyboard.BACKSPACE = 8;
|
|
|
|
Phaser.Keyboard.TAB = 9;
|
|
|
|
Phaser.Keyboard.CLEAR = 12;
|
|
|
|
Phaser.Keyboard.ENTER = 13;
|
|
|
|
Phaser.Keyboard.SHIFT = 16;
|
|
|
|
Phaser.Keyboard.CONTROL = 17;
|
|
|
|
Phaser.Keyboard.ALT = 18;
|
|
|
|
Phaser.Keyboard.CAPS_LOCK = 20;
|
|
|
|
Phaser.Keyboard.ESC = 27;
|
|
|
|
Phaser.Keyboard.SPACEBAR = 32;
|
|
|
|
Phaser.Keyboard.PAGE_UP = 33;
|
|
|
|
Phaser.Keyboard.PAGE_DOWN = 34;
|
|
|
|
Phaser.Keyboard.END = 35;
|
|
|
|
Phaser.Keyboard.HOME = 36;
|
|
|
|
Phaser.Keyboard.LEFT = 37;
|
|
|
|
Phaser.Keyboard.UP = 38;
|
|
|
|
Phaser.Keyboard.RIGHT = 39;
|
|
|
|
Phaser.Keyboard.DOWN = 40;
|
2015-07-09 13:28:58 +00:00
|
|
|
Phaser.Keyboard.PLUS = 43;
|
|
|
|
Phaser.Keyboard.MINUS = 44;
|
2013-08-31 20:50:34 +00:00
|
|
|
Phaser.Keyboard.INSERT = 45;
|
|
|
|
Phaser.Keyboard.DELETE = 46;
|
|
|
|
Phaser.Keyboard.HELP = 47;
|
|
|
|
Phaser.Keyboard.NUM_LOCK = 144;
|