Skip to content

[p5.js 2.0 Bug Report]: misleading keyIsDown() documentation #7786

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

Open
1 of 17 tasks
MrDuckyTesla opened this issue Apr 29, 2025 · 10 comments
Open
1 of 17 tasks

[p5.js 2.0 Bug Report]: misleading keyIsDown() documentation #7786

MrDuckyTesla opened this issue Apr 29, 2025 · 10 comments

Comments

@MrDuckyTesla
Copy link

MrDuckyTesla commented Apr 29, 2025

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.0.1

Web browser and version

Google Chrome Version 23H2 (Build 22631.4460)

Operating system

Windows 11

Steps to reproduce this

Steps:

  1. Make new empty p5.js sketch
  2. paste "console.log(keyIsDown(88));" as shown below
  3. press down on "x" key and notice how console prints only false
  4. change version to 1.11.5
  5. run sketch again and notice how console prints true when key is down

Snippet:

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  console.log(keyIsDown(88));
}
Copy link

welcome bot commented Apr 29, 2025

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

@mudit06mah
Copy link

I believe this error is arising due to the fact that event handling has changed in 2.x

@perminder-17
Copy link
Contributor

perminder-17 commented Apr 30, 2025

I believe this error is arising due to the fact that event handling has changed in 2.x

That's correct @mudit06mah , it's a breaking change which happend on this PR: #7472

Now, you can't use any code inside keyIsDown() function, I think we need to fix the docs for it.

Thanks @MrDuckyTesla for catching this, it's another breaking change. keyIsDown, which now accepts the values of KeyboardEvent.key and KeyboardEvent.code instead of numeric codes
If you use the p5 constants like CONTROL_KEY then it works the same, but now CONTROL_KEY maps to 'Control' instead of a number.

Solution: We should fix the docs and remove the usage of code at description where it says it will work using code as well.

If anyone is interested in fixing this up, I am happy to assign to them. Thanks everyone for their inputs. 🙂

@perminder-17
Copy link
Contributor

Also, if possible @MrDuckyTesla can you please update the heading of this issue so it would be easy for everyone to know what's getting fixed? Maybe it's an error in the docs, so we could write the same as heading?

@MrDuckyTesla MrDuckyTesla changed the title [p5.js 2.0 Bug Report]: keyIsDown() function not working [p5.js 2.0 Bug Report]: keyIsDown() Apr 30, 2025
@MrDuckyTesla MrDuckyTesla changed the title [p5.js 2.0 Bug Report]: keyIsDown() [p5.js 2.0 Bug Report]: misleading keyIsDown() documentation Apr 30, 2025
@MrDuckyTesla
Copy link
Author

@perminder-17 I hope that this title is more accurate regarding the issue.

@perminder-17
Copy link
Contributor

perminder-17 commented Apr 30, 2025

@perminder-17 I hope that this title is more accurate regarding the issue.

Yes, this looks great.

@perminder-17 perminder-17 moved this to Open for Discussion in p5.js 2.x 🌱🌳 Apr 30, 2025
@perminder-17 perminder-17 moved this from Open for Discussion to Ready for Work in p5.js 2.x 🌱🌳 Apr 30, 2025
@suhas-developer07
Copy link

The reason you're seeing false rapidly in the console is because the draw() function in p5.js runs about 60 times per second. Inside it, you're calling: console.log(keyIsDown(88));
So if the 'X' key (ASCII code 88) is not being pressed, keyIsDown(88) will return false, and that value is printed continuously — creating what looks like "spamming" in the console.

💡 Suggested Fix (to reduce console spam):

Only log when the key is actually pressed:

if (keyIsDown(88)) {
console.log("X is being held down");
}
This avoids unnecessary logs and makes testing easier.

Hope this helps!

@perminder-17
Copy link
Contributor

The reason you're seeing false rapidly in the console is because the draw() function in p5.js runs about 60 times per second. Inside it, you're calling: console.log(keyIsDown(88)); So if the 'X' key (ASCII code 88) is not being pressed, keyIsDown(88) will return false, and that value is printed continuously — creating what looks like "spamming" in the console.

💡 Suggested Fix (to reduce console spam):

Only log when the key is actually pressed:

if (keyIsDown(88)) { console.log("X is being held down"); } This avoids unnecessary logs and makes testing easier.

Hope this helps!

Hi, thanks for the suggestons. I think it could be one of the reasons but only in 1.x versions of p5.js. If you do anything like

// this works with 1.x versions. in 2.x version it will console nothing. 
if (keyIsDown(88)) {
console.log("X is being held down");
}

in 2.x version, you could still see nothing in the console but if you do:

if (keyIsDown('x')) { // replace with x
console.log("X is being held down");
}

You could probably see it logs in the console.

@dpanshug
Copy link

dpanshug commented May 3, 2025

It seems the main task is to update the JSDoc for keyIsDown() in src/events/keyboard.js so the description correctly reflects that it checks if any key is pressed down, not a specific one. This should clear up the documentation confusion mentioned.

@perminder-17 Could you please assign this to me?

@perminder-17
Copy link
Contributor

It seems the main task is to update the JSDoc for keyIsDown() in src/events/keyboard.js so the description correctly reflects that it checks if any key is pressed down, not a specific one. This should clear up the documentation confusion mentioned.

@perminder-17 Could you please assign this to me?

Yes, correct. I have assigned this to you🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Ready for Work
Development

No branches or pull requests

5 participants