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

Updated keyIsDown() to work with characters as arguments #7470

Closed
wants to merge 2 commits into from

Conversation

Vaivaswat2244
Copy link

Resolves #6798

Changes:

This PR enhances the keyIsDown() function to accept single-character strings as input, making it more intuitive and flexible to use. Previously, keyIsDown() only accepted numeric key codes. Now users can check for keys using either key codes or character strings.

Changes:

  • Added support for single-character strings in keyIsDown()
  • When a string is provided, it's converted to its corresponding key code using charCodeAt()
  • Maintains backward compatibility with existing numeric key code usage
  • Added documentation and examples showing character key usage

Screenshots of the change:

Screenshot from 2025-01-16 02-08-46

Now for this I used the following sketch for testing the updated function

const sketch = function(p) {
      p.setup = function() {
        p.createCanvas(400, 400);
        p.textSize(16);
      };

      p.draw = function() {
        p.background(200);
        let y = 30;

        // Test original keycode functionality
        p.text(`Testing keyIsDown - Press keys to see results`, 20, y);
        y += 40;

        // Test arrow keys (using constants)
        p.text(`LEFT_ARROW: ${p.keyIsDown(p.LEFT_ARROW)}`, 20, y);
        y += 25;
        p.text(`RIGHT_ARROW: ${p.keyIsDown(p.RIGHT_ARROW)}`, 20, y);
        y += 40;

        // Test string input (your new functionality)
        p.text(`'a' key: ${p.keyIsDown('a')}`, 20, y);
        y += 25;
        p.text(`'A' key: ${p.keyIsDown('A')}`, 20, y);
        y += 25;
        p.text(`'1' key: ${p.keyIsDown('1')}`, 20, y);
        y += 40;

        // Show current key info
        if (p.keyIsPressed) {
          p.text(`Current key: '${p.key}'`, 20, y);
          y += 25;
          p.text(`Current keyCode: ${p.keyCode}`, 20, y);
        }
      };
    };

PR Checklist

Copy link

welcome bot commented Jan 15, 2025

🎉 Thanks for opening this pull request! Please check out our contributing guidelines if you haven't already. And be sure to add yourself to the list of contributors on the readme page!

@davepagurek
Copy link
Contributor

Hi @Vaivaswat2244! Was this meant to be closed?

@Vaivaswat2244
Copy link
Author

Yeah, actually this does not update the keyisDown to work with Keyboard event.key or code instead this was just accepting the characters as parameter for keyisDown and returning keyCode of that to allow users to work with characters as parameters.
So I'm thinking firstly to fix #7436 then move to the keyisDown issue. I'll raise a pr for that by EOD. And once that gets done, then I'll reopen this one (if no other changes would be required) or make another one with improved logic.

@davepagurek
Copy link
Contributor

Sounds good, thanks for the update!

@Vaivaswat2244
Copy link
Author

hey @davepagurek , have raised the pr solving both the deprecation of keyCode(#7436) and update on keyIsDown issue(#6798)
Can you have a look at them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants