[p5.js 2.0 Bug Report]: [p5.js 2.0 Bug Report]: _onblur doesn't fully reset keyboard state , keyIsPressed and keyIsDown() stay stuck after losing focus
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 24k
- Forks
- 3.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 25
Description
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.x (main)
Web browser and version
Any
Operating system
Windows 11, but doesn't matter
Steps to reproduce this
Steps:
- Open a sketch with keyboard input (see snippet below)
- Click the canvas, then hold down the right arrow key
- While still holding it, Alt-Tab to another window
- Release the key in the other window
- Switch back to the sketch tab
Snippet:
function setup() {
createCanvas(400, 200);
}
function draw() {
background(220);
textSize(16);
text('keyIsPressed: ' + keyIsPressed, 20, 40);
text('RIGHT_ARROW down: ' + keyIsDown(RIGHT_ARROW), 20, 80);
}
After step 5, both keyIsPressed and keyIsDown(RIGHT_ARROW) are still true even though no key is being held. The key stays "stuck" until you physically press and release it again inside the sketch. Super annoying in any movement-based sketch
Root cause:
_onblur in keyboard.js only clears _downKeys but completely ignores _downKeyCodes, keyIsPressed, key, and code:
// what's there now
fn._onblur = function(e) {
this._downKeys = {};
};
_downKeyCodes is new to 2.x _onblur was just never updated when it got added. Since keyIsDown() checks both maps, clearing only one of them doesn't actually fix the stuck state.,
Fix would be:
fn._onblur = function(e) {
this._downKeys = {};
this._downKeyCodes = {};
this.keyIsPressed = false;
this.key = '';
this.code = '';
};
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in keyboard.js at the _onblur handler and trace the state used by keyIsPressed and keyIsDown(). Reproduce the Alt-Tab scenario with the provided sketch, then verify that losing focus no longer leaves keyboard state stuck and that the relevant keyboard behavior remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100