Repository navigation
fix(wasm): Stuck and flickering keyboard modifier state - #24575
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24575/wasm-skia-net9/index.html |
|
The build 235287 found UI Test snapshots differences: Details
|
|
|
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24575/wasm-skia-net9/index.html |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24575/wasm-skia-net9/index.html |
|
The build 235383 found UI Test snapshots differences: Details
|
|
|
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 #17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #17100 Co-Authored-By: Jacques Bourque <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 #17100 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
7a25f40 to
9bfda24
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-24575/wasm-skia-net9/index.html |
|
The build 235520 found UI Test snapshots differences: Details
|
|
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. |
Port of b580458 (unoplatform#24575). KeyboardStateTracker emulated the Locked flag as a press parity counter. Key events update the tracker once per element they are raised on, so a held key read as Down or as Down | Locked depending on how deep the focused element was, and comparisons against Down alone, such as the drag modifiers of PointerRoutedEventArgs, missed it half of the time. Nothing reads the flag. Only Down is tracked now, which makes the tracker idempotent. This also adds SyncModifierState, which the next two commits use to repair a modifier whose key up never arrived. 6.8 difference: the tracker is in Uno.UWP (Uno.WinRT on master).
Port of a9ff5c6 (unoplatform#24575). A modifier released while the browser or the OS holds focus produces no key up, so it stayed reported as held for the rest of the session. Every key event carries the state of all four modifiers, and OnKey now applies it to the tracker before dispatching. 6.8 difference: the file is InputManager.Keyboard.skia.cs.
Port of 3a6454e (unoplatform#24575). Pointer events carry the browser's ctrl and shift on every occurrence, so they repair a modifier whose key up was lost before any key is pressed again. Ctrl held for a click that opens a link in another tab is released over that tab, and the first tab kept reporting it as down. Alt and Meta are not reported on pointer events and stay on the key event path.
Port of 4ebc924 (unoplatform#24575). WebAssemblyWindowWrapper had OnNativeActivated and OnNativeVisibilityChanged, but nothing called them. Window focus and blur now feed activation and the document's visibilitychange feeds visibility, so Window.Activated and Window.VisibilityChanged are raised on this target. An activation change also resets the tracked keyboard state (BaseWindowImplementation), which clears a key released while another tab or window had focus without waiting for the next input event. As on the other targets, it closes the light-dismiss popups that are open (PopupRoot) unless FeatureConfiguration.Popup.PreventLightDismissOnWindowDeactivated is set.
Enter in a TextBox that does not accept returns reaches managed code through up to three DOM events: its keydown (through BrowserKeyboardInputSource), the beforeinput of the line break the browser then sets out to insert, and its keyup. With a hardware keyboard the first two each raised KeyDown, and since OnEnterKeyPressed raises the release too (unoplatform#24575) KeyUp came twice as well: one press gave KeyDown, KeyDown, KeyUp, KeyUp. Each press is now raised once: - The beforeinput handler leaves a key alone that BrowserKeyboardInputSource raises from its keydown, which it does for a keydown that has the code it tells the key by. A soft keyboard's Enter has none and is still raised from the beforeinput. - The keyup handler does not raise a key the beforeinput handler raised, and keeps a keyup that has a code from BrowserKeyboardInputSource then, which would raise the release again. - The keyup it does raise the key for is kept from BrowserKeyboardInputSource as well, which raised the release a second time when that keyup had a code (the shape of an Enter that commits an IME composition). What has raised the key is forgotten at its keyup, when the input is detached and at the next keydown. The keydown of another key keeps it for an Enter the line break raised, though: that Enter may still be down, and its keyup is not to raise it again. Which presses of Enter are raised is unchanged.
Enter in a TextBox that does not accept returns reaches managed code through up to three DOM events: its keydown (through BrowserKeyboardInputSource), the beforeinput of the line break the browser then sets out to insert, and its keyup. With a hardware keyboard the first two each raised KeyDown, and since OnEnterKeyPressed raises the release too (unoplatform#24575) KeyUp came twice as well: one press gave KeyDown, KeyDown, KeyUp, KeyUp. Each press is now raised once: - The beforeinput handler leaves a key alone that BrowserKeyboardInputSource raises from its keydown, which it does for a keydown that has the code it tells the key by. A soft keyboard's Enter has none and is still raised from the beforeinput. - The keyup handler does not raise a key the beforeinput handler raised, and keeps a keyup that has a code from BrowserKeyboardInputSource then, which would raise the release again. - The keyup it does raise the key for is kept from BrowserKeyboardInputSource as well, which raised the release a second time when that keyup had a code (the shape of an Enter that commits an IME composition). The presses of Enter that are down are tracked by the code of their key, and BrowserKeyboardInputSource reports the keydowns and keyups that reach the document. A press stays a single one when another key goes down before its release, when the main and the numpad Enter overlap, when a handler moves focus off the TextBox before the keyup (which then goes to the document), and when it starts on another control and is released on the TextBox, which then only gets the release. Which presses of Enter are raised for a key pressed and released on a TextBox is unchanged.
Enter in a TextBox that does not accept returns reaches managed code through up to three DOM events: its keydown (through BrowserKeyboardInputSource), the beforeinput of the line break the browser then sets out to insert, and its keyup. With a hardware keyboard the first two each raised KeyDown, and since OnEnterKeyPressed raises the release too (unoplatform#24575) KeyUp came twice as well: one press gave KeyDown, KeyDown, KeyUp, KeyUp. Each press is now raised once: - The beforeinput handler leaves a key alone that BrowserKeyboardInputSource raises from its keydown, which it does for a keydown that has the code it tells the key by. A soft keyboard's Enter has none and is still raised from the beforeinput. - The keyup handler does not raise a key the beforeinput handler raised, and stops that keyup, when it has a code, before it reaches BrowserKeyboardInputSource, which would raise the release again. The presses of Enter that are down are tracked by the code of their key, and BrowserKeyboardInputSource reports the keydowns and keyups that reach the document. A press stays a single one when another key goes down before its release, when the main and the numpad Enter overlap, when a handler moves focus off the TextBox before the keyup (which then goes to the document), and when it starts on another control and is released on the TextBox, which then only gets the release.
GitHub Issue: closes unoplatform/kahua-private#545
PR Type:
🐞 Bugfix
What changed? 🚀
KeyboardStateTrackertracked modifier state as a persistent dictionary updated only by matched key down/key up pairs, which produced two defects on WebAssembly.Stuck modifiers after a focus steal. When a modifier's key up never reached the page — the browser stealing focus mid-keystroke, opening dev tools with Ctrl+Shift+I being a reliable trigger — that modifier stayed
Downfor the rest of the session. Anything readingCoreWindow.GetKeyStatethen misbehaved on every later keypress: aDataGridtreated plain arrow keys as Ctrl+Arrow, even though the browser's own key events correctly reportedctrlKey: false.Lockedflickering during auto-repeat. TheLockedflag was toggled on every key down. That is meaningful for Caps Lock / Num Lock / Scroll Lock, but it was applied to every key, and browsers raise repeated key downs with no key up in between while a key is held.GetKeyStatetherefore alternated betweenDownandDown, Lockedfor any key held long enough.Changes:
Downstate.Lockedmirrors the Win32 "toggled" bit, a parity counter the OS maintains per key; deriving it from received key events needs exactly one update per physical press, which no platform guarantees. 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 unfocused.visibilitychangeonto the shared activation path. Beyond raisingWindow.ActivatedandWindow.VisibilityChanged, which never fired on this target, this is what lets a key released while the browser holds focus stop being reported as held.PR Checklist ✅
Screenshots Compare Test Runresults. The reported counts (skia-linux57,skia-windows2310,wasm11) are identical on every other open PR built in the same window, so they reflect a global baseline drift rather than anything this PR changes.Unit tests cover the tracker (
Given_KeyboardStateTracker, 12 passing) and aKeyboardStateProbesample underWindows/UI/Core/KeyboardStateTestsshows liveGetKeyStatevalues for manual verification of both the focus-steal and auto-repeat cases.🤖 Generated with Claude Code