forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardManager.js
More file actions
263 lines (211 loc) · 6.47 KB
/
Copy pathKeyboardManager.js
File metadata and controls
263 lines (211 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var Class = require('../../utils/Class');
var EventEmitter = require('eventemitter3');
var Key = require('./keys/Key');
var KeyCodes = require('./keys/KeyCodes');
var KeyCombo = require('./combo/KeyCombo');
var ProcessKeyCombo = require('./combo/ProcessKeyCombo');
var ProcessKeyDown = require('./keys/ProcessKeyDown');
var ProcessKeyUp = require('./keys/ProcessKeyUp');
/**
* The Keyboard class monitors keyboard input and dispatches keyboard events.
*
* _Note_: many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
* See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
*
* Also please be aware that certain browser extensions can disable or override Phaser keyboard handling.
* For example the Chrome extension vimium is known to disable Phaser from using the D key. And there are others.
* So please check your extensions before opening Phaser issues.
*/
var KeyboardManager = new Class({
Extends: EventEmitter,
initialize:
function KeyboardManager (inputManager)
{
EventEmitter.call(this);
this.manager = inputManager;
this.enabled = false;
this.target;
this.keys = [];
this.combos = [];
this.captures = [];
// Standard FIFO queue
this.queue = [];
this.handler;
},
/**
* The Boot handler is called by Phaser.Game when it first starts up.
* The renderer is available by now.
*/
boot: function ()
{
var config = this.manager.config;
this.enabled = config.inputKeyboard;
this.target = config.inputKeyboardEventTarget;
if (this.enabled)
{
this.startListeners();
}
},
startListeners: function ()
{
var queue = this.queue;
var captures = this.captures;
var handler = function (event)
{
if (event.defaultPrevented)
{
// Do nothing if event already handled
return;
}
queue.push(event);
if (captures[event.keyCode])
{
event.preventDefault();
}
};
this.handler = handler;
this.target.addEventListener('keydown', handler, false);
this.target.addEventListener('keyup', handler, false);
},
stopListeners: function ()
{
this.target.removeEventListener('keydown', this.handler);
this.target.removeEventListener('keyup', this.handler);
},
/**
* Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right and also space and shift.
*/
createCursorKeys: function ()
{
return this.addKeys({
up: KeyCodes.UP,
down: KeyCodes.DOWN,
left: KeyCodes.LEFT,
right: KeyCodes.RIGHT,
space: KeyCodes.SPACE,
shift: KeyCodes.SHIFT
});
},
/**
* A practical way to create an object containing user selected hotkeys.
*
* For example,
*
* 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.
*/
addKeys: function (keys)
{
var output = {};
for (var key in keys)
{
output[key] = this.addKey(keys[key]);
}
return output;
},
/**
* 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.
*/
addKey: function (keyCode)
{
var keys = this.keys;
if (!keys[keyCode])
{
keys[keyCode] = new Key(keyCode);
this.captures[keyCode] = true;
}
return keys[keyCode];
},
/**
* Removes a Key object from the Keyboard manager.
*/
removeKey: function (keyCode)
{
if (this.keys[keyCode])
{
this.keys[keyCode] = undefined;
this.captures[keyCode] = false;
}
},
addKeyCapture: function (keyCodes)
{
if (!Array.isArray(keyCodes))
{
keyCodes = [ keyCodes ];
}
for (var i = 0; i < keyCodes.length; i++)
{
this.captures[keyCodes[i]] = true;
}
},
removeKeyCapture: function (keyCodes)
{
if (!Array.isArray(keyCodes))
{
keyCodes = [ keyCodes ];
}
for (var i = 0; i < keyCodes.length; i++)
{
this.captures[keyCodes[i]] = false;
}
},
createCombo: function (keys, config)
{
return new KeyCombo(this, keys, config);
},
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
// type = 'keydown', 'keyup'
// keyCode = integer
update: function ()
{
var len = this.queue.length;
if (!this.enabled || len === 0)
{
return;
}
// Clears the queue array, and also means we don't work on array data that could potentially
// be modified during the processing phase
var queue = this.queue.splice(0, len);
var keys = this.keys;
// Process the event queue, dispatching all of the events that have stored up
for (var i = 0; i < len; i++)
{
var event = queue[i];
// Will emit a keyboard or keyup event
this.emit(event.type, event);
if (event.type === 'keydown')
{
this.emit('down_' + event.keyCode, event);
if (keys[event.keyCode])
{
ProcessKeyDown(keys[event.keyCode], event);
}
}
else
{
this.emit('up_' + event.keyCode, event);
if (keys[event.keyCode])
{
ProcessKeyUp(keys[event.keyCode], event);
}
}
}
},
shutdown: function ()
{
this.removeAllListeners();
},
destroy: function ()
{
this.stopListeners();
this.removeAllListeners();
this.keys = [];
this.combos = [];
this.captures = [];
this.queue = [];
this.handler = undefined;
}
});
module.exports = KeyboardManager;