From c1c553d42d53ea2c87e89a75800e2620ed0d7e6e Mon Sep 17 00:00:00 2001 From: Rishab Kumar Jha Date: Thu, 19 Dec 2024 21:19:58 +0530 Subject: [PATCH 1/2] fixed repeated key issue --- src/events/keyboard.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/events/keyboard.js b/src/events/keyboard.js index 9e336022a9..34b1f231b9 100644 --- a/src/events/keyboard.js +++ b/src/events/keyboard.js @@ -441,15 +441,25 @@ p5.prototype.keyCode = 0; * */ p5.prototype._onkeydown = function(e) { - if (this._downKeys[e.which]) { - // prevent multiple firings + if (e.repeat) { + // Ignore repeated key events when holding down a key return; } + this._setProperty('isKeyPressed', true); this._setProperty('keyIsPressed', true); this._setProperty('keyCode', e.which); this._downKeys[e.which] = true; this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which); + + // Track keys pressed with meta key + if (e.metaKey) { + if (!this._metaKeys) { + this._metaKeys = []; + } + this._metaKeys.push(e.which); + } + const context = this._isGlobal ? window : this; if (typeof context.keyPressed === 'function' && !e.charCode) { const executeDefault = context.keyPressed(e); @@ -458,6 +468,7 @@ p5.prototype._onkeydown = function(e) { } } }; + /** * A function that's called once when any key is released. * @@ -615,18 +626,21 @@ p5.prototype._onkeydown = function(e) { * */ p5.prototype._onkeyup = function(e) { + this._setProperty('isKeyPressed', false); + this._setProperty('keyIsPressed', false); + this._setProperty('_lastKeyCodePressed', this._keyCode); this._downKeys[e.which] = false; - if (!this._areDownKeys()) { - this._setProperty('isKeyPressed', false); - this._setProperty('keyIsPressed', false); + if (e.which === 91 || e.which === 93) { // Meta key codes + // When meta key is released, clear all keys pressed with it + if (this._metaKeys) { + this._metaKeys.forEach(key => { + this._downKeys[key] = false; + }); + this._metaKeys = []; + } } - this._setProperty('_lastKeyCodeTyped', null); - - this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which); - this._setProperty('keyCode', e.which); - const context = this._isGlobal ? window : this; if (typeof context.keyReleased === 'function') { const executeDefault = context.keyReleased(e); From 2ed379332ec6acad31e21fab7d8c7293db45f3e4 Mon Sep 17 00:00:00 2001 From: Rishab Kumar Jha Date: Fri, 20 Dec 2024 11:13:34 +0530 Subject: [PATCH 2/2] minor change --- src/events/keyboard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events/keyboard.js b/src/events/keyboard.js index 34b1f231b9..f6e3b04703 100644 --- a/src/events/keyboard.js +++ b/src/events/keyboard.js @@ -631,7 +631,7 @@ p5.prototype._onkeyup = function(e) { this._setProperty('_lastKeyCodePressed', this._keyCode); this._downKeys[e.which] = false; - if (e.which === 91 || e.which === 93) { // Meta key codes + if (e.key === 'Meta') { // Meta key codes // When meta key is released, clear all keys pressed with it if (this._metaKeys) { this._metaKeys.forEach(key => {