Skip to content

Commit

Permalink
updated keyIsDown() to work with characters as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaivaswat2244 committed Jan 18, 2025
1 parent bcbed0f commit c42bb7e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,11 +906,32 @@ function keyboard(p5, fn){
* </code>
* </div>
*/
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.
Expand Down

0 comments on commit c42bb7e

Please sign in to comment.