Repository navigation
fix(wasm): Stuck and flickering keyboard modifier state (6.8) - #25155
agneszitte merged 8 commits into
Conversation
KeyboardStateTracker emulated the Locked flag as a per-key press parity counter. That is only equivalent to the Win32 "toggled" bit it mirrors if every physical press produces exactly one update, which no platform guarantees: routed key events are raised once per element while bubbling, so the resulting flag depended on the focused element's depth in the visual tree, and OS auto-repeat flipped it again for a key held down. Nothing reads the flag, and for Caps Lock - the one key where an app would consult it - a press counter is the wrong source anyway, since the lock can be toggled before the app starts or while it is in the background. Dropping it also makes the tracker idempotent, which removes the need to tell an auto-repeat apart from a key down whose key up was never delivered: both mean the key is down. It also fixes the drag modifiers in PointerRoutedEventArgs.GetState, which compare against Down exactly and silently dropped whenever the parity landed on Down | Locked. See unoplatform#17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit b580458)
Key downs and key ups cannot be trusted to stay paired: a modifier released while the browser or the OS holds focus - opening dev tools mid-keystroke, for instance - never produces a key up, leaving the key reported as held for the rest of the session. Every key event already carries the real state of all four modifiers alongside its own payload, so apply that in OnKey before dispatching. This is the single per-event entry point, so it runs once per physical key rather than once per element while bubbling. The key the event is about needs no special treatment: the key down/up applied while the event bubbles lands after this and wins, which also avoids relying on how a platform reports a modifier's own key up. See unoplatform#17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit a9ff5c6)
A mobile on-screen keyboard can raise Shift during auto-capitalization without ever releasing it, with no window blur involved. In a touch-only session no key event follows either, so neither the key event resync nor the activation reset can correct it and every tap reads as Shift+tap. Pointer events carry the browser's own ctrl/shift on every occurrence, so feed those to the tracker as well. The write is skipped when the value is unchanged, to keep pointermove free of dictionary churn. Alt and Meta are not reported on pointer events and stay on the key event path. See unoplatform#17100 Co-Authored-By: Jacques Bourque <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit 3a6454e)
WebAssemblyWindowWrapper already had OnNativeActivated and OnNativeVisibilityChanged, but nothing ever called them: this was the only Skia target not feeding the shared activation path. Android, AppleUIKit and X11 all wire theirs. Map the browser equivalents onto it - window focus/blur for activation, document visibilitychange for visibility. Window.Activated and Window.VisibilityChanged consequently start being raised on this target, where they never fired before. This also restores the reset of the tracked keyboard state that BaseWindowImplementation performs on an activation change, which is what stops a modifier held while the browser steals focus from being reported as held once the page gets focus back. See unoplatform#17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit 4ebc924)
OnEnterKeyPressed raised KeyDown with no matching KeyUp, so every Enter pressed on the on-screen keyboard left the key reported as held for the rest of the session. The AppleUIKit equivalent already raises both. See unoplatform#17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit 9852c2d)
By default the server reports auto-repeat as a KeyRelease/KeyPress pair, so a key held down arrives as a rapid sequence of presses and releases and CoreWindow.GetKeyState reports it as not held in between. Detectable auto-repeat delivers repeated key presses instead, matching every other platform; GTK, Qt and SDL all request it. It is set per connection, and key events are read from whichever connection owns the window they target, so request it on both. See unoplatform#17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit ac564bb)
Reports CoreWindow.GetKeyState for the modifier keys and the last key pressed, so a modifier that stays reported as held, or a held key whose state is not stable, is visible without a debugger. Also written to the console, where the transition matters more than the current value. See unoplatform#17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit 92deeda)
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> (cherry picked from commit 9bfda24)
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-25155/wasm-skia-net9/index.html |
|
The build 238592 did not find any UI Test snapshots differences. |
morning4coffe-dev
left a comment
There was a problem hiding this comment.
Verified: the diff matches master's #24575 except for the stated adaptations (TextBox instead of ITextBoxHost/core.Owner on 6.8; the X11 detectable-auto-repeat call placed before 6.8's renderer selection). KeyboardStateTracker.cs body is identical to master. Root cause of the WASM stuck-modifier bug is confirmed: before this PR nothing on WASM called OnNativeActivated (NativeWindowWrapper.wasm.cs:33), so BaseWindowImplementation.OnActivationStateChanged (BaseWindowImplementation.cs:234-257) never reached KeyboardStateTracker.Reset() on WASM. Wiring focus/blur to activation is therefore a real root-cause fix for the focus-steal case. The Locked-flicker fix (track Down only) is also a root-cause fix. The PR merges cleanly onto current servicing/6.8 (282bd9b).
Findings, by severity:
- High (regression, shared with master's #24575): modifier resync is applied on every Skia key event, but several platforms never report Alt. InputManager.Keyboard.skia.cs:56 calls SyncModifierStates(args.KeyboardModifiers) and lines 141-155 clear Menu, LeftMenu and RightMenu whenever the event carries no Menu flag. Platforms that never set VirtualKeyModifiers.Menu: X11 (X11XamlRootHost.x11events.cs:318-345, where Mod1Mask, which is Alt on most layouts, maps to Shift), Android (VirtualKeyHelper.Android.cs:172 FromModifiers, no Alt mapping), and UIKit (VirtualKeyHelper.UIKit.cs:149 FromModifierFlags, no Alt mapping). Effect: on those platforms, holding Alt and pressing any other key now clears the Alt state, which 6.8 tracked correctly by key pairs. On X11 it also sets Shift to Down while Alt is held, because Mod1 is mapped to Shift. The X11 mapping defect predates this PR (same in 6.8 base 06d268c), but this PR makes the flags authoritative for CoreWindow.GetKeyState. Consumers include PointerRoutedEventArgs.cs:127-135 (GetAsyncKeyState for Shift, Control and Menu). Suggested root-cause fix: map Alt to Menu on X11, Android and UIKit, and map Mod1 to Menu instead of Shift; alternatively only resync the modifiers a platform actually reports. Add a test. I did not verify the current master file, because the local master checkout is stale and gh could not resolve the path, so the shared-with-master claim rests on the identical diff.
- Medium (behavior change, not called out in "What changed"): CoreVirtualKeyStates.Locked is no longer reported for any key on any Skia platform (KeyboardStateTracker.cs:14-22 remarks, and the removed Locked tests). 6.8 approximated Locked by toggling it on key down, which included Caps Lock. The remarks give a sound reason, but apps that read GetKeyState(VirtualKey.CapitalLock) & Locked will now get None. I found no consumer of Locked in this repo (grep over src). Ask for a note in the PR description or release notes, and state whether Caps Lock will be queried from the platform later. I did not check WinUI's Locked semantics against microsoft-ui-xaml.
- Low: SetStateOnSideKeys (KeyboardStateTracker.cs:108) clears both sides when a combined key is released. This is new behavior for Shift and Menu compared with 6.8, which only cleared the combined key. It is correct for an unpaired release, but when one side is still held it clears the other side until the next key or pointer event resyncs it. Note this in a comment or accept it.
- Low (test hygiene): the class comment at Given_KeyboardStateTracker.cs:11 says each test uses its own key, but When_SyncModifierState_Sets_Down (line 125) leaves Control and RightControl Down, and the tracker is process-wide static state. Add a Reset() in a TestCleanup or in the test.
- Info: the browser pointer resync (BrowserPointerInputSource.cs:104-105) covers Control and Shift only, as the comment explains. Alt is not resynced from pointer events on WASM, so a stuck Alt there is healed only by blur, which now calls Reset. That is acceptable and consistent with the code comment.
CI (statusCheckRollup): all build, runtime and snapshot checks SUCCESS, including the screenshot comparison, XAML Style Check and unit tests. Codacy Static Code Analysis is ACTION_REQUIRED (details not inspected). Uno.UI - docs is NEUTRAL.
Not validated by me: no build, no unit-test run, no runtime run on X11, Android, WASM or UIKit. The 12/12 unit-test claim is author-reported; I confirmed the test count from the diff. Findings 1 and 2 are code-review findings only.
Verdict: COMMENT.
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
|
@Mergifyio backport release/stable/6.7 |
✅ Backports have been createdDetails
|
…6.7/pr-25155 fix(wasm): Stuck and flickering keyboard modifier state (6.8) (backport #25155)
GitHub Issue: unoplatform/kahua-private#545 (backport of #24575)
PR Type:
🐞 Bugfix
What changed? 🚀
Backport of #24575 to 6.8. All 8 commits are cherry-picked with
-x.KeyboardStateTrackerkept modifier state in a persistent dictionary, updated only by matched key down/key up pairs. That caused two bugs on WebAssembly:Downfor the rest of the session. Opening dev tools with Ctrl+Shift+I reliably triggers this.CoreWindow.GetKeyStatethen reported Ctrl as held, so aDataGridtreated plain arrows as Ctrl+Arrow.Lockedflickering during auto-repeat.Lockedwas toggled on every key down, and a held key repeats key downs with no key up in between.The fixes, the same as #24575:
Downstate.Differences from #24575. Two commits conflicted because master has since been refactored around them. In both, the commit's own lines are unchanged:
fix(wasm): Raise key up for the soft keyboard Enter key: master reaches the focused text box throughITextBoxHost/core.Owner, while 6.8 still usesTextBox. The key-up raise is applied to 6.8'sTextBoxform.fix(x11): Enable detectable auto-repeat: master creates its windows through the graphics negotiation pipeline, while 6.8 still has its explicit Vulkan, GL and EGL renderer selection. The per-connectionXkbSetDetectableAutoRepeatcall goes right after the second display connection opens, as on master, ahead of 6.8's renderer selection.Validation:
Given_KeyboardStateTrackerpasses 12/12 onservicing/6.8, which is every test in the file.servicing/6.8:Uno.UI.Runtime.Skia.WebAssembly.Browserruntime, C# and TypeScript;Uno.UI.Runtime.Skia.X11runtime;KeyboardStateProbesample.PR Checklist ✅
Given_KeyboardStateTrackerunit tests and theKeyboardStateProbesample, both from fix(wasm): Stuck and flickering keyboard modifier state #24575.Screenshots Compare Test Runresults.🤖 Generated with Claude Code