Skip to content

Commit bcbed0f

Browse files
committed
fixed the repeat key issue in onkeyup
1 parent dd458c0 commit bcbed0f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/events/keyboard.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ function keyboard(p5, fn){
9797
*/
9898
fn.isKeyPressed = false;
9999
fn.keyIsPressed = false; // khan
100+
fn._code = null;
101+
fn.key = '';
100102

101103
/**
102104
* A `String` system variable that contains the value of the last key typed.
@@ -440,14 +442,14 @@ function keyboard(p5, fn){
440442
* </div>
441443
*/
442444
fn._onkeydown = function(e) {
445+
this._code = e.code;
446+
// Check for repeat key events
443447
if (this._downKeys[e.code]) {
444-
// prevent multiple firings
445448
return;
446449
}
447450
this.isKeyPressed = true;
448451
this.keyIsPressed = true;
449452
this.keyCode = e.which;
450-
this._code = e.code;
451453
this.key = e.key;
452454
this._downKeys[e.code] = true;
453455
const context = this._isGlobal ? window : this;
@@ -615,18 +617,19 @@ function keyboard(p5, fn){
615617
* </div>
616618
*/
617619
fn._onkeyup = function(e) {
618-
this._downKeys[e.which] = false;
620+
delete this._downKeys[e.code];
619621

620-
if (!this._areDownKeys()) {
622+
if (Object.keys(this._downKeys).length === 0) {
621623
this.isKeyPressed = false;
622624
this.keyIsPressed = false;
625+
this.key = '';
626+
this._code = null;
627+
} else {
628+
// If other keys are still pressed, update code to the last pressed key
629+
const lastPressedKey = Object.keys(this._downKeys).pop();
630+
this._code = lastPressedKey;
623631
}
624632

625-
this._lastKeyCodeTyped = null;
626-
627-
this.key = e.key || String.fromCharCode(e.which) || e.which;
628-
this.keyCode = e.which;
629-
630633
const context = this._isGlobal ? window : this;
631634
if (typeof context.keyReleased === 'function') {
632635
const executeDefault = context.keyReleased(e);

0 commit comments

Comments
 (0)