@@ -97,8 +97,7 @@ function keyboard(p5, fn){
9797 */
9898 fn . isKeyPressed = false ;
9999 fn . keyIsPressed = false ; // khan
100- fn . _code = null ;
101- fn . key = '' ;
100+ fn . code = null ;
102101
103102 /**
104103 * A `String` system variable that contains the value of the last key typed.
@@ -442,16 +441,16 @@ function keyboard(p5, fn){
442441 * </div>
443442 */
444443 fn . _onkeydown = function ( e ) {
445- this . _code = e . code ;
446- // Check for repeat key events
447444 if ( this . _downKeys [ e . code ] ) {
448445 return ;
449446 }
450447 this . isKeyPressed = true ;
451448 this . keyIsPressed = true ;
452449 this . keyCode = e . which ;
453450 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 ;
455454 const context = this . _isGlobal ? window : this ;
456455 if ( typeof context . keyPressed === 'function' && ! e . charCode ) {
457456 const executeDefault = context . keyPressed ( e ) ;
@@ -617,17 +616,18 @@ function keyboard(p5, fn){
617616 * </div>
618617 */
619618 fn . _onkeyup = function ( e ) {
620- delete this . _downKeys [ e . code ] ;
619+ delete this . _downKeyCodes [ e . code ] ;
620+ delete this . _downKeys [ e . key ] ;
621621
622622 if ( Object . keys ( this . _downKeys ) . length === 0 ) {
623623 this . isKeyPressed = false ;
624624 this . keyIsPressed = false ;
625625 this . key = '' ;
626- this . _code = null ;
626+ this . code = null ;
627627 } else {
628628 // If other keys are still pressed, update code to the last pressed key
629629 const lastPressedKey = Object . keys ( this . _downKeys ) . pop ( ) ;
630- this . _code = lastPressedKey ;
630+ this . code = lastPressedKey ;
631631 }
632632
633633 const context = this . _isGlobal ? window : this ;
@@ -814,7 +814,7 @@ function keyboard(p5, fn){
814814 * <a href="https://keycode.info" target="_blank">keycode.info</a>.
815815 *
816816 * @method keyIsDown
817- * @param {Number } code key to check.
817+ * @param {Number || String } code key to check.
818818 * @return {Boolean } whether the key is down or not.
819819 *
820820 * @example
@@ -906,32 +906,27 @@ function keyboard(p5, fn){
906906 * </code>
907907 * </div>
908908 */
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+ }
912913
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 ;
916917 }
917918
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 ] ;
931928 }
932-
933- return false ;
934- } ;
929+ }
935930 /**
936931 * The _areDownKeys function returns a boolean true if any keys pressed
937932 * and a false if no keys are currently pressed.
0 commit comments