Skip to content

Commit 88f47e2

Browse files
committed
Keyboard now uses a new internal method getKeyCode to normalize the key code value based on browser support. It first checks for event.key, then event.keyIdentifier and finally event.keyCode (thanks @SVasilev phaserjs#2542)
1 parent 2ac594c commit 88f47e2

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
385385
* Group.addAt has been refactored to be a simple call to `Group.add`, removing lots of duplicate code in the process.
386386
* Group.create has a new optional argument `index` which controls the index within the group to insert the child to. Where 0 is the bottom of the Group. It also now makes proper use of `Group.add`, cutting down on more duplicate code.
387387
* Group.createMultiple now returns an Array containing references to all of the children that the method created.
388+
* Keyboard now uses a new internal method `getKeyCode` to normalize the key code value based on browser support. It first checks for `event.key`, then `event.keyIdentifier` and finally `event.keyCode` (thanks @SVasilev #2542)
388389

389390
### Bug Fixes
390391

src/input/Keyboard.js

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,37 @@ Phaser.Keyboard.prototype = {
360360

361361
},
362362

363+
/**
364+
* Normalises the keyCode value from the browser and returns it.
365+
*
366+
* This is based on browser support. It first checks for `event.key`, then `event.keyIdentifier`
367+
* and finally `event.keyCode`
368+
*
369+
* @method Phaser.Keyboard#getKeyCode
370+
* @param {KeyboardEvent} event
371+
* @private
372+
*/
373+
getKeyCode: function (event) {
374+
375+
if (event.key !== undefined)
376+
{
377+
return event.key;
378+
}
379+
else if (event.keyIdentifier !== undefined)
380+
{
381+
return event.keyIdentifier;
382+
}
383+
else if (event.keyCode !== undefined)
384+
{
385+
return event.keyCode;
386+
}
387+
else
388+
{
389+
return 0;
390+
}
391+
392+
},
393+
363394
/**
364395
* Process the keydown event.
365396
*
@@ -376,20 +407,22 @@ Phaser.Keyboard.prototype = {
376407
return;
377408
}
378409

410+
var key = this.getKeyCode();
411+
379412
// The event is being captured but another hotkey may need it
380-
if (this._capture[event.keyCode])
413+
if (this._capture[key])
381414
{
382415
event.preventDefault();
383416
}
384417

385-
if (!this._keys[event.keyCode])
418+
if (!this._keys[key])
386419
{
387-
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
420+
this._keys[key] = new Phaser.Key(this.game, key);
388421
}
389422

390-
this._keys[event.keyCode].processKeyDown(event);
423+
this._keys[key].processKeyDown(event);
391424

392-
this._k = event.keyCode;
425+
this._k = key;
393426

394427
if (this.onDownCallback)
395428
{
@@ -437,17 +470,19 @@ Phaser.Keyboard.prototype = {
437470
return;
438471
}
439472

440-
if (this._capture[event.keyCode])
473+
var key = this.getKeyCode();
474+
475+
if (this._capture[key])
441476
{
442477
event.preventDefault();
443478
}
444479

445-
if (!this._keys[event.keyCode])
480+
if (!this._keys[key])
446481
{
447-
this._keys[event.keyCode] = new Phaser.Key(this.game, event.keyCode);
482+
this._keys[key] = new Phaser.Key(this.game, key);
448483
}
449484

450-
this._keys[event.keyCode].processKeyUp(event);
485+
this._keys[key].processKeyUp(event);
451486

452487
if (this.onUpCallback)
453488
{

0 commit comments

Comments
 (0)