Phaser.Keyboard = function (game){ this.game = game; this.enabled = true ; this.event = null ; this.pressEvent = null ; this.callbackContext = this; this.onDownCallback = null ; this.onPressCallback = null ; this.onUpCallback = null ; this._keys = [] ; this._capture = [] ; this._onKeyDown = null ; this._onKeyPress = null ; this._onKeyUp = null ; this._i = 0; this._k = 0; } ; Phaser.Keyboard.prototype = { addCallbacks: function (context, onDown, onUp, onPress){ this.callbackContext = context; if (onDown !== undefined && onDown !== null ) { this.onDownCallback = onDown; } if (onUp !== undefined && onUp !== null ) { this.onUpCallback = onUp; } if (onPress !== undefined && onPress !== null ) { this.onPressCallback = onPress; } } , addKey: function (keycode){ if (!this._keys[keycode]) { this._keys[keycode] = new Phaser.Key(this.game, keycode); this.addKeyCapture(keycode); } return this._keys[keycode]; } , addKeys: function (keys){ var output = { } ; for (var key in keys){ output[key] = this.addKey(keys[key]); } return output; } , removeKey: function (keycode){ if (this._keys[keycode]) { this._keys[keycode] = null ; this.removeKeyCapture(keycode); } } , createCursorKeys: function (){ return this.addKeys({ 'up': Phaser.KeyCode.UP, 'down': Phaser.KeyCode.DOWN, 'left': Phaser.KeyCode.LEFT, 'right': Phaser.KeyCode.RIGHT} ); } , start: function (){ if (this.game.device.cocoonJS) { return ; } if (this._onKeyDown !== null ) { return ; } var _this = this; this._onKeyDown = function (event){ return _this.processKeyDown(event); } ; this._onKeyUp = function (event){ 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 ); } , stop: function (){ window.removeEventListener('keydown', this._onKeyDown); window.removeEventListener('keyup', this._onKeyUp); window.removeEventListener('keypress', this._onKeyPress); this._onKeyDown = null ; this._onKeyUp = null ; this._onKeyPress = null ; } , destroy: function (){ this.stop(); this.clearCaptures(); this._keys.length = 0; this._i = 0; } , addKeyCapture: function (keycode){ if (typeof keycode === 'object') { for (var key in keycode){ this._capture[keycode[key]] = true ; } } else { this._capture[keycode] = true ; } } , removeKeyCapture: function (keycode){ delete this._capture[keycode]; } , clearCaptures: function (){ this._capture = { } ; } , update: function (){ this._i = _AN_Read_length('length', this._keys); while (this._i-- ){ if (this._keys[this._i]) { this._keys[this._i].update(); } } } , processKeyDown: function (event){ this.event = event; if (!this.game.input.enabled || !this.enabled) { return ; } var key = event.keyCode; if (this._capture[key]) { event.preventDefault(); } if (!this._keys[key]) { this._keys[key] = new Phaser.Key(this.game, key); } this._keys[key].processKeyDown(event); this._k = key; if (this.onDownCallback) { this.onDownCallback.call(this.callbackContext, event); } } , processKeyPress: function (event){ this.pressEvent = event; if (!this.game.input.enabled || !this.enabled) { return ; } if (this.onPressCallback) { this.onPressCallback.call(this.callbackContext, String.fromCharCode(event.charCode), event); } } , processKeyUp: function (event){ this.event = event; if (!this.game.input.enabled || !this.enabled) { return ; } var key = event.keyCode; if (this._capture[key]) { event.preventDefault(); } if (!this._keys[key]) { this._keys[key] = new Phaser.Key(this.game, key); } this._keys[key].processKeyUp(event); if (this.onUpCallback) { this.onUpCallback.call(this.callbackContext, event); } } , reset: function (hard){ if (hard === undefined) { hard = true ; } this.event = null ; var i = _AN_Read_length('length', this._keys); while (i-- ){ if (this._keys[i]) { this._keys[i].reset(hard); } } } , downDuration: function (keycode, duration){ if (this._keys[keycode]) { return this._keys[keycode].downDuration(duration); } else { return null ; } } , upDuration: function (keycode, duration){ if (this._keys[keycode]) { return this._keys[keycode].upDuration(duration); } else { return null ; } } , isDown: function (keycode){ if (this._keys[keycode]) { return this._keys[keycode].isDown; } else { return null ; } } } ; Object.defineProperty(Phaser.Keyboard.prototype, "lastChar", { get: function (){ if (this.event.charCode === 32) { return ''; } else { return String.fromCharCode(this.pressEvent.charCode); } } } ); Object.defineProperty(Phaser.Keyboard.prototype, "lastKey", { get: function (){ return this._keys[this._k]; } } ); Phaser.Keyboard.prototype.constructor = Phaser.Keyboard; Phaser.KeyCode = { A: "A".charCodeAt(0), B: "B".charCodeAt(0), C: "C".charCodeAt(0), D: "D".charCodeAt(0), E: "E".charCodeAt(0), F: "F".charCodeAt(0), G: "G".charCodeAt(0), H: "H".charCodeAt(0), I: "I".charCodeAt(0), J: "J".charCodeAt(0), K: "K".charCodeAt(0), L: "L".charCodeAt(0), M: "M".charCodeAt(0), N: "N".charCodeAt(0), O: "O".charCodeAt(0), P: "P".charCodeAt(0), Q: "Q".charCodeAt(0), R: "R".charCodeAt(0), S: "S".charCodeAt(0), T: "T".charCodeAt(0), U: "U".charCodeAt(0), V: "V".charCodeAt(0), W: "W".charCodeAt(0), X: "X".charCodeAt(0), Y: "Y".charCodeAt(0), Z: "Z".charCodeAt(0), ZERO: "0".charCodeAt(0), ONE: "1".charCodeAt(0), TWO: "2".charCodeAt(0), THREE: "3".charCodeAt(0), FOUR: "4".charCodeAt(0), FIVE: "5".charCodeAt(0), SIX: "6".charCodeAt(0), SEVEN: "7".charCodeAt(0), EIGHT: "8".charCodeAt(0), NINE: "9".charCodeAt(0), NUMPAD_0: 96, NUMPAD_1: 97, NUMPAD_2: 98, NUMPAD_3: 99, NUMPAD_4: 100, NUMPAD_5: 101, NUMPAD_6: 102, NUMPAD_7: 103, NUMPAD_8: 104, NUMPAD_9: 105, NUMPAD_MULTIPLY: 106, NUMPAD_ADD: 107, NUMPAD_ENTER: 108, NUMPAD_SUBTRACT: 109, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, F1: 112, F2: 113, F3: 114, F4: 115, F5: 116, F6: 117, F7: 118, F8: 119, F9: 120, F10: 121, F11: 122, F12: 123, F13: 124, F14: 125, F15: 126, COLON: 186, EQUALS: 187, COMMA: 188, UNDERSCORE: 189, PERIOD: 190, QUESTION_MARK: 191, TILDE: 192, OPEN_BRACKET: 219, BACKWARD_SLASH: 220, CLOSED_BRACKET: 221, QUOTES: 222, BACKSPACE: 8, TAB: 9, CLEAR: 12, ENTER: 13, SHIFT: 16, CONTROL: 17, ALT: 18, CAPS_LOCK: 20, ESC: 27, SPACEBAR: 32, PAGE_UP: 33, PAGE_DOWN: 34, END: 35, HOME: 36, LEFT: 37, UP: 38, RIGHT: 39, DOWN: 40, PLUS: 43, MINUS: 44, INSERT: 45, DELETE: 46, HELP: 47, NUM_LOCK: 144} ; for (var key in Phaser.KeyCode){ if (Phaser.KeyCode.hasOwnProperty(key) && !key.match(/[a-z]/)) { Phaser.Keyboard[key] = Phaser.KeyCode[key]; } }