feat: added keyboard compatibility shim for p5.js v1.x -> v2.x#34
Open
kunstewi wants to merge 7 commits intoprocessing:mainfrom
Open
feat: added keyboard compatibility shim for p5.js v1.x -> v2.x#34kunstewi wants to merge 7 commits intoprocessing:mainfrom
kunstewi wants to merge 7 commits intoprocessing:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.