fix: Create a Keyboard in Application - #358
Open
willeastcott wants to merge 1 commit into
Open
willeastcott wants to merge 1 commit into
willeastcott wants to merge 1 commit into
Conversation
<Application> created the mouse and touch devices but no keyboard, and `keyboard` is excluded from its props, so app.keyboard was always null and scripts and hooks that read it threw. Create one attached to the window, as the Editor launcher and <pc-app> do. It is created with the application, after the check for a mount that was torn down while the graphics device was being created, and app.destroy() detaches it. Unmounting, and the first mount that StrictMode discards, therefore leave no keyboard attached. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: f0d9602 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
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.
<Application>creates the mouse and touch devices but no keyboard, andkeyboardis one of its excluded props, soapp.keyboardis alwaysnullin a React app. The User Manual relies on it: the React tab of Moving Bodies in the Physics chapter callsapp.keyboard.isPressed(KEY_RIGHT)in auseAppEvent('update', ...)handler to push a rigid body, and throws as soon as that handler runs.This creates a
Keyboardattached towindowalong with the application, as the Editor's launcher and the Web Components<pc-app>do, so key presses register without the canvas having focus. It has the default options, so it doesn't callpreventDefault()orstopPropagation(), and typing in page inputs and keyboard scrolling are unaffected. As with<pc-app>, it also sees keys typed into page inputs, since those events bubble to the window.Unmounting needs no new cleanup:
app.destroy()already runs then, and it detaches the keyboard and setsapp.keyboardback tonull. The keyboard is created after the existing check for a mount torn down while the graphics device was being created. So StrictMode's double mount leaves one keyboard attached whichever way its first mount ends: under the tests'act()it's abandoned before creating an application, and in a browser its application is created and then destroyed along with its keyboard.Checked in Chromium with engine 2.11.8 (the lockfile's version), WebGL2 and StrictMode, using the Moving Bodies
Pushcomponent as published on a dynamic box: holding the right arrow pushes the box, key presses register while focus is on the page body rather than the canvas, and after an unmount and remount exactly one keyboard is attached. The engine'smainhandles the keyboard inAppBasethe same way.The tests are in a new file,
Application.keyboard.test.tsx, since they run against the real engine (vi.unmock('playcanvas')). The shared mock intest/setup.tsbuilds a stand-in application with its own input devices, so it never sees the keyboard the component creates. The tests check thatapp.keyboardtracks key presses onwindow, and that under StrictMode only the application's keyboard stays attached, and is detached on unmount. Both fail without the fix. The StrictMode test also fails if the keyboard is created before the graphics device is awaited.#353 (the
ElementInput) changes the sameApplicationoptions, and alsoApplication.test.tsxandtest/setup.ts. This PR avoids the lines #353 changes, so the two merge cleanly in either order:git merge-treeagainst its head (503f52e) finds no conflicts, and the combined tree passes lint and all tests, #353's new test included. With both merged, the devices are created aselementInput,mouse,touch,keyboard. The keyboard's place in that order doesn't matter, since the element input and mouse don't listen for key events.A changeset is included (patch).
🤖 Generated with Claude Code