-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
KeyCombo.js
287 lines (251 loc) · 7.82 KB
/
KeyCombo.js
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../../utils/Class');
var Events = require('../events');
var GetFastValue = require('../../../utils/object/GetFastValue');
var ProcessKeyCombo = require('./ProcessKeyCombo');
var ResetKeyCombo = require('./ResetKeyCombo');
/**
* @classdesc
* A KeyCombo will listen for a specific string of keys from the Keyboard, and when it receives them
* it will emit a `keycombomatch` event from the Keyboard Manager.
*
* The keys to be listened for can be defined as:
*
* A string (i.e. 'ATARI')
* An array of either integers (key codes) or strings, or a mixture of both
* An array of objects (such as Key objects) with a public 'keyCode' property
*
* For example, to listen for the Konami code (up, up, down, down, left, right, left, right, b, a, enter)
* you could pass the following array of key codes:
*
* ```javascript
* this.input.keyboard.createCombo([ 38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13 ], { resetOnMatch: true });
*
* this.input.keyboard.on('keycombomatch', function (event) {
* console.log('Konami Code entered!');
* });
* ```
*
* Or, to listen for the user entering the word PHASER:
*
* ```javascript
* this.input.keyboard.createCombo('PHASER');
* ```
*
* @class KeyCombo
* @memberof Phaser.Input.Keyboard
* @constructor
* @listens Phaser.Input.Keyboard.Events#ANY_KEY_DOWN
* @since 3.0.0
*
* @param {Phaser.Input.Keyboard.KeyboardPlugin} keyboardPlugin - A reference to the Keyboard Plugin.
* @param {(string|number[]|object[])} keys - The keys that comprise this combo.
* @param {Phaser.Types.Input.Keyboard.KeyComboConfig} [config] - A Key Combo configuration object.
*/
var KeyCombo = new Class({
initialize:
function KeyCombo (keyboardPlugin, keys, config)
{
if (config === undefined) { config = {}; }
// Can't have a zero or single length combo (string or array based)
if (keys.length < 2)
{
return false;
}
/**
* A reference to the Keyboard Manager
*
* @name Phaser.Input.Keyboard.KeyCombo#manager
* @type {Phaser.Input.Keyboard.KeyboardPlugin}
* @since 3.0.0
*/
this.manager = keyboardPlugin;
/**
* A flag that controls if this Key Combo is actively processing keys or not.
*
* @name Phaser.Input.Keyboard.KeyCombo#enabled
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.enabled = true;
/**
* An array of the keycodes that comprise this combo.
*
* @name Phaser.Input.Keyboard.KeyCombo#keyCodes
* @type {array}
* @default []
* @since 3.0.0
*/
this.keyCodes = [];
// if 'keys' is a string we need to get the keycode of each character in it
for (var i = 0; i < keys.length; i++)
{
var char = keys[i];
if (typeof char === 'string')
{
this.keyCodes.push(char.toUpperCase().charCodeAt(0));
}
else if (typeof char === 'number')
{
this.keyCodes.push(char);
}
else if (char.hasOwnProperty('keyCode'))
{
this.keyCodes.push(char.keyCode);
}
}
/**
* The current keyCode the combo is waiting for.
*
* @name Phaser.Input.Keyboard.KeyCombo#current
* @type {number}
* @since 3.0.0
*/
this.current = this.keyCodes[0];
/**
* The current index of the key being waited for in the 'keys' string.
*
* @name Phaser.Input.Keyboard.KeyCombo#index
* @type {number}
* @default 0
* @since 3.0.0
*/
this.index = 0;
/**
* The length of this combo (in keycodes)
*
* @name Phaser.Input.Keyboard.KeyCombo#size
* @type {number}
* @since 3.0.0
*/
this.size = this.keyCodes.length;
/**
* The time the previous key in the combo was matched.
*
* @name Phaser.Input.Keyboard.KeyCombo#timeLastMatched
* @type {number}
* @default 0
* @since 3.0.0
*/
this.timeLastMatched = 0;
/**
* Has this Key Combo been matched yet?
*
* @name Phaser.Input.Keyboard.KeyCombo#matched
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.matched = false;
/**
* The time the entire combo was matched.
*
* @name Phaser.Input.Keyboard.KeyCombo#timeMatched
* @type {number}
* @default 0
* @since 3.0.0
*/
this.timeMatched = 0;
/**
* If they press the wrong key do we reset the combo?
*
* @name Phaser.Input.Keyboard.KeyCombo#resetOnWrongKey
* @type {boolean}
* @default 0
* @since 3.0.0
*/
this.resetOnWrongKey = GetFastValue(config, 'resetOnWrongKey', true);
/**
* The max delay in ms between each key press. Above this the combo is reset. 0 means disabled.
*
* @name Phaser.Input.Keyboard.KeyCombo#maxKeyDelay
* @type {number}
* @default 0
* @since 3.0.0
*/
this.maxKeyDelay = GetFastValue(config, 'maxKeyDelay', 0);
/**
* If previously matched and they press the first key of the combo again, will it reset?
*
* @name Phaser.Input.Keyboard.KeyCombo#resetOnMatch
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.resetOnMatch = GetFastValue(config, 'resetOnMatch', false);
/**
* If the combo matches, will it delete itself?
*
* @name Phaser.Input.Keyboard.KeyCombo#deleteOnMatch
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.deleteOnMatch = GetFastValue(config, 'deleteOnMatch', false);
var _this = this;
var onKeyDownHandler = function (event)
{
if (_this.matched || !_this.enabled)
{
return;
}
var matched = ProcessKeyCombo(event, _this);
if (matched)
{
_this.manager.emit(Events.COMBO_MATCH, _this, event);
if (_this.resetOnMatch)
{
ResetKeyCombo(_this);
}
else if (_this.deleteOnMatch)
{
_this.destroy();
}
}
};
/**
* The internal Key Down handler.
*
* @name Phaser.Input.Keyboard.KeyCombo#onKeyDown
* @private
* @type {KeyboardKeydownCallback}
* @fires Phaser.Input.Keyboard.Events#COMBO_MATCH
* @since 3.0.0
*/
this.onKeyDown = onKeyDownHandler;
this.manager.on(Events.ANY_KEY_DOWN, this.onKeyDown);
},
/**
* How far complete is this combo? A value between 0 and 1.
*
* @name Phaser.Input.Keyboard.KeyCombo#progress
* @type {number}
* @readonly
* @since 3.0.0
*/
progress: {
get: function ()
{
return this.index / this.size;
}
},
/**
* Destroys this Key Combo and all of its references.
*
* @method Phaser.Input.Keyboard.KeyCombo#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.enabled = false;
this.keyCodes = [];
this.manager.off(Events.ANY_KEY_DOWN, this.onKeyDown);
this.manager = null;
}
});
module.exports = KeyCombo;