Skip to content

Commit c847f34

Browse files
committed
addKey and removeKey can take Key objects now. Plus finished the docs.
1 parent 699951b commit c847f34

1 file changed

Lines changed: 80 additions & 57 deletions

File tree

src/input/keyboard/KeyboardManager.js

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -312,111 +312,133 @@ var KeyboardManager = new Class({
312312
},
313313

314314
/**
315-
* If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
316-
* The Key object can then be polled, have events attached to it, etc.
315+
* Adds a Key object to the Keyboard Manager.
316+
*
317+
* The given argument can be either an existing Key object, a string, such as `A` or `SPACE`, or a key code value.
318+
*
319+
* If a Key object is given, and one already exists matching the same key code, the existing one is replaced with the new one.
317320
*
318321
* @method Phaser.Input.Keyboard.KeyboardManager#addKey
319322
* @since 3.0.0
320323
*
321-
* @param {(string|integer)} keyCode - [description]
324+
* @param {(Phaser.Input.Keyboard.Key|string|integer)} key - Either a Key object, a string, such as `A` or `SPACE`, or a key code value.
322325
*
323-
* @return {Phaser.Input.Keyboard.Key} [description]
326+
* @return {Phaser.Input.Keyboard.Key} The newly created Key object, or a reference to it if it already existed in the keys array.
324327
*/
325-
addKey: function (keyCode)
328+
addKey: function (key)
326329
{
327330
var keys = this.keys;
328331

329-
if (typeof keyCode === 'string')
332+
if (key instanceof Key)
333+
{
334+
var idx = keys.indexOf(key);
335+
336+
if (idx > -1)
337+
{
338+
keys[idx] = key;
339+
}
340+
else
341+
{
342+
keys[key.keyCode] = key;
343+
}
344+
345+
return key;
346+
}
347+
348+
if (typeof key === 'string')
330349
{
331-
keyCode = KeyCodes[keyCode.toUpperCase()];
350+
key = KeyCodes[key.toUpperCase()];
332351
}
333352

334-
if (!keys[keyCode])
353+
if (!keys[key])
335354
{
336-
keys[keyCode] = new Key(keyCode);
355+
keys[key] = new Key(key);
337356
}
338357

339-
return keys[keyCode];
358+
return keys[key];
340359
},
341360

342361
/**
343-
* Removes a Key object from the Keyboard manager.
362+
* Removes a Key object from the Keyboard Manager.
363+
*
364+
* The given argument can be either a Key object, a string, such as `A` or `SPACE`, or a key code value.
344365
*
345366
* @method Phaser.Input.Keyboard.KeyboardManager#removeKey
346367
* @since 3.0.0
347368
*
348-
* @param {(string|integer)} keyCode - [description]
369+
* @param {(Phaser.Input.Keyboard.Key|string|integer)} key - Either a Key object, a string, such as `A` or `SPACE`, or a key code value.
349370
*/
350-
removeKey: function (keyCode)
371+
removeKey: function (key)
351372
{
352-
if (this.keys[keyCode])
353-
{
354-
this.keys[keyCode] = undefined;
355-
}
356-
},
373+
var keys = this.keys;
357374

358-
/**
359-
* [description]
360-
*
361-
* @method Phaser.Input.Keyboard.KeyboardManager#addKeyCapture
362-
* @since 3.0.0
363-
*
364-
* @param {(string|integer|string[]|integer[])} keyCodes - [description]
365-
addKeyCapture: function (keyCodes)
366-
{
367-
if (!Array.isArray(keyCodes))
375+
if (key instanceof Key)
368376
{
369-
keyCodes = [ keyCodes ];
370-
}
377+
var idx = keys.indexOf(key);
371378

372-
for (var i = 0; i < keyCodes.length; i++)
373-
{
374-
this.captures[keyCodes[i]] = true;
379+
if (idx > -1)
380+
{
381+
this.keys[idx] = undefined;
382+
}
375383
}
376-
},
377-
*/
378-
379-
/**
380-
* [description]
381-
*
382-
* @method Phaser.Input.Keyboard.KeyboardManager#removeKeyCapture
383-
* @since 3.0.0
384-
*
385-
* @param {(string|integer|string[]|integer[])} keyCodes - [description]
386-
removeKeyCapture: function (keyCodes)
387-
{
388-
if (!Array.isArray(keyCodes))
384+
else if (typeof key === 'string')
389385
{
390-
keyCodes = [ keyCodes ];
386+
key = KeyCodes[key.toUpperCase()];
391387
}
392388

393-
for (var i = 0; i < keyCodes.length; i++)
389+
if (keys[key])
394390
{
395-
this.captures[keyCodes[i]] = false;
391+
keys[key] = undefined;
396392
}
397393
},
398-
*/
399394

400395
/**
401-
* [description]
396+
* Creates a new KeyCombo.
397+
*
398+
* A KeyCombo will listen for a specific string of keys from the Keyboard, and when it receives them
399+
* it will emit a `keycombomatch` event from the Keyboard Manager.
400+
*
401+
* The keys to be listened for can be defined as:
402+
*
403+
* A string (i.e. 'ATARI')
404+
* An array of either integers (key codes) or strings, or a mixture of both
405+
* An array of objects (such as Key objects) with a public 'keyCode' property
406+
*
407+
* For example, to listen for the Konami code (up, up, up, down, down, down, left, left, left, right, right, right)
408+
* you could pass the following array of key codes:
409+
*
410+
* ```javascript
411+
* this.input.keyboard.createCombo([ 38, 38, 38, 40, 40, 40, 37, 37, 37, 39, 39, 39 ], { resetOnMatch: true });
412+
*
413+
* this.input.keyboard.on('keycombomatch', function (event) {
414+
* console.log('Konami Code entered!');
415+
* });
416+
* ```
417+
*
418+
* Or, to listen for the user entering the word PHASER:
419+
*
420+
* ```javascript
421+
* this.input.keyboard.createCombo('PHASER');
422+
* ```
402423
*
403424
* @method Phaser.Input.Keyboard.KeyboardManager#createCombo
404425
* @since 3.0.0
405426
*
406-
* @param {(string|integer[]|object[])} keys - [description]
407-
* @param {KeyComboConfig} config - [description]
427+
* @param {(string|integer[]|object[])} keys - The keys that comprise this combo.
428+
* @param {KeyComboConfig} [config] - A Key Combo configuration object.
408429
*
409-
* @return {Phaser.Input.Keyboard.KeyCombo} [description]
430+
* @return {Phaser.Input.Keyboard.KeyCombo} The new KeyCombo object.
410431
*/
411432
createCombo: function (keys, config)
412433
{
413434
return new KeyCombo(this, keys, config);
414435
},
415436

416437
/**
417-
* [description]
438+
* Internal update handler called by the Input Manager, which is in turn invoked by the Game step.
418439
*
419440
* @method Phaser.Input.Keyboard.KeyboardManager#update
441+
* @private
420442
* @since 3.0.0
421443
*/
422444
update: function ()
@@ -471,7 +493,8 @@ var KeyboardManager = new Class({
471493
},
472494

473495
/**
474-
* [description]
496+
* Shuts the Keyboard Manager down.
497+
* All this does is remove any listeners bound to it.
475498
*
476499
* @method Phaser.Input.Keyboard.KeyboardManager#shutdown
477500
* @since 3.0.0
@@ -482,7 +505,7 @@ var KeyboardManager = new Class({
482505
},
483506

484507
/**
485-
* Destroys this Keyboard Manager instance.
508+
* Destroys this Keyboard Manager instance and all references it holds, plus clears out local arrays.
486509
*
487510
* @method Phaser.Input.Keyboard.KeyboardManager#destroy
488511
* @since 3.0.0

0 commit comments

Comments
 (0)