Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced deprecated keyCode functionality and docs with KeyboardEvent.code & KeyboardEvent.key also updates the keyIsDown function to accept alphanumerics as parameters #7472

Merged
merged 12 commits into from
Feb 4, 2025
Merged
1 change: 1 addition & 0 deletions src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ class p5 {

this._styles = [];
this._downKeys = {}; //Holds the key codes of currently pressed keys
this._downKeyCodes = {};
}
}

Expand Down
57 changes: 26 additions & 31 deletions src/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ function keyboard(p5, fn){
*/
fn.isKeyPressed = false;
fn.keyIsPressed = false; // khan
fn._code = null;
fn.key = '';
fn.code = null;

/**
* A `String` system variable that contains the value of the last key typed.
Expand Down Expand Up @@ -442,16 +441,16 @@ function keyboard(p5, fn){
* </div>
*/
fn._onkeydown = function(e) {
this._code = e.code;
// Check for repeat key events
if (this._downKeys[e.code]) {
return;
}
this.isKeyPressed = true;
this.keyIsPressed = true;
this.keyCode = e.which;
this.key = e.key;
this._downKeys[e.code] = true;
this.code = e.code;
this._downKeyCodes[e.code] = true;
this._downKeys[e.key] = true;
const context = this._isGlobal ? window : this;
if (typeof context.keyPressed === 'function' && !e.charCode) {
const executeDefault = context.keyPressed(e);
Expand Down Expand Up @@ -617,17 +616,18 @@ function keyboard(p5, fn){
* </div>
*/
fn._onkeyup = function(e) {
delete this._downKeys[e.code];
delete this._downKeyCodes[e.code];
delete this._downKeys[e.key];

if (Object.keys(this._downKeys).length === 0) {
this.isKeyPressed = false;
this.keyIsPressed = false;
this.key = '';
this._code = null;
this.code = null;
} else {
// If other keys are still pressed, update code to the last pressed key
const lastPressedKey = Object.keys(this._downKeys).pop();
Vaivaswat2244 marked this conversation as resolved.
Show resolved Hide resolved
this._code = lastPressedKey;
this.code = lastPressedKey;
}

const context = this._isGlobal ? window : this;
Expand Down Expand Up @@ -814,7 +814,7 @@ function keyboard(p5, fn){
* <a href="https://keycode.info" target="_blank">keycode.info</a>.
*
* @method keyIsDown
* @param {Number} code key to check.
* @param {Number || String} code key to check.
* @return {Boolean} whether the key is down or not.
*
* @example
Expand Down Expand Up @@ -906,32 +906,27 @@ function keyboard(p5, fn){
* </code>
* </div>
*/
p5.prototype.keyIsDown = function(code) {
console.log('Current _downKeys:', this._downKeys);
console.log('Current key:', this.key);
function isCode(input) {
if (typeof input !== 'string') {
return false;
}

// For backward compatibility - if code is a number
if (typeof code === 'number') {
return this._downKeys[code] || false;
// If it's a single digit, it should be treated as a code (with "Digit" prefix)
Vaivaswat2244 marked this conversation as resolved.
Show resolved Hide resolved
if (input.length === 1 && /[0-9]/.test(input)) {
Vaivaswat2244 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

// 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;
// If it's longer than 1 character, it's a code
return input.length > 1;
}
fn.keyIsDown = function(input) {
if (isCode(input)) {
Vaivaswat2244 marked this conversation as resolved.
Show resolved Hide resolved
const key = input.length === 1 ? `Digit${input}` : input;
return this._downKeyCodes[key] || this._downKeys[key];
} else {
return this._downKeys[input] || this._downKeyCodes[input];
}

return false;
};
}
/**
* The _areDownKeys function returns a boolean true if any keys pressed
* and a false if no keys are currently pressed.
Expand Down