@@ -97,8 +97,7 @@ 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
+ fn . code = null ;
102
101
103
102
/**
104
103
* A `String` system variable that contains the value of the last key typed.
@@ -442,16 +441,16 @@ function keyboard(p5, fn){
442
441
* </div>
443
442
*/
444
443
fn . _onkeydown = function ( e ) {
445
- this . _code = e . code ;
446
- // Check for repeat key events
447
444
if ( this . _downKeys [ e . code ] ) {
448
445
return ;
449
446
}
450
447
this . isKeyPressed = true ;
451
448
this . keyIsPressed = true ;
452
449
this . keyCode = e . which ;
453
450
this . key = e . key ;
454
- this . _downKeys [ e . code ] = true ;
451
+ this . code = e . code ;
452
+ this . _downKeyCodes [ e . code ] = true ;
453
+ this . _downKeys [ e . key ] = true ;
455
454
const context = this . _isGlobal ? window : this ;
456
455
if ( typeof context . keyPressed === 'function' && ! e . charCode ) {
457
456
const executeDefault = context . keyPressed ( e ) ;
@@ -617,17 +616,18 @@ function keyboard(p5, fn){
617
616
* </div>
618
617
*/
619
618
fn . _onkeyup = function ( e ) {
620
- delete this . _downKeys [ e . code ] ;
619
+ delete this . _downKeyCodes [ e . code ] ;
620
+ delete this . _downKeys [ e . key ] ;
621
621
622
622
if ( Object . keys ( this . _downKeys ) . length === 0 ) {
623
623
this . isKeyPressed = false ;
624
624
this . keyIsPressed = false ;
625
625
this . key = '' ;
626
- this . _code = null ;
626
+ this . code = null ;
627
627
} else {
628
628
// If other keys are still pressed, update code to the last pressed key
629
629
const lastPressedKey = Object . keys ( this . _downKeys ) . pop ( ) ;
630
- this . _code = lastPressedKey ;
630
+ this . code = lastPressedKey ;
631
631
}
632
632
633
633
const context = this . _isGlobal ? window : this ;
@@ -814,7 +814,7 @@ function keyboard(p5, fn){
814
814
* <a href="https://keycode.info" target="_blank">keycode.info</a>.
815
815
*
816
816
* @method keyIsDown
817
- * @param {Number } code key to check.
817
+ * @param {Number || String } code key to check.
818
818
* @return {Boolean } whether the key is down or not.
819
819
*
820
820
* @example
@@ -906,32 +906,27 @@ function keyboard(p5, fn){
906
906
* </code>
907
907
* </div>
908
908
*/
909
- p5 . prototype . keyIsDown = function ( code ) {
910
- console . log ( 'Current _downKeys:' , this . _downKeys ) ;
911
- console . log ( 'Current key:' , this . key ) ;
909
+ function isCode ( input ) {
910
+ if ( typeof input !== 'string' ) {
911
+ return false ;
912
+ }
912
913
913
- // For backward compatibility - if code is a number
914
- if ( typeof code === 'number' ) {
915
- return this . _downKeys [ code ] || false ;
914
+ // If it's a single digit, it should be treated as a code (with "Digit" prefix)
915
+ if ( input . length === 1 && / [ 0 - 9 ] / . test ( input ) ) {
916
+ return true ;
916
917
}
917
918
918
- // For string inputs (new functionality)
919
- if ( typeof code === 'string' ) {
920
- // Handle single character inputs
921
- if ( code . length === 1 ) {
922
- if ( / [ A - Z a - z ] / . test ( code ) ) {
923
- // For letters, we need to check the actual key value
924
- return this . key === code ;
925
- } else if ( / [ 0 - 9 ] / . test ( code ) ) {
926
- return this . _downKeys [ `Digit${ code } ` ] || false ;
927
- }
928
- }
929
- // Handle direct code inputs (e.g., 'KeyA', 'ArrowLeft', etc.)
930
- return this . _downKeys [ code ] || false ;
919
+ // If it's longer than 1 character, it's a code
920
+ return input . length > 1 ;
921
+ }
922
+ fn . keyIsDown = function ( input ) {
923
+ if ( isCode ( input ) ) {
924
+ const key = input . length === 1 ? `Digit${ input } ` : input ;
925
+ return this . _downKeyCodes [ key ] || this . _downKeys [ key ] ;
926
+ } else {
927
+ return this . _downKeys [ input ] || this . _downKeyCodes [ input ] ;
931
928
}
932
-
933
- return false ;
934
- } ;
929
+ }
935
930
/**
936
931
* The _areDownKeys function returns a boolean true if any keys pressed
937
932
* and a false if no keys are currently pressed.
0 commit comments