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
18 changes: 18 additions & 0 deletions src/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
* @requires core
*/
export function isCode(input) {
const leftRightKeys = [
'AltLeft', 'AltRight',
'ShiftLeft', 'ShiftRight',
'ControlLeft', 'ControlRight',
'MetaLeft', 'MetaRight',
];
if (leftRightKeys.includes(input)) {
return false;
}
if (typeof input !== 'string') {
return false;
}
Expand Down Expand Up @@ -916,6 +925,15 @@ function keyboard(p5, fn){
* </div>
*/
function isCode(input) {
const leftRightKeys = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that we're defining isCode twice -- I think we can remove this one, and just use the exported one above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, sure.

'AltLeft', 'AltRight',
'ShiftLeft', 'ShiftRight',
'ControlLeft', 'ControlRight',
'MetaLeft', 'MetaRight',
];
if (leftRightKeys.includes(input)) {
return false;
}
if (typeof input !== 'string') {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions test/unit/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ suite('Keyboard Events', function() {
assert.isTrue(isCode('Control'));
assert.isTrue(isCode('ab'));
});

test('returns false for strings for letright keys', function() {
assert.isFalse(isCode('AltLeft'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want AltLeft to return true actually, and just Alt to return false. I'm testing out the demo on MDN here: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code#try_it_out

Hitting the alt key, it outputs: KeyboardEvent: key='Alt' | code='AltLeft'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed this, returning false for keys and true for codes

assert.isFalse(isCode('ShiftRight'));
});

test('handles edge cases correctly', function() {
assert.isFalse(isCode('')); // empty string
Expand Down