feat: added keyboard compatibility shim for p5.js v1.x to v2.x#34
feat: added keyboard compatibility shim for p5.js v1.x to v2.x#34kunstewi wants to merge 15 commits intoprocessing:mainfrom
Conversation
| 1. Instead of `bezierVertex(x1, y1, x2, y2, x3, y3)` and `quadraticVertex(x1, y1, x2, y2)`, use multiple `bezierVertex(x, y)` calls, and set order with `bezierOrder` _(to revert to 1.x use, include [shapes.js](https://github.com/processing/p5.js-compatibility/blob/main/src/shapes.js))_ | ||
| 2. Instead of `curveVertex`, use `splineVertex` - and expect `endShape(CLOSE)` to create a smoothly connected shape _(for 1.x usage, include [shapes.js](https://github.com/processing/p5.js-compatibility/blob/main/src/shapes.js))_ | ||
| 3. The previous usage of of `textWidth(..)` is now covered by `fontWidth(..)` in 2.x. Use `textWidth(..)` to measure text *without* space (tight bounding box). In 2.x, `fontWidth(..)` measures text width including trailing space. | ||
| 4. Instead of `keyCode === UP_ARROW` (and similar), use `keyIsDown(UP_ARROW)` (works in both versions) or `code === UP_ARROW` (works in 2.x). _(to revert to 1.x use, include [events.js](https://github.com/processing/p5.js-compatibility/blob/main/src/events.js))_ |
There was a problem hiding this comment.
Ah, hm, @kunstewi maybe this is a good signal that the backwards compatibility vars actually should be in events.js? Although initially I described in the issue that it should be data, it's true that other keyboard backwards compatibility support is already in events and should be kept consistent.
Apologies this took so long to get to. If you move the vars over that would be good, I'll do my best to test and merge asap. Feel free to @ on the discord contribute channel if needed! thanks for working on this.
There was a problem hiding this comment.
thanks Kit (@ksen0) I have added the requested changes, I have added the keyboard compatibility layer in the events.js and kept touch and mouse in the data.js as it is.
…eyboard-compatibility
…mpatibility into keyboard-compatibility merged readme update
Background
p5.js v2.x dropped several legacy keyboard APIs that many existing sketches depend on specifically the named numeric keyCode constants (e.g. LEFT_ARROW, RIGHT_ARROW) and the event.keyCode property on keyCode. This PR adds a compatibility shim inside src/data.js that transparently restores that behavior so v1.x sketches run on v2.x without any source changes.
A v1.x sketch like this:
silently breaks on v2.x because:
RIGHT_ARROW, LEFT_ARROW, etc. are no longer defined globals
keyCode no longer holds the legacy numeric value (v2.x uses string-based key/code)
keyIsDown() only accepts the new string format.
Changes Made
The overrides all delegate to the original v2.x handlers after patching, so no existing v2.x behavior is broken.
Testing
without the keyboard compatibility code
tests not passing

example code not working
https://github.com/user-attachments/assets/fd9ec047-ce15-452e-836d-97e4ea12498e
with the kyboard compatibility code
tests passing

example code working
Screen.Recording.2026-02-22.at.3.04.06.PM.mov
AI disclosure
closes #26
hey @ksen0 could you please take a look, and propose changes or let me know if i am moving in the right direction.
Thanks for your time.