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

Replaced deprecated keyCode functionality and docs with KeyboardEvent.code & KeyboardEvent.key also updates the keyIsDown function to accept alphanumerics as parameters #7472

Open
wants to merge 3 commits into
base: dev-2.0
Choose a base branch
from

Conversation

Vaivaswat2244
Copy link

@Vaivaswat2244 Vaivaswat2244 commented Jan 17, 2025

Resolves #7436 and #6798

Changes:

This PR modernizes keyboard event handling by transitioning from the deprecated keyCode property to the modern KeyboardEvent.code and KeyboardEvent.key properties. This change improves keyboard input reliability and brings p5.js in line with current web standards.

  • Updated keyboard event handling to use KeyboardEvent.code instead of e.which
  • Added proper key tracking using _code property
  • Updated keyboard constants to use modern string values
  • Updated keyIsDown function to use alphanumeric as parameters

Screenshots of the change:

Screenshot from 2025-01-18 23-45-02

/preview/index.html test sketch is:

const sketch = function(p) {
      p.setup = function() {
        p.createCanvas(800, 600);
        p.textSize(16);
        p.textFont('monospace');
      };

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

        // Title
        p.fill(0);
        p.text("keyIsDown() Test", 20, y);
        y += 40;

        // Current key state
        p.text(Current key: ${p.key}, 20, y);
        y += 25;
        p.text(Current code: ${p._code}, 20, y);
        y += 25;

        // Test keyIsDown with direct codes
        p.text("Code String Tests:", 20, y);
        y += 25;
        p.text('ArrowLeft': ${p.keyIsDown('ArrowLeft')}, 40, y);
        y += 20;
        p.text('KeyA': ${p.keyIsDown('KeyA')}, 40, y);
        y += 40;

        // Test keyIsDown with single characters
        p.text("Single Character Tests:", 20, y);
        y += 25;
        p.text('a': ${p.keyIsDown('a')}, 40, y);
        y += 20;
        p.text('A': ${p.keyIsDown('A')}, 40, y);
        y += 20;
        p.text('1': ${p.keyIsDown('1')}, 40, y);
        y += 40;

        // Show current _downKeys object
        p.text("Current _downKeys object:", 20, y);
        y += 25;
        p.text(JSON.stringify(p._downKeys, null, 2), 40, y);
      };

PR Checklist

@Vaivaswat2244 Vaivaswat2244 changed the title Replaced deprecated keyCode functionality and docs with KeyboardEvent.code & KeyboardEvent.key Replaced deprecated keyCode functionality and docs with KeyboardEvent.code & KeyboardEvent.key also updates the keyIsDown function to accept alphanumerics as parameters Jan 18, 2025
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.

1 participant