p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

[p5.js 2.0+ Bug Report]: Keys stay "down" after the window loses focus because _onblur clears only one of the two key maps

Open vedant21-oss opened this issue 1 month ago • 1 comments

Most appropriate sub-area of p5.js?

  • Events

p5.js version

2.3.2 (verified on main @ 94fb07d)

Web browser and version

All — the blur handler and both key maps are browser-independent.

Operating system

All

Steps to reproduce this

Steps:

  1. Run the sketch below.
  2. Hold down W.
  3. While still holding it, switch to another tab or window — this fires blur.
  4. Release W while the sketch is in the background.
  5. Switch back to the sketch. It still reports keyIsDown('KeyW') === true and keyIsPressed === true, and keeps reporting that until W is pressed and released again.

Snippet:

function setup() {
  createCanvas(240, 100);
  textSize(14);
}

function draw() {
  background(220);
  text(`keyIsDown('KeyW'): ${keyIsDown('KeyW')}`, 10, 40);
  text(`keyIsPressed: ${keyIsPressed}`, 10, 65);
}

Cause

p5 keeps two maps of currently-pressed keys:

  • this._downKeys, keyed by KeyboardEvent.key ("w")
  • this._downKeyCodes, keyed by KeyboardEvent.code ("KeyW")

Both are written on keydown:

// src/events/keyboard.js:615
this._downKeyCodes[e.code] = true;
this._downKeys[e.key] = true;

But the blur handler clears only the first one:

// src/events/keyboard.js:941
fn._onblur = function (e) {
  this._downKeys = {};
};

keyIsDown() reads both, and for any multi-character identifier — isCode() returns true for "KeyW", "ArrowLeft", "Space" — it checks _downKeyCodes first:

fn.keyIsDown = function (input) {
  if (isCode(input)) {
    return this._downKeyCodes[input] || this._downKeys[input] || false;
  } else {
    return this._downKeys[input] || this._downKeyCodes[input] || false;
  }
};

So the stale _downKeyCodes entry wins. keyIsPressed, key and code are left stale for the same reason: _areDownKeys() only ever inspects _downKeys, so the reset path in _onkeyup never runs for the abandoned key either.

This is the "character that won't stop walking after you alt-tab" bug, which is precisely what the blur listener was added to prevent.

Expected behaviour

Losing focus should clear all key state. No keyup will ever arrive for a key released while the page is in the background, so there is no other opportunity to clear it.

Suggested fix

 fn._onblur = function (e) {
   this._downKeys = {};
+  this._downKeyCodes = {};
+  this.keyIsPressed = false;
+  this.key = '';
+  this.code = '';
 };

Possibly related

p5.Graphics._initializeInstanceVariables() (src/core/p5.Graphics.js:572) initialises _downKeys but not _downKeyCodes, while the p5 copy at src/core/main.js:446 initialises both. So the two maps are inconsistent there as well. Happy to file that separately if you'd prefer to keep this issue narrow.

I'd be glad to open a PR for the _onblur fix with a test that dispatches keydown → blur and asserts keyIsDown() goes false.

vedant21-oss avatar Oct 04 '26 12:10 vedant21-oss

Closing as a duplicate of #8540, which reports the same _onblur problem with the same reproduction. Apologies, I missed it when searching before filing.

vedant21-oss avatar Oct 04 '26 13:10 vedant21-oss