Merge pull request #2118 from pnstickne/wip-2031

Created Phaser.KeyCode enumeration / pseudo-type
This commit is contained in:
Richard Davey 2015-10-13 13:56:46 +03:00
commit 3ee59f1de3
2 changed files with 251 additions and 119 deletions

View file

@ -10,7 +10,7 @@
* @class Phaser.Key * @class Phaser.Key
* @constructor * @constructor
* @param {Phaser.Game} game - Current game instance. * @param {Phaser.Game} game - Current game instance.
* @param {number} keycode - The key code this Key is responsible for. * @param {integer} keycode - The key code this Key is responsible for. See {@link Phaser.KeyCode}.
*/ */
Phaser.Key = function (game, keycode) { Phaser.Key = function (game, keycode) {

View file

@ -7,8 +7,8 @@
/** /**
* The Keyboard class monitors keyboard input and dispatches keyboard events. * 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 * _Note_: many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
* 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/ * See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
* *
* @class Phaser.Keyboard * @class Phaser.Keyboard
* @constructor * @constructor
@ -142,7 +142,7 @@ Phaser.Keyboard.prototype = {
* The Key object can then be polled, have events attached to it, etc. * The Key object can then be polled, have events attached to it, etc.
* *
* @method Phaser.Keyboard#addKey * @method Phaser.Keyboard#addKey
* @param {number} keycode - The keycode of the key, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR * @param {integer} keycode - The {@link Phaser.KeyCode keycode} of the key.
* @return {Phaser.Key} The Key object which you can store locally and reference directly. * @return {Phaser.Key} The Key object which you can store locally and reference directly.
*/ */
addKey: function (keycode) { addKey: function (keycode) {
@ -161,13 +161,15 @@ Phaser.Keyboard.prototype = {
/** /**
* A practical way to create an object containing user selected hotkeys. * A practical way to create an object containing user selected hotkeys.
* *
* For example: `addKeys( { 'up': Phaser.Keyboard.W, 'down': Phaser.Keyboard.S, 'left': Phaser.Keyboard.A, 'right': Phaser.Keyboard.D } );` * For example,
* *
* Would return an object containing the properties `up`, `down`, `left` and `right` that you could poll just like a Phaser.Key object. * addKeys( { 'up': Phaser.KeyCode.W, 'down': Phaser.KeyCode.S, 'left': Phaser.KeyCode.A, 'right': Phaser.KeyCode.D } );
*
* would return an object containing properties (`up`, `down`, `left` and `right`) referring to {@link Phaser.Key} object.
* *
* @method Phaser.Keyboard#addKeys * @method Phaser.Keyboard#addKeys
* @param {object} keys - A key mapping object, i.e. `{ 'up': Phaser.Keyboard.W, 'down': Phaser.Keyboard.S }` or `{ 'up': 52, 'down': 53 }`. * @param {object} keys - A key mapping object, i.e. `{ 'up': Phaser.KeyCode.W, 'down': Phaser.KeyCode.S }` or `{ 'up': 52, 'down': 53 }`.
* @return {object} An object containing user selected properties * @return {object} An object containing the properties mapped to {@link Phaser.Key} values.
*/ */
addKeys: function (keys) { addKeys: function (keys) {
@ -186,7 +188,7 @@ Phaser.Keyboard.prototype = {
* Removes a Key object from the Keyboard manager. * Removes a Key object from the Keyboard manager.
* *
* @method Phaser.Keyboard#removeKey * @method Phaser.Keyboard#removeKey
* @param {number} keycode - The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR * @param {integer} keycode - The {@link Phaser.KeyCode keycode} of the key to remove.
*/ */
removeKey: function (keycode) { removeKey: function (keycode) {
@ -203,11 +205,11 @@ Phaser.Keyboard.prototype = {
* Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right. * Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right.
* *
* @method Phaser.Keyboard#createCursorKeys * @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. * @return {object} An object containing properties: `up`, `down`, `left` and `right` of {@link Phaser.Key} objects.
*/ */
createCursorKeys: function () { createCursorKeys: function () {
return this.addKeys({ 'up': Phaser.Keyboard.UP, 'down': Phaser.Keyboard.DOWN, 'left': Phaser.Keyboard.LEFT, 'right': Phaser.Keyboard.RIGHT }); return this.addKeys({ 'up': Phaser.KeyCode.UP, 'down': Phaser.KeyCode.DOWN, 'left': Phaser.KeyCode.LEFT, 'right': Phaser.KeyCode.RIGHT });
}, },
@ -216,6 +218,7 @@ Phaser.Keyboard.prototype = {
* This is called automatically by Phaser.Input and should not normally be invoked directly. * This is called automatically by Phaser.Input and should not normally be invoked directly.
* *
* @method Phaser.Keyboard#start * @method Phaser.Keyboard#start
* @protected
*/ */
start: function () { start: function () {
@ -287,11 +290,14 @@ Phaser.Keyboard.prototype = {
/** /**
* By default when a key is pressed Phaser will not stop the event from propagating up to the browser. * 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. * 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. *
* The `addKeyCapture` method enables consuming keyboard event for specific keys so it doesn't bubble up to the the browser
* and cause the default browser behavior.
*
* Pass in either a single keycode or an array/hash of keycodes. * Pass in either a single keycode or an array/hash of keycodes.
* *
* @method Phaser.Keyboard#addKeyCapture * @method Phaser.Keyboard#addKeyCapture
* @param {number|array|object} keycode - Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68]. * @param {integer|integer[]|object} keycode - Either a single {@link Phaser.KeyCode keycode} or an array/hash of keycodes such as `[65, 67, 68]`.
*/ */
addKeyCapture: function (keycode) { addKeyCapture: function (keycode) {
@ -312,7 +318,7 @@ Phaser.Keyboard.prototype = {
* Removes an existing key capture. * Removes an existing key capture.
* *
* @method Phaser.Keyboard#removeKeyCapture * @method Phaser.Keyboard#removeKeyCapture
* @param {number} keycode * @param {integer} keycode - The {@link Phaser.KeyCode keycode} to remove capturing of.
*/ */
removeKeyCapture: function (keycode) { removeKeyCapture: function (keycode) {
@ -475,7 +481,7 @@ Phaser.Keyboard.prototype = {
* or was pressed down longer ago than then given duration. * or was pressed down longer ago than then given duration.
* *
* @method Phaser.Keyboard#downDuration * @method Phaser.Keyboard#downDuration
* @param {number} keycode - The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR * @param {integer} keycode - The {@link Phaser.KeyCode keycode} of the key to check: i.e. Phaser.KeyCode.UP or Phaser.KeyCode.SPACEBAR.
* @param {number} [duration=50] - The duration within which the key is considered as being just pressed. Given in ms. * @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. * @return {boolean} True if the key was pressed down within the given duration, false if not or null if the Key wasn't found.
*/ */
@ -497,7 +503,7 @@ Phaser.Keyboard.prototype = {
* or was pressed down longer ago than then given duration. * or was pressed down longer ago than then given duration.
* *
* @method Phaser.Keyboard#upDuration * @method Phaser.Keyboard#upDuration
* @param {number} keycode - The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR * @param {Phaser.KeyCode|integer} keycode - The keycode of the key to check, i.e. Phaser.KeyCode.UP or Phaser.KeyCode.SPACEBAR.
* @param {number} [duration=50] - The duration within which the key is considered as being just released. Given in ms. * @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. * @return {boolean} True if the key was released within the given duration, false if not or null if the Key wasn't found.
*/ */
@ -518,7 +524,7 @@ Phaser.Keyboard.prototype = {
* Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser. * Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser.
* *
* @method Phaser.Keyboard#isDown * @method Phaser.Keyboard#isDown
* @param {number} keycode - The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR * @param {integer} keycode - The {@link Phaser.KeyCode keycode} of the key to check: i.e. Phaser.KeyCode.UP or Phaser.KeyCode.SPACEBAR.
* @return {boolean} True if the key is currently down, false if not or null if the Key wasn't found. * @return {boolean} True if the key is currently down, false if not or null if the Key wasn't found.
*/ */
isDown: function (keycode) { isDown: function (keycode) {
@ -577,105 +583,231 @@ Object.defineProperty(Phaser.Keyboard.prototype, "lastKey", {
Phaser.Keyboard.prototype.constructor = Phaser.Keyboard; Phaser.Keyboard.prototype.constructor = Phaser.Keyboard;
Phaser.Keyboard.A = "A".charCodeAt(0); /**
Phaser.Keyboard.B = "B".charCodeAt(0); * A key code represents a physical key on a keyboard.
Phaser.Keyboard.C = "C".charCodeAt(0); *
Phaser.Keyboard.D = "D".charCodeAt(0); * The KeyCode class contains commonly supported keyboard key codes which can be used
Phaser.Keyboard.E = "E".charCodeAt(0); * as keycode`-parameters in several {@link Phaser.Keyboard} and {@link Phaser.Key} methods.
Phaser.Keyboard.F = "F".charCodeAt(0); *
Phaser.Keyboard.G = "G".charCodeAt(0); * _Note_: These values should only be used indirectly, eg. as `Phaser.KeyCode.KEY`.
Phaser.Keyboard.H = "H".charCodeAt(0); * Future versions may replace the actual values, such that they remain compatible with `keycode`-parameters.
Phaser.Keyboard.I = "I".charCodeAt(0); * The current implementation maps to the {@link https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode KeyboardEvent.keyCode} property.
Phaser.Keyboard.J = "J".charCodeAt(0); *
Phaser.Keyboard.K = "K".charCodeAt(0); * _Note_: Use `Phaser.KeyCode.KEY` instead of `Phaser.Keyboard.KEY` to refer to a key code;
Phaser.Keyboard.L = "L".charCodeAt(0); * the latter approach is supported for compatibility.
Phaser.Keyboard.M = "M".charCodeAt(0); *
Phaser.Keyboard.N = "N".charCodeAt(0); * @namespace
Phaser.Keyboard.O = "O".charCodeAt(0); */
Phaser.Keyboard.P = "P".charCodeAt(0); Phaser.KeyCode = {
Phaser.Keyboard.Q = "Q".charCodeAt(0); /** @static */
Phaser.Keyboard.R = "R".charCodeAt(0); A: "A".charCodeAt(0),
Phaser.Keyboard.S = "S".charCodeAt(0); /** @static */
Phaser.Keyboard.T = "T".charCodeAt(0); B: "B".charCodeAt(0),
Phaser.Keyboard.U = "U".charCodeAt(0); /** @static */
Phaser.Keyboard.V = "V".charCodeAt(0); C: "C".charCodeAt(0),
Phaser.Keyboard.W = "W".charCodeAt(0); /** @static */
Phaser.Keyboard.X = "X".charCodeAt(0); D: "D".charCodeAt(0),
Phaser.Keyboard.Y = "Y".charCodeAt(0); /** @static */
Phaser.Keyboard.Z = "Z".charCodeAt(0); E: "E".charCodeAt(0),
Phaser.Keyboard.ZERO = "0".charCodeAt(0); /** @static */
Phaser.Keyboard.ONE = "1".charCodeAt(0); F: "F".charCodeAt(0),
Phaser.Keyboard.TWO = "2".charCodeAt(0); /** @static */
Phaser.Keyboard.THREE = "3".charCodeAt(0); G: "G".charCodeAt(0),
Phaser.Keyboard.FOUR = "4".charCodeAt(0); /** @static */
Phaser.Keyboard.FIVE = "5".charCodeAt(0); H: "H".charCodeAt(0),
Phaser.Keyboard.SIX = "6".charCodeAt(0); /** @static */
Phaser.Keyboard.SEVEN = "7".charCodeAt(0); I: "I".charCodeAt(0),
Phaser.Keyboard.EIGHT = "8".charCodeAt(0); /** @static */
Phaser.Keyboard.NINE = "9".charCodeAt(0); J: "J".charCodeAt(0),
Phaser.Keyboard.NUMPAD_0 = 96; /** @static */
Phaser.Keyboard.NUMPAD_1 = 97; K: "K".charCodeAt(0),
Phaser.Keyboard.NUMPAD_2 = 98; /** @static */
Phaser.Keyboard.NUMPAD_3 = 99; L: "L".charCodeAt(0),
Phaser.Keyboard.NUMPAD_4 = 100; /** @static */
Phaser.Keyboard.NUMPAD_5 = 101; M: "M".charCodeAt(0),
Phaser.Keyboard.NUMPAD_6 = 102; /** @static */
Phaser.Keyboard.NUMPAD_7 = 103; N: "N".charCodeAt(0),
Phaser.Keyboard.NUMPAD_8 = 104; /** @static */
Phaser.Keyboard.NUMPAD_9 = 105; O: "O".charCodeAt(0),
Phaser.Keyboard.NUMPAD_MULTIPLY = 106; /** @static */
Phaser.Keyboard.NUMPAD_ADD = 107; P: "P".charCodeAt(0),
Phaser.Keyboard.NUMPAD_ENTER = 108; /** @static */
Phaser.Keyboard.NUMPAD_SUBTRACT = 109; Q: "Q".charCodeAt(0),
Phaser.Keyboard.NUMPAD_DECIMAL = 110; /** @static */
Phaser.Keyboard.NUMPAD_DIVIDE = 111; R: "R".charCodeAt(0),
Phaser.Keyboard.F1 = 112; /** @static */
Phaser.Keyboard.F2 = 113; S: "S".charCodeAt(0),
Phaser.Keyboard.F3 = 114; /** @static */
Phaser.Keyboard.F4 = 115; T: "T".charCodeAt(0),
Phaser.Keyboard.F5 = 116; /** @static */
Phaser.Keyboard.F6 = 117; U: "U".charCodeAt(0),
Phaser.Keyboard.F7 = 118; /** @static */
Phaser.Keyboard.F8 = 119; V: "V".charCodeAt(0),
Phaser.Keyboard.F9 = 120; /** @static */
Phaser.Keyboard.F10 = 121; W: "W".charCodeAt(0),
Phaser.Keyboard.F11 = 122; /** @static */
Phaser.Keyboard.F12 = 123; X: "X".charCodeAt(0),
Phaser.Keyboard.F13 = 124; /** @static */
Phaser.Keyboard.F14 = 125; Y: "Y".charCodeAt(0),
Phaser.Keyboard.F15 = 126; /** @static */
Phaser.Keyboard.COLON = 186; Z: "Z".charCodeAt(0),
Phaser.Keyboard.EQUALS = 187; /** @static */
Phaser.Keyboard.COMMA = 188; ZERO: "0".charCodeAt(0),
Phaser.Keyboard.UNDERSCORE = 189; /** @static */
Phaser.Keyboard.PERIOD = 190; ONE: "1".charCodeAt(0),
Phaser.Keyboard.QUESTION_MARK = 191; /** @static */
Phaser.Keyboard.TILDE = 192; TWO: "2".charCodeAt(0),
Phaser.Keyboard.OPEN_BRACKET = 219; /** @static */
Phaser.Keyboard.BACKWARD_SLASH = 220; THREE: "3".charCodeAt(0),
Phaser.Keyboard.CLOSED_BRACKET = 221; /** @static */
Phaser.Keyboard.QUOTES = 222; FOUR: "4".charCodeAt(0),
Phaser.Keyboard.BACKSPACE = 8; /** @static */
Phaser.Keyboard.TAB = 9; FIVE: "5".charCodeAt(0),
Phaser.Keyboard.CLEAR = 12; /** @static */
Phaser.Keyboard.ENTER = 13; SIX: "6".charCodeAt(0),
Phaser.Keyboard.SHIFT = 16; /** @static */
Phaser.Keyboard.CONTROL = 17; SEVEN: "7".charCodeAt(0),
Phaser.Keyboard.ALT = 18; /** @static */
Phaser.Keyboard.CAPS_LOCK = 20; EIGHT: "8".charCodeAt(0),
Phaser.Keyboard.ESC = 27; /** @static */
Phaser.Keyboard.SPACEBAR = 32; NINE: "9".charCodeAt(0),
Phaser.Keyboard.PAGE_UP = 33; /** @static */
Phaser.Keyboard.PAGE_DOWN = 34; NUMPAD_0: 96,
Phaser.Keyboard.END = 35; /** @static */
Phaser.Keyboard.HOME = 36; NUMPAD_1: 97,
Phaser.Keyboard.LEFT = 37; /** @static */
Phaser.Keyboard.UP = 38; NUMPAD_2: 98,
Phaser.Keyboard.RIGHT = 39; /** @static */
Phaser.Keyboard.DOWN = 40; NUMPAD_3: 99,
Phaser.Keyboard.PLUS = 43; /** @static */
Phaser.Keyboard.MINUS = 44; NUMPAD_4: 100,
Phaser.Keyboard.INSERT = 45; /** @static */
Phaser.Keyboard.DELETE = 46; NUMPAD_5: 101,
Phaser.Keyboard.HELP = 47; /** @static */
Phaser.Keyboard.NUM_LOCK = 144; NUMPAD_6: 102,
/** @static */
NUMPAD_7: 103,
/** @static */
NUMPAD_8: 104,
/** @static */
NUMPAD_9: 105,
/** @static */
NUMPAD_MULTIPLY: 106,
/** @static */
NUMPAD_ADD: 107,
/** @static */
NUMPAD_ENTER: 108,
/** @static */
NUMPAD_SUBTRACT: 109,
/** @static */
NUMPAD_DECIMAL: 110,
/** @static */
NUMPAD_DIVIDE: 111,
/** @static */
F1: 112,
/** @static */
F2: 113,
/** @static */
F3: 114,
/** @static */
F4: 115,
/** @static */
F5: 116,
/** @static */
F6: 117,
/** @static */
F7: 118,
/** @static */
F8: 119,
/** @static */
F9: 120,
/** @static */
F10: 121,
/** @static */
F11: 122,
/** @static */
F12: 123,
/** @static */
F13: 124,
/** @static */
F14: 125,
/** @static */
F15: 126,
/** @static */
COLON: 186,
/** @static */
EQUALS: 187,
/** @static */
COMMA: 188,
/** @static */
UNDERSCORE: 189,
/** @static */
PERIOD: 190,
/** @static */
QUESTION_MARK: 191,
/** @static */
TILDE: 192,
/** @static */
OPEN_BRACKET: 219,
/** @static */
BACKWARD_SLASH: 220,
/** @static */
CLOSED_BRACKET: 221,
/** @static */
QUOTES: 222,
/** @static */
BACKSPACE: 8,
/** @static */
TAB: 9,
/** @static */
CLEAR: 12,
/** @static */
ENTER: 13,
/** @static */
SHIFT: 16,
/** @static */
CONTROL: 17,
/** @static */
ALT: 18,
/** @static */
CAPS_LOCK: 20,
/** @static */
ESC: 27,
/** @static */
SPACEBAR: 32,
/** @static */
PAGE_UP: 33,
/** @static */
PAGE_DOWN: 34,
/** @static */
END: 35,
/** @static */
HOME: 36,
/** @static */
LEFT: 37,
/** @static */
UP: 38,
/** @static */
RIGHT: 39,
/** @static */
DOWN: 40,
/** @static */
PLUS: 43,
/** @static */
MINUS: 44,
/** @static */
INSERT: 45,
/** @static */
DELETE: 46,
/** @static */
HELP: 47,
/** @static */
NUM_LOCK: 144
};
// Duplicate Phaser.KeyCode values in Phaser.Keyboard for compatibility
for (var key in Phaser.KeyCode) {
if (Phaser.KeyCode.hasOwnProperty(key) && !key.match(/[a-z]/)) {
Phaser.Keyboard[key] = Phaser.KeyCode[key];
}
}