Skip to content

Commit c42bb7e

Browse files
committed
updated keyIsDown() to work with characters as arguments
1 parent bcbed0f commit c42bb7e

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/events/keyboard.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,32 @@ function keyboard(p5, fn){
906906
* </code>
907907
* </div>
908908
*/
909-
fn.keyIsDown = function(code) {
910-
// p5._validateParameters('keyIsDown', arguments);
911-
return this._downKeys[code] || false;
909+
p5.prototype.keyIsDown = function(code) {
910+
console.log('Current _downKeys:', this._downKeys);
911+
console.log('Current key:', this.key);
912+
913+
// For backward compatibility - if code is a number
914+
if (typeof code === 'number') {
915+
return this._downKeys[code] || false;
916+
}
917+
918+
// For string inputs (new functionality)
919+
if (typeof code === 'string') {
920+
// Handle single character inputs
921+
if (code.length === 1) {
922+
if (/[A-Za-z]/.test(code)) {
923+
// For letters, we need to check the actual key value
924+
return this.key === code;
925+
} else if (/[0-9]/.test(code)) {
926+
return this._downKeys[`Digit${code}`] || false;
927+
}
928+
}
929+
// Handle direct code inputs (e.g., 'KeyA', 'ArrowLeft', etc.)
930+
return this._downKeys[code] || false;
931+
}
932+
933+
return false;
912934
};
913-
914935
/**
915936
* The _areDownKeys function returns a boolean true if any keys pressed
916937
* and a false if no keys are currently pressed.

0 commit comments

Comments
 (0)