From 97fa3ab2c0108d2302b3ba7a97d4fdc59d1d04b9 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Mon, 4 Jun 2018 16:49:03 +0100 Subject: [PATCH] KeyboardManager.addKeys can now take either an object or key codes, or a comma-separated string, as its input. --- src/input/keyboard/KeyboardManager.js | 65 ++++++++++++++++----------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/src/input/keyboard/KeyboardManager.js b/src/input/keyboard/KeyboardManager.js index 671851462..24f5a06f8 100644 --- a/src/input/keyboard/KeyboardManager.js +++ b/src/input/keyboard/KeyboardManager.js @@ -13,12 +13,6 @@ var KeyMap = require('./keys/KeyMap'); var ProcessKeyDown = require('./keys/ProcessKeyDown'); var ProcessKeyUp = require('./keys/ProcessKeyUp'); -/** - * @callback KeyboardHandler - * - * @property {KeyboardEvent} event - [description] - */ - /** * @classdesc * The Keyboard Manager is a helper class that belongs to the Input Manager. @@ -119,7 +113,7 @@ var KeyboardManager = new Class({ this.combos = []; /** - * [description] + * An internal event queue. * * @name Phaser.Input.Keyboard.KeyboardManager#queue * @type {KeyboardEvent[]} @@ -204,7 +198,8 @@ var KeyboardManager = new Class({ }, /** - * [description] + * 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.0.0 @@ -219,7 +214,8 @@ var KeyboardManager = new Class({ }, /** - * [description] + * Stops the Keyboard Event listeners. + * This is called automatically and does not need to be manually invoked. * * @method Phaser.Input.Keyboard.KeyboardManager#stopListeners * @since 3.0.0 @@ -266,26 +262,50 @@ var KeyboardManager = new Class({ /** * A practical way to create an object containing user selected hotkeys. * - * For example, + * For example: * - * addKeys({ 'up': Phaser.Input.Keyboard.KeyCodes.W, 'down': Phaser.Input.Keyboard.KeyCodes.S }); + * ```javascript + * this.input.keyboard.addKeys({ 'up': Phaser.Input.Keyboard.KeyCodes.W, 'down': Phaser.Input.Keyboard.KeyCodes.S }); + * ``` + * + * would return an object containing the properties (`up` and `down`) mapped to W and S {@link Phaser.Input.Keyboard.Key} objects. * - * would return an object containing properties (`up` and `down`) referring to {@link Phaser.Input.Keyboard.Key} objects. + * You can also pass in a comma-separated string: + * + * ```javascript + * this.input.keyboard.addKeys('W,S,A,D'); + * ``` + * + * Which will return an object with the properties W, S, A and D mapped to the relevant Key objects. + * + * To use non-alpha numeric keys, use a string, such as 'UP', 'SPACE' or 'LEFT'. * * @method Phaser.Input.Keyboard.KeyboardManager#addKeys * @since 3.0.0 * - * @param {object} keys - [description] + * @param {(object|string)} keys - An object containing Key Codes, or a comma-separated string. * - * @return {object} [description] + * @return {object} An object containing Key objects mapped to the input properties. */ addKeys: function (keys) { var output = {}; - for (var key in keys) + if (typeof keys === 'string') { - output[key] = this.addKey(keys[key]); + keys = keys.split(','); + + for (var i = 0; i < keys.length; i++) + { + output[keys[i]] = this.addKey(keys[i]); + } + } + else + { + for (var key in keys) + { + output[key] = this.addKey(keys[key]); + } } return output; @@ -308,14 +328,12 @@ var KeyboardManager = new Class({ if (typeof keyCode === 'string') { - keyCode = keyCodes[keyCode.toUpperCase()]; + keyCode = KeyCodes[keyCode.toUpperCase()]; } if (!keys[keyCode]) { keys[keyCode] = new Key(keyCode); - - // this.captures[keyCode] = true; } return keys[keyCode]; @@ -334,7 +352,6 @@ var KeyboardManager = new Class({ if (this.keys[keyCode]) { this.keys[keyCode] = undefined; - // this.captures[keyCode] = false; } }, @@ -345,7 +362,6 @@ var KeyboardManager = new Class({ * @since 3.0.0 * * @param {(string|integer|string[]|integer[])} keyCodes - [description] - */ addKeyCapture: function (keyCodes) { if (!Array.isArray(keyCodes)) @@ -358,6 +374,7 @@ var KeyboardManager = new Class({ this.captures[keyCodes[i]] = true; } }, + */ /** * [description] @@ -366,7 +383,6 @@ var KeyboardManager = new Class({ * @since 3.0.0 * * @param {(string|integer|string[]|integer[])} keyCodes - [description] - */ removeKeyCapture: function (keyCodes) { if (!Array.isArray(keyCodes)) @@ -379,6 +395,7 @@ var KeyboardManager = new Class({ this.captures[keyCodes[i]] = false; } }, + */ /** * [description] @@ -465,7 +482,7 @@ var KeyboardManager = new Class({ }, /** - * [description] + * Destroys this Keyboard Manager instance. * * @method Phaser.Input.Keyboard.KeyboardManager#destroy * @since 3.0.0 @@ -478,9 +495,7 @@ var KeyboardManager = new Class({ this.keys = []; this.combos = []; - this.captures = []; this.queue = []; - this.handler = undefined; this.manager = null; }