@@ -97,6 +97,8 @@ function keyboard(p5, fn){
97
97
*/
98
98
fn . isKeyPressed = false ;
99
99
fn . keyIsPressed = false ; // khan
100
+ fn . _code = null ;
101
+ fn . key = '' ;
100
102
101
103
/**
102
104
* A `String` system variable that contains the value of the last key typed.
@@ -440,14 +442,14 @@ function keyboard(p5, fn){
440
442
* </div>
441
443
*/
442
444
fn . _onkeydown = function ( e ) {
445
+ this . _code = e . code ;
446
+ // Check for repeat key events
443
447
if ( this . _downKeys [ e . code ] ) {
444
- // prevent multiple firings
445
448
return ;
446
449
}
447
450
this . isKeyPressed = true ;
448
451
this . keyIsPressed = true ;
449
452
this . keyCode = e . which ;
450
- this . _code = e . code ;
451
453
this . key = e . key ;
452
454
this . _downKeys [ e . code ] = true ;
453
455
const context = this . _isGlobal ? window : this ;
@@ -615,18 +617,19 @@ function keyboard(p5, fn){
615
617
* </div>
616
618
*/
617
619
fn . _onkeyup = function ( e ) {
618
- this . _downKeys [ e . which ] = false ;
620
+ delete this . _downKeys [ e . code ] ;
619
621
620
- if ( ! this . _areDownKeys ( ) ) {
622
+ if ( Object . keys ( this . _downKeys ) . length === 0 ) {
621
623
this . isKeyPressed = false ;
622
624
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 ;
623
631
}
624
632
625
- this . _lastKeyCodeTyped = null ;
626
-
627
- this . key = e . key || String . fromCharCode ( e . which ) || e . which ;
628
- this . keyCode = e . which ;
629
-
630
633
const context = this . _isGlobal ? window : this ;
631
634
if ( typeof context . keyReleased === 'function' ) {
632
635
const executeDefault = context . keyReleased ( e ) ;
0 commit comments