Skip to content

Commit 8fd0a7a

Browse files
committed
Finished off the Keyboard Manager, tided up the classes and removed un-needed functions.
1 parent 9579263 commit 8fd0a7a

20 files changed

Lines changed: 159 additions & 89 deletions

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '7940ae30-f782-11e6-8cec-3b8d56169a63'
2+
build: '79e0cfa0-f84f-11e6-aabb-e9b6cbc3e561'
33
};
44
module.exports = CHECKSUM;

v3/src/input/keyboard/KeyboardManager.js

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
var EventDispatcher = require('../../events/EventDispatcher');
22
var Event = require('./events');
3-
var KeyCodes = require('./KeyCodes');
4-
var Key = require('./Key');
5-
var ProcessKeyDown = require('./ops/ProcessKeyDown');
6-
var ProcessKeyUp = require('./ops/ProcessKeyUp');
7-
3+
var KeyCodes = require('./keys/KeyCodes');
4+
var Key = require('./keys/Key');
5+
var ProcessKeyDown = require('./keys/ProcessKeyDown');
6+
var ProcessKeyUp = require('./keys/ProcessKeyUp');
7+
8+
/**
9+
* The Keyboard class monitors keyboard input and dispatches keyboard events.
10+
*
11+
* _Note_: many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
12+
* See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
13+
*
14+
* Also please be aware that certain browser extensions can disable or override Phaser keyboard handling.
15+
* For example the Chrome extension vimium is known to disable Phaser from using the D key. And there are others.
16+
* So please check your extensions before opening Phaser issues.
17+
*
18+
* @class Phaser.Keyboard
19+
* @constructor
20+
* @param {Phaser.Game} game - A reference to the currently running game.
21+
*/
822
var KeyboardManager = function (inputManager)
923
{
1024
this.manager = inputManager;
@@ -74,6 +88,47 @@ KeyboardManager.prototype = {
7488
this.target.removeEventListener('keyup', this.keyHandler);
7589
},
7690

91+
/**
92+
* Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right.
93+
*
94+
* @method Phaser.Keyboard#createCursorKeys
95+
* @return {object} An object containing properties: `up`, `down`, `left` and `right` of {@link Phaser.Key} objects.
96+
*/
97+
createCursorKeys: function ()
98+
{
99+
return this.addKeys({
100+
up: KeyCodes.UP,
101+
down: KeyCodes.DOWN,
102+
left: KeyCodes.LEFT,
103+
right: KeyCodes.RIGHT
104+
});
105+
},
106+
107+
/**
108+
* A practical way to create an object containing user selected hotkeys.
109+
*
110+
* For example,
111+
*
112+
* addKeys( { 'up': Phaser.KeyCode.W, 'down': Phaser.KeyCode.S, 'left': Phaser.KeyCode.A, 'right': Phaser.KeyCode.D } );
113+
*
114+
* would return an object containing properties (`up`, `down`, `left` and `right`) referring to {@link Phaser.Key} object.
115+
*
116+
* @method Phaser.Keyboard#addKeys
117+
* @param {object} keys - A key mapping object, i.e. `{ 'up': Phaser.KeyCode.W, 'down': Phaser.KeyCode.S }` or `{ 'up': 52, 'down': 53 }`.
118+
* @return {object} An object containing the properties mapped to {@link Phaser.Key} values.
119+
*/
120+
addKeys: function (keys)
121+
{
122+
var output = {};
123+
124+
for (var key in keys)
125+
{
126+
output[key] = this.addKey(keys[key]);
127+
}
128+
129+
return output;
130+
},
131+
77132
/**
78133
* If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
79134
* The Key object can then be polled, have events attached to it, etc.
@@ -87,8 +142,6 @@ KeyboardManager.prototype = {
87142
if (!this.keys[keycode])
88143
{
89144
this.keys[keycode] = new Key(this, keycode, name);
90-
91-
// this.addKeyCapture(keycode);
92145
}
93146

94147
return this.keys[keycode];
@@ -105,8 +158,6 @@ KeyboardManager.prototype = {
105158
if (this.keys[keycode])
106159
{
107160
this.keys[keycode] = undefined;
108-
109-
// this.removeKeyCapture(keycode);
110161
}
111162
},
112163

@@ -116,11 +167,18 @@ KeyboardManager.prototype = {
116167

117168
update: function ()
118169
{
119-
// Process the event queue, dispatching all of the events that have stored up
170+
if (!this.enabled)
171+
{
172+
return;
173+
}
174+
175+
// Clears the queue array, and also means we don't work on array data that could potentially
176+
// be modified during the processing phase
177+
var queue = this.queue.splice(0, this.queue.length);
120178

121-
var queue = this.queue;
122179
var keys = this.keys;
123180

181+
// Process the event queue, dispatching all of the events that have stored up
124182
for (var i = 0; i < queue.length; i++)
125183
{
126184
var event = queue[i];
@@ -129,6 +187,11 @@ KeyboardManager.prototype = {
129187
{
130188
this.events.dispatch(new Event.KEY_DOWN_EVENT(event));
131189

190+
if (Event._DOWN[event.keyCode])
191+
{
192+
this.events.dispatch(new Event._DOWN[event.keyCode](event));
193+
}
194+
132195
if (keys[event.keyCode])
133196
{
134197
ProcessKeyDown(keys[event.keyCode], event);
@@ -138,14 +201,17 @@ KeyboardManager.prototype = {
138201
{
139202
this.events.dispatch(new Event.KEY_UP_EVENT(event));
140203

204+
if (Event._UP[event.keyCode])
205+
{
206+
this.events.dispatch(new Event._UP[event.keyCode](event));
207+
}
208+
141209
if (keys[event.keyCode])
142210
{
143211
ProcessKeyUp(keys[event.keyCode], event);
144212
}
145213
}
146214
}
147-
148-
queue.length = 0;
149215
}
150216

151217
};
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
1-
module.exports = {
1+
var Event = require('../../../events/Event');
2+
var KeyCodes = require('./../keys/KeyCodes');
3+
4+
var events = {
25
KEY_DOWN_EVENT: require('./KeyDownEvent'),
3-
KEY_UP_EVENT: require('./KeyUpEvent')
6+
KEY_UP_EVENT: require('./KeyUpEvent'),
7+
_UP: [],
8+
_DOWN: []
49
};
10+
11+
function createKeyEvent (type)
12+
{
13+
var KeyEvent = function (keyboardEvent)
14+
{
15+
Event.call(this, type);
16+
17+
this.data = keyboardEvent;
18+
};
19+
20+
KeyEvent.prototype = Object.create(Event.prototype);
21+
KeyEvent.prototype.constructor = KeyEvent;
22+
23+
return KeyEvent;
24+
}
25+
26+
// Inject the KeyCode events
27+
28+
for (var code in KeyCodes)
29+
{
30+
// The Key Down Event Types
31+
32+
var downType = 'KEY_DOWN_' + code;
33+
var upType = 'KEY_UP_' + code;
34+
35+
events._DOWN[KeyCodes[code]] = createKeyEvent(downType);
36+
events._UP[KeyCodes[code]] = createKeyEvent(upType);
37+
38+
// More friendly aliases to the main events
39+
events[downType] = events._DOWN[KeyCodes[code]];
40+
events[upType] = events._UP[KeyCodes[code]];
41+
}
42+
43+
module.exports = events;

v3/src/input/keyboard/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
module.exports = {
44

5-
Key: require('./Key'),
65
KeyboardManager: require('./KeyboardManager'),
7-
KeyCodes: require('./KeyCodes')
6+
7+
Key: require('./keys/Key'),
8+
KeyCodes: require('./keys/KeyCodes'),
9+
10+
JustDown: require('./keys/JustDown'),
11+
JustUp: require('./keys/JustUp'),
12+
DownDuration: require('./keys/DownDuration'),
13+
UpDuration: require('./keys/UpDuration')
814

915
};
File renamed without changes.
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
// A generic Key object which can be passed to the Process functions (and so on)
22

3-
// keycode can be either numeric or a string
3+
// keycode must be an integer
44

5-
var Key = function (keycode, name)
5+
var Key = function (keyCode)
66
{
7-
if (typeof keycode === 'string')
8-
{
9-
keycode = keycode.toUpperCase().charCodeAt(0);
10-
}
11-
12-
if (name === undefined || name === '')
13-
{
14-
name = String.fromCharCode(keycode);
15-
}
16-
177
/**
18-
* @property {number} keyCode - The keycode of this key.
8+
* @property {integer} keyCode - The keycode of this key.
199
*/
20-
this.keyCode = keycode;
10+
this.keyCode = keyCode;
2111

2212
/**
23-
* @property {string} name - A string representation of this Key (worked out via String.fromCharCode if possible, or set in the arguments)
13+
* @property {KeyboardEvent} originalEvent - The original DOM event.
2414
*/
25-
this.name = name;
15+
this.originalEvent = undefined;
2616

2717
/**
2818
* @property {boolean} preventDefault - Should this Key prevent event propagation?
@@ -66,6 +56,12 @@ var Key = function (keycode, name)
6656
*/
6757
this.shiftKey = false;
6858

59+
/**
60+
* @property {integer} location - The location of the modifier key. 0 for standard (or unknown), 1 for left, 2 for right, 3 for numpad.
61+
* @default
62+
*/
63+
this.location = 0;
64+
6965
/**
7066
* @property {number} timeDown - The timestamp when the key was last pressed down. This is based on Game.time.now.
7167
*/
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@ module.exports = {
22
BACKSPACE: 8,
33
TAB: 9,
44
ENTER: 13,
5+
56
SHIFT: 16,
67
CTRL: 17,
78
ALT: 18,
9+
810
PAUSE: 19,
911
CAPS_LOCK: 20,
1012
ESC: 27,
1113
SPACE: 32,
14+
1215
PAGE_UP: 33,
1316
PAGE_DOWN: 34,
1417
END: 35,
1518
HOME: 36,
19+
1620
LEFT: 37,
1721
UP: 38,
1822
RIGHT: 39,
1923
DOWN: 40,
24+
2025
PRINT_SCREEN: 42,
2126
INSERT: 45,
2227
DELETE: 46,
28+
2329
ZERO: 48,
2430
ONE: 49,
2531
TWO: 50,
@@ -30,6 +36,7 @@ module.exports = {
3036
SEVEN: 55,
3137
EIGHT: 56,
3238
NINE: 57,
39+
3340
A: 65,
3441
B: 66,
3542
C: 67,
@@ -56,6 +63,7 @@ module.exports = {
5663
X: 88,
5764
Y: 89,
5865
Z: 90,
66+
5967
F1: 112,
6068
F2: 113,
6169
F3: 114,
@@ -68,6 +76,7 @@ module.exports = {
6876
F10: 121,
6977
F11: 122,
7078
F12: 123,
79+
7180
SEMICOLON: 186,
7281
PLUS: 187,
7382
COMMA: 188,

v3/src/input/keyboard/ops/ProcessKeyDown.js renamed to v3/src/input/keyboard/keys/ProcessKeyDown.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
var ProcessKeyDown = function (key, event)
33
{
4+
key.originalEvent = event;
5+
46
if (key.preventDefault)
57
{
68
event.preventDefault();
@@ -14,12 +16,14 @@ var ProcessKeyDown = function (key, event)
1416
key.altKey = event.altKey;
1517
key.ctrlKey = event.ctrlKey;
1618
key.shiftKey = event.shiftKey;
19+
key.location = event.location;
1720

1821
key.isDown = true;
1922
key.isUp = false;
2023
key.timeDown = event.timeStamp;
2124
key.duration = 0;
2225
key.repeats++;
26+
2327
key._justDown = true;
2428
key._justUp = false;
2529

0 commit comments

Comments
 (0)