Skip to content

Commit 4443f7c

Browse files
authored
Merge pull request #7435 from Rishab87/repeated-key-presses
fixed repeated key presses when a modifier key is held
2 parents 69580cf + 2ed3793 commit 4443f7c

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

Diff for: src/events/keyboard.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -441,15 +441,25 @@ p5.prototype.keyCode = 0;
441441
* </div>
442442
*/
443443
p5.prototype._onkeydown = function(e) {
444-
if (this._downKeys[e.which]) {
445-
// prevent multiple firings
444+
if (e.repeat) {
445+
// Ignore repeated key events when holding down a key
446446
return;
447447
}
448+
448449
this._setProperty('isKeyPressed', true);
449450
this._setProperty('keyIsPressed', true);
450451
this._setProperty('keyCode', e.which);
451452
this._downKeys[e.which] = true;
452453
this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);
454+
455+
// Track keys pressed with meta key
456+
if (e.metaKey) {
457+
if (!this._metaKeys) {
458+
this._metaKeys = [];
459+
}
460+
this._metaKeys.push(e.which);
461+
}
462+
453463
const context = this._isGlobal ? window : this;
454464
if (typeof context.keyPressed === 'function' && !e.charCode) {
455465
const executeDefault = context.keyPressed(e);
@@ -458,6 +468,7 @@ p5.prototype._onkeydown = function(e) {
458468
}
459469
}
460470
};
471+
461472
/**
462473
* A function that's called once when any key is released.
463474
*
@@ -615,18 +626,21 @@ p5.prototype._onkeydown = function(e) {
615626
* </div>
616627
*/
617628
p5.prototype._onkeyup = function(e) {
629+
this._setProperty('isKeyPressed', false);
630+
this._setProperty('keyIsPressed', false);
631+
this._setProperty('_lastKeyCodePressed', this._keyCode);
618632
this._downKeys[e.which] = false;
619633

620-
if (!this._areDownKeys()) {
621-
this._setProperty('isKeyPressed', false);
622-
this._setProperty('keyIsPressed', false);
634+
if (e.key === 'Meta') { // Meta key codes
635+
// When meta key is released, clear all keys pressed with it
636+
if (this._metaKeys) {
637+
this._metaKeys.forEach(key => {
638+
this._downKeys[key] = false;
639+
});
640+
this._metaKeys = [];
641+
}
623642
}
624643

625-
this._setProperty('_lastKeyCodeTyped', null);
626-
627-
this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);
628-
this._setProperty('keyCode', e.which);
629-
630644
const context = this._isGlobal ? window : this;
631645
if (typeof context.keyReleased === 'function') {
632646
const executeDefault = context.keyReleased(e);

0 commit comments

Comments
 (0)