Skip to content

fix(wasm): Stuck and flickering keyboard modifier state (6.8) - #25155

Merged
agneszitte merged 8 commits into
unoplatform:servicing/6.8from
ramezgerges:dev/rara/68-wasm-keyboard-state
Oct 13, 2026
Merged

agneszitte merged 8 commits into
unoplatform:servicing/6.8from
ramezgerges:dev/rara/68-wasm-keyboard-state

Conversation

@ramezgerges

Copy link
Copy Markdown
Contributor

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.

KeyboardStateTracker kept modifier state in a persistent dictionary, updated only by matched key down/key up pairs. That caused two bugs on WebAssembly:

  • Stuck modifiers after a focus steal. A modifier whose key up never reached the page stayed Down for the rest of the session. Opening dev tools with Ctrl+Shift+I reliably triggers this. CoreWindow.GetKeyState then reported Ctrl as held, so a DataGrid treated plain arrows as Ctrl+Arrow.
  • Locked flickering during auto-repeat. Locked was toggled on every key down, and a held key repeats key downs with no key up in between.

The fixes, the same as #24575:

  • Track only the Down state.
  • Resync the modifiers from the flags carried by key events, and on WebAssembly from pointer events too.
  • Report window activation and page visibility on WebAssembly.
  • Raise a key up for the soft keyboard's Enter key.
  • Enable detectable auto-repeat on X11.

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 through ITextBoxHost/core.Owner, while 6.8 still uses TextBox. The key-up raise is applied to 6.8's TextBox form.
  • 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-connection XkbSetDetectableAutoRepeat call goes right after the second display connection opens, as on master, ahead of 6.8's renderer selection.

Validation:

  • Unit tests: Given_KeyboardStateTracker passes 12/12 on servicing/6.8, which is every test in the file.
  • Compile: these all build on servicing/6.8:
    • the Uno.UI.Runtime.Skia.WebAssembly.Browser runtime, C# and TypeScript;
    • the Uno.UI.Runtime.Skia.X11 runtime;
    • the desktop SamplesApp, with the new KeyboardStateProbe sample.
  • Not validated at runtime on 6.8: the focus-steal, auto-repeat and X11 held-key cases. fix(wasm): Stuck and flickering keyboard modifier state #24575 covered them on master, and the changed code is the same.

PR Checklist ✅

🤖 Generated with Claude Code

ramezgerges and others added 8 commits October 9, 2026 12:10
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)
@github-actions github-actions Bot added area/skia ✏️ Categorizes an issue or PR as relevant to Skia area/automation Categorizes an issue or PR as relevant to project automation labels Oct 9, 2026
@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 238592 did not find any UI Test snapshots differences.

@morning4coffe-dev morning4coffe-dev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

@mergify

mergify Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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.

@agneszitte
agneszitte merged commit 27de86e into unoplatform:servicing/6.8 Oct 13, 2026
43 of 44 checks passed
@agneszitte

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/6.7

@mergify

mergify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7

✅ Backports have been created

Details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation area/skia ✏️ Categorizes an issue or PR as relevant to Skia

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants