Skip to content

Commit 2810396

Browse files
committed
The KeyboardPlugin will now track the key code and timestamp of the previous key pressed and compare it to the current event. If they match, it will skip the event. On some systems if you were to type quickly, you would sometimes get duplicate key events firing (the exact same event firing more than once). This is now prevented from happening.
1 parent c400084 commit 2810396

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

src/input/keyboard/KeyboardPlugin.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,24 @@ var KeyboardPlugin = new Class({
146146
*/
147147
this.combos = [];
148148

149-
this.prevCode = 0;
149+
/**
150+
* Internal repeat key flag.
151+
*
152+
* @name Phaser.Input.Keyboard.KeyboardPlugin#prevCode
153+
* @type {string}
154+
* @private
155+
* @since 3.50.0
156+
*/
157+
this.prevCode = null;
158+
159+
/**
160+
* Internal repeat key flag.
161+
*
162+
* @name Phaser.Input.Keyboard.KeyboardPlugin#prevTime
163+
* @type {number}
164+
* @private
165+
* @since 3.50.0
166+
*/
150167
this.prevTime = 0;
151168

152169
sceneInputPlugin.pluginEvents.once(InputEvents.BOOT, this.boot, this);
@@ -725,6 +742,16 @@ var KeyboardPlugin = new Class({
725742
continue;
726743
}
727744

745+
// Duplicate event bailout
746+
if (code === this.prevCode && event.timeStamp === this.prevTime)
747+
{
748+
// On some systems, the exact same event will fire multiple times. This prevents it.
749+
continue;
750+
}
751+
752+
this.prevCode = code;
753+
this.prevTime = event.timeStamp;
754+
728755
if (event.type === 'keydown')
729756
{
730757
// Key specific callback first

0 commit comments

Comments
 (0)