-
-
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?
Conversation
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.
Thanks for taking this on, this is a great start!
I think we might need to keep track of two downkey objects, one for code
s and one for key
s, in order to fully support querying of both.
/** | ||
* @typedef {18} ALT | ||
* @typedef {'AltLeft' | 'AltRight'} ALT |
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?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work if our key event handlers only set _downkeys[e.code]
?
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.
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.
if (code.length === 1) { | ||
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 comment
The 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, _downKeyCodes
and _downKeys
, and update both on keydown/keyup.
// 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; |
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.
We should probably be consistent and check for keys if code.length === 1
and check for key codes otherwise.
// 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 comment
The 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 key
, for things like Enter and the arrow keys: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values If it's one of those, we would need to check against down keys as opposed to down key codes.
Resolves #7436 and #6798
Changes:
This PR modernizes keyboard event handling by transitioning from the deprecated
keyCode
property to the modernKeyboardEvent.code
andKeyboardEvent.key
properties. This change improves keyboard input reliability and brings p5.js in line with current web standards.KeyboardEvent.code
instead ofe.which
_code
propertyconstants
to use modern string valuesScreenshots of the change:
/preview/index.html test sketch is:
PR Checklist
npm run lint
passes