Skip to content

[KeyboardManager] Fix sticky Ctrl caused by stale AltGr flag#46672

Open
fluffyspace wants to merge 3 commits intomicrosoft:mainfrom
fluffyspace:fix/keyboard-manager-sticky-ctrl-altgr
Open

[KeyboardManager] Fix sticky Ctrl caused by stale AltGr flag#46672
fluffyspace wants to merge 3 commits intomicrosoft:mainfrom
fluffyspace:fix/keyboard-manager-sticky-ctrl-altgr

Conversation

@fluffyspace
Copy link
Copy Markdown

@fluffyspace fluffyspace commented Mar 31, 2026

Summary

Fixes #46693

  • The static bool isAltRightKeyInvoked flag in HandleShortcutRemapEvent is set when AltGr (RAlt+LCtrl) is pressed, but only reset inside Case 1's else branch — which requires a shortcut to be actively invoked (isShortcutInvoked == true)
  • If the user presses and releases AltGr when no shortcut is active, the flag stays true permanently across all future calls
  • Once stale, the flag blocks modifier key restoration and state resets at 13 locations throughout the function (lines 646, 670, 791, 800, 866, 880, 889, 907, 927, 978, 996), and causes LCtrl key-up events to break out of the handler loop (line 620-622) instead of being properly processed
  • This makes LCtrl permanently stuck for any shortcut remap that uses LCtrl as a modifier

Fix

Reset isAltRightKeyInvoked when VK_RMENU (Right Alt) is released, regardless of whether a shortcut is currently invoked. This is added as an else if right after the existing flag-set condition.

Test plan

  • Configure a shortcut remap using LCtrl (e.g. LCtrl+Y → Backspace)
  • Press and release AltGr without triggering any shortcut
  • Use the LCtrl shortcut — verify Ctrl does not become stuck
  • Verify AltGr-based shortcuts still work correctly
  • Verify normal AltGr character input (e.g. AltGr+Q for @ on German layout) is unaffected

…d flag

The static isAltRightKeyInvoked flag was set to true when AltGr (RAlt+LCtrl)
was pressed, but only reset inside Case 1's else branch which requires an
active shortcut invocation. If AltGr was pressed when no shortcut was active,
the flag stayed true permanently, blocking modifier restoration and state
resets throughout HandleShortcutRemapEvent, causing LCtrl to become stuck.

Reset the flag when VK_RMENU is released regardless of shortcut state.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a KeyboardManager shortcut-remap edge case where a stale isAltRightKeyInvoked (AltGr) flag can prevent proper modifier restoration, leading to “stuck Ctrl” behavior.

Changes:

  • Adds logic to clear isAltRightKeyInvoked on VK_RMENU (Right Alt) key-up events inside HandleShortcutRemapEvent.

Comment on lines +306 to +310
@@ -307,6 +307,10 @@ namespace KeyboardEventHandlers
{
isAltRightKeyInvoked = true;
}
else if (data->lParam->vkCode == VK_RMENU && (data->wParam == WM_KEYUP || data->wParam == WM_SYSKEYUP))
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

The new reset branch for isAltRightKeyInvoked can be skipped on VK_RMENU key-up events: the preceding if (vkCode == VK_RMENU && ii.GetVirtualKeyState(VK_LCONTROL)) also runs on key-up, and if VK_LCONTROL is still reported as down at that moment (common with AltGr), it will set the flag back to true and prevent the reset. To ensure the flag is reliably cleared on release, handle the key-up case first (or gate the "set" branch to key-down only).

Copilot uses AI. Check for mistakes.
@niels9001
Copy link
Copy Markdown
Collaborator

@fluffyspace What issue is this PR closing?

Three test cases covering the bug where isAltRightKeyInvoked stays true
after AltGr is pressed without an active shortcut:

1. Basic: AltGr press-release followed by shortcut invoke and full
   key release — verifies state resets properly
2. Revert: AltGr press-release followed by shortcut with extra key
   pressed — verifies Case 3 else path restores modifiers
3. Repeated: Multiple AltGr press-release cycles followed by shortcut
   — verifies no accumulated corruption

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@fluffyspace
Copy link
Copy Markdown
Author

@niels9001 Apologies for the missing link — the issue was filed after the PR was initially created. It's now linked in the description:

The PR description has been updated with the full root cause analysis and test plan.

@fluffyspace
Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

The set branch for isAltRightKeyInvoked did not check for key-down,
so on VK_RMENU key-up with LCtrl still held (normal AltGr release
order) it would re-set the flag to true before the reset branch
could execute. Add WM_KEYDOWN/WM_SYSKEYDOWN check to the set
condition so the else-if reset on key-up is reached reliably.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@niels9001 niels9001 added the Product-Keyboard Shortcut Manager Issues regarding Keyboard Shortcut Manager label Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-Keyboard Shortcut Manager Issues regarding Keyboard Shortcut Manager

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[KeyboardManager] Ctrl becomes permanently stuck after pressing AltGr when shortcut remap uses LCtrl

3 participants