-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
base: dev-2.0
Are you sure you want to change the base?
Changes from 3 commits
dd458c0
bcbed0f
c42bb7e
5709aac
0ce4e89
9cfef5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,8 @@ function keyboard(p5, fn){ | |
*/ | ||
fn.isKeyPressed = false; | ||
fn.keyIsPressed = false; // khan | ||
fn._code = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is only used in this module and not used by other modules through the instance, it should be a local variable and not attached to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, currently user is able to do |
||
fn.key = ''; | ||
|
||
/** | ||
* A `String` system variable that contains the value of the last key typed. | ||
|
@@ -440,15 +442,16 @@ function keyboard(p5, fn){ | |
* </div> | ||
*/ | ||
fn._onkeydown = function(e) { | ||
if (this._downKeys[e.which]) { | ||
// prevent multiple firings | ||
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._downKeys[e.which] = true; | ||
this.key = e.key || String.fromCharCode(e.which) || e.which; | ||
this.key = e.key; | ||
this._downKeys[e.code] = true; | ||
const context = this._isGlobal ? window : this; | ||
if (typeof context.keyPressed === 'function' && !e.charCode) { | ||
const executeDefault = context.keyPressed(e); | ||
|
@@ -614,18 +617,19 @@ function keyboard(p5, fn){ | |
* </div> | ||
*/ | ||
fn._onkeyup = function(e) { | ||
this._downKeys[e.which] = false; | ||
delete this._downKeys[e.code]; | ||
|
||
if (!this._areDownKeys()) { | ||
if (Object.keys(this._downKeys).length === 0) { | ||
this.isKeyPressed = false; | ||
this.keyIsPressed = false; | ||
this.key = ''; | ||
this._code = null; | ||
} else { | ||
// If other keys are still pressed, update code to the last pressed key | ||
const lastPressedKey = Object.keys(this._downKeys).pop(); | ||
this._code = lastPressedKey; | ||
} | ||
|
||
this._lastKeyCodeTyped = null; | ||
|
||
this.key = e.key || String.fromCharCode(e.which) || e.which; | ||
this.keyCode = e.which; | ||
|
||
const context = this._isGlobal ? window : this; | ||
if (typeof context.keyReleased === 'function') { | ||
const executeDefault = context.keyReleased(e); | ||
|
@@ -902,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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should still be |
||
console.log('Current _downKeys:', this._downKeys); | ||
console.log('Current key:', this.key); | ||
|
||
// For backward compatibility - if code is a number | ||
if (typeof code === 'number') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work if our key event handlers only set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also @limzykenneth do you think we need backwards compatibility? We might need to put a lookup table like this https://stackoverflow.com/a/66180965 into our code to map old key codes to keys, although it looks like it's hard to get complete backwards compatibility. That's probably enough though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the numerical code has been deprecated as a standard, we can remove it since its the point of this implementation to use the easier |
||
return this._downKeys[code] || false; | ||
} | ||
|
||
// For string inputs (new functionality) | ||
if (typeof code === 'string') { | ||
// Handle single character inputs | ||
if (code.length === 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should also add an else if branch to handle the multi-character possibilities for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are some overlap between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here, we are using |
||
if (/[A-Za-z]/.test(code)) { | ||
// For letters, we need to check the actual key value | ||
return this.key === code; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this only checks if the last pressed key was the one passed in, rather than if that key is currently down. We might need to make two objects, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, got it |
||
} else if (/[0-9]/.test(code)) { | ||
return this._downKeys[`Digit${code}`] || false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably be consistent and check for keys if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we are currently checking with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully replacing The slight inconsistency right now is if the user passes in the string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, got it. fn.keyIsDown = function(input){
if (typeof input ==='string'){
if (input.length === 1) {
return this._downKeys[input] ||
(/[0-9]/.test(input) && this._downKeyCodes[`Digit${input}`])
|| false;
}
return this._downKeyCodes[input] || this._downKeys[input] || false;
}
return false;
}; here _downKeyCodes is storing e.code and _downKeys is storing e.key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! It might makes sense to make a fn.keyIsDown = function(input) {
if (isCode(input)) {
return this._downCodes[input];
} else {
return this._downKeys[input];
}
} ...and possibly also unit test |
||
} | ||
} | ||
// 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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why the typedef is both of these while the constant is just one?