Skip to content

Preserve ENHANCED_KEY for direct key events (Alt up, F7 down) - #20619

Open
TheJesper wants to merge 1 commit into
microsoft:mainfrom
TheJesper:fix/18120-enhanced-key-rightalt-release
Open

Preserve ENHANCED_KEY for direct key events (Alt up, F7 down)#20619
TheJesper wants to merge 1 commit into
microsoft:mainfrom
TheJesper:fix/18120-enhanced-key-rightalt-release

Conversation

@TheJesper

@TheJesper TheJesper commented Aug 28, 2026

Copy link
Copy Markdown

Summary of the Pull Request

WindowEmperor::_dispatchSpecialKey intercepts Alt key-up (and F7 key-down) to handle the window menu before XAML sees them. These events get forwarded through IDirectKeyListener::OnDirectKeyEvent, but the extended-key bit from lParam was dropped along the way. Without it, TermControl can't distinguish RightAlt from LeftAlt on release, so a RightAlt key-up produces an INPUT_RECORD identical to LeftAlt. The key press works fine because it goes through the regular XAML _KeyHandler path where CorePhysicalKeyStatus.IsExtendedKey is available.

Fix: thread an extended bool through the OnDirectKeyEvent chain and set ControlKeyStates::EnhancedKey when true - same thing the regular key path already does. Changed signature: OnDirectKeyEvent(vkey, scanCode, down, extended).

References and Relevant Issues

Closes #18120

Detailed Description of the Pull Request / Additional comments

The asymmetry happens because the emperor intercepts Alt-up for system menu handling - it needs to swallow it before XAML turns it into a menu activation. That interception path (_dispatchSpecialKey) only extracted the virtual key, scan code, and up/down state from lParam, ignoring bit 24 (the extended-key flag). This PR extracts that bit and passes it through.

Validation Steps Performed

Built two copies locally (x64 Debug, VS 2026 / SDK 10.0.26100), registered with Add-AppxPackage -Register, ran on Windows 11. Used a small ReadConsoleInputW repro tool with win32-input-mode enabled (\x1b[?9001h), pressing RightAlt and LeftAlt on a Swedish keyboard layout (where RightAlt is AltGr, so Windows translates it to Ctrl+Alt - hence the (auto) LeftCtrl events).

Before (parent commit 079d1cc) - RightAlt release loses its identity:
before

pressed     reported    action   EK    note
----------  ----------  -------  ----  ------
RightAlt    LeftCtrl    press    0     (auto)
RightAlt    RightAlt    press    1
RightAlt    LeftCtrl    release  0     (auto)
RightAlt    LeftAlt     release  0     <-- wrong, should be RightAlt
LeftAlt     LeftAlt     press    0
LeftAlt     LeftAlt     release  0

After (this branch) - works correctly:
after

pressed     reported    action   EK    note
----------  ----------  -------  ----  ------
RightAlt    LeftCtrl    press    0     (auto)
RightAlt    RightAlt    press    1
RightAlt    LeftCtrl    release  0     (auto)
RightAlt    RightAlt    release  1     <-- correct
LeftAlt     LeftAlt     press    0
LeftAlt     LeftAlt     release  0

Also added InputTest::Win32KeyEventsRetainEnhancedKey which checks that win32-input-mode encodes the flag correctly for both press and release of RightAlt. Passes locally.

PR Checklist

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Key events that reach us via WindowEmperor::_dispatchSpecialKey (F7 down,
Alt up) were forwarded through IDirectKeyListener::OnDirectKeyEvent without
the extended-key bit from lParam. TermControl therefore never set
ENHANCED_KEY on these events, so releasing e.g. RightAlt produced an
INPUT_RECORD that is indistinguishable from a LeftAlt release.

Thread an "extended" flag through the OnDirectKeyEvent chain and set
EnhancedKey in TermControl, matching what the regular _KeyHandler path
derives from CorePhysicalKeyStatus.IsExtendedKey.

Adds a TerminalCore unit test verifying that win32-input-mode key events
retain ENHANCED_KEY on both press and release of RightAlt.

Closes microsoft#18120
@TheJesper
TheJesper force-pushed the fix/18120-enhanced-key-rightalt-release branch from 06c5cc2 to 0c9ef02 Compare August 30, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENHANCED_KEY flag missing when releasing RightAlt

1 participant