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

fixed repeated key presses when a modifier key is held #7435

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/events/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,25 @@ p5.prototype.keyCode = 0;
* </div>
*/
p5.prototype._onkeydown = function(e) {
if (this._downKeys[e.which]) {
// prevent multiple firings
if (e.repeat) {
// Ignore repeated key events when holding down a key
return;
}

this._setProperty('isKeyPressed', true);
this._setProperty('keyIsPressed', true);
this._setProperty('keyCode', e.which);
this._downKeys[e.which] = true;
this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);

// Track keys pressed with meta key
if (e.metaKey) {
if (!this._metaKeys) {
this._metaKeys = [];
}
this._metaKeys.push(e.which);
}

const context = this._isGlobal ? window : this;
if (typeof context.keyPressed === 'function' && !e.charCode) {
const executeDefault = context.keyPressed(e);
Expand All @@ -458,6 +468,7 @@ p5.prototype._onkeydown = function(e) {
}
}
};

/**
* A function that's called once when any key is released.
*
Expand Down Expand Up @@ -615,18 +626,21 @@ p5.prototype._onkeydown = function(e) {
* </div>
*/
p5.prototype._onkeyup = function(e) {
this._setProperty('isKeyPressed', false);
this._setProperty('keyIsPressed', false);
this._setProperty('_lastKeyCodePressed', this._keyCode);
this._downKeys[e.which] = false;

if (!this._areDownKeys()) {
this._setProperty('isKeyPressed', false);
this._setProperty('keyIsPressed', false);
if (e.which === 91 || e.which === 93) { // Meta key codes
Copy link
Contributor

Choose a reason for hiding this comment

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

On my mac, the left meta key generates code 224, so this branch never gets executed. Is there a reason for using e.which over checking if e.key === 'Meta'?

Copy link
Author

Choose a reason for hiding this comment

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

no their wasn't any specific reason for using e.which, really sorry I have changed it to check for e.key === 'Meta' for consistency accross different platforms

// When meta key is released, clear all keys pressed with it
if (this._metaKeys) {
this._metaKeys.forEach(key => {
this._downKeys[key] = false;
});
this._metaKeys = [];
}
}

this._setProperty('_lastKeyCodeTyped', null);

this._setProperty('key', e.key || String.fromCharCode(e.which) || e.which);
this._setProperty('keyCode', e.which);

const context = this._isGlobal ? window : this;
if (typeof context.keyReleased === 'function') {
const executeDefault = context.keyReleased(e);
Expand Down
Loading