From c42bb7e64a5c1cbdac47a3cb284cc2701f5c0785 Mon Sep 17 00:00:00 2001 From: Vaivaswat Date: Sat, 18 Jan 2025 23:46:40 +0530 Subject: [PATCH] updated keyIsDown() to work with characters as arguments --- src/events/keyboard.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/events/keyboard.js b/src/events/keyboard.js index 8391a674b5..cfb2102f70 100644 --- a/src/events/keyboard.js +++ b/src/events/keyboard.js @@ -906,11 +906,32 @@ function keyboard(p5, fn){ * * */ - fn.keyIsDown = function(code) { - // p5._validateParameters('keyIsDown', arguments); - return this._downKeys[code] || false; + p5.prototype.keyIsDown = function(code) { + console.log('Current _downKeys:', this._downKeys); + console.log('Current key:', this.key); + + // For backward compatibility - if code is a number + if (typeof code === 'number') { + return this._downKeys[code] || false; + } + + // For string inputs (new functionality) + if (typeof code === 'string') { + // Handle single character inputs + if (code.length === 1) { + if (/[A-Za-z]/.test(code)) { + // For letters, we need to check the actual key value + return this.key === code; + } else if (/[0-9]/.test(code)) { + return this._downKeys[`Digit${code}`] || false; + } + } + // Handle direct code inputs (e.g., 'KeyA', 'ArrowLeft', etc.) + return this._downKeys[code] || false; + } + + return false; }; - /** * The _areDownKeys function returns a boolean true if any keys pressed * and a false if no keys are currently pressed.