Repository: microsoft/terminal (language: cpp)

Issue:
dwControlKeyState behaviour changed between Windows 10/Windows 11
### Windows Terminal version

1.19.231010001-preview

### Windows build number

10.0.22621.0

### Other Software

hiew 8.79 (inside wsl)

### Steps to reproduce

I have a win32 console application ([hiew](https://hiew.ru/), a powerful hex editor) that worked fine when invoked from within WSL in Windows 10, but no longer works in Windows 11. It still works okay when invoked from cmd or powershell, but I liked using it under WSL ☹️ 

Various keys no longer seem to register.

I wrote a quick c program to dump console input events, and I observe the following pattern:

- Pressing F11 with a win32 console application under WSL on Windows 10 (conhost 10.0.19041.3393) (Works ✅):

```
Key event:
 bKeyDown:          1
 wRepeatCount:      1
 wVirtualKeyCode:   0x7a
    MAPVK_VK_TO_VSC  => 0x57
    MAPVK_VK_TO_CHAR => 00
 wVirtualScanCode:  0x57
    MAPVK_VSC_TO_VK => 0x7a
    MAPVK_VSC_TO_VK_EX => 0x7a
 uChar:
  UnicodeChar:      '' (0000)
  AsciiChar:        '' (00)
    VkKeyScanA  => 0x3c0
 dwControlKeyState: 0
```

- Pressing F11 with a win32 console application under cmd on Windows 11 (I've tried release conhost, and current stable and preview OpenConsole, with same result ✅)


```
Key event:
 bKeyDown:          1
 wRepeatCount:      1
 wVirtualKeyCode:   0x7a
    MAPVK_VK_TO_VSC  => 0x57
    MAPVK_VK_TO_CHAR => 00
 wVirtualScanCode:  0x57
    MAPVK_VSC_TO_VK => 0x7a
    MAPVK_VSC_TO_VK_EX => 0x7a
 uChar:
  UnicodeChar:      ' ' (0000)
  AsciiChar:        ' ' (00)
    VkKeyScanA  => 0x3c0
 dwControlKeyState: 0
```

- Pressing F11  with a win32 console application under *WSL* on Windows 11 (Also tried release conhost, stable and preview OpenConsole, with same result ❌ )

```
Key event:
 bKeyDown:          1
 wRepeatCount:      1
 wVirtualKeyCode:   0x7a
    MAPVK_VK_TO_VSC  => 0x57
    MAPVK_VK_TO_CHAR => 00
 wVirtualScanCode:  0x57
    MAPVK_VSC_TO_VK => 0x7a
    MAPVK_VSC_TO_VK_EX => 0x7a
 uChar:
  UnicodeChar:      '' (0000)
  AsciiChar:        '' (00)
    VkKeyScanA  => 0x3c0
 dwControlKeyState: 256
ENHANCED_KEY
```

I think the issue is that [ENHANCED_KEY](https://learn.microsoft.com/en-us/windows/console/key-event-record-str#) is now being set, and it wasn't before. I don't know why this changed, but it seems to have broken at least one app ☹️ 

Do you know what might be causing this?


### Expected Behavior

Keys to register.

### Actual Behavior

Keys are ignored, I suspect because dwControlKeyState changed.


Discussion / hints:
I'm almost certain this is probably due to #5021, though, there's probably a complicated relationship here between the fact that there's a different ConPTY that's used for Terminal (and is shipped with the Terminal), vs the one that WSL uses for interop (and is shipped with the OS)
If I replace conhost.exe with OpenConsole, will WSL automatically pick it up interop as well? (I don't know how all the pieces fit together sorry!)

I'm willing to give it a shot and see if it's already been fixed somewhere...? (I assume I'll have to `takeown`/`icacl`/etc.., I don't mind voiding my warranty 😄)

It absolutely will! No warranty, etc, but you know the drill. :smile:

Thanks for being so down to test things with us.
To do it "correctly", you'd use a tool called `sfpcopy.exe` but it'll also work without it. Putting the original `conhost.exe` back will not restore Windows to its original state because `conhost.exe` is actually hardlinked to
```
C:\Windows\WinSxS\amd64_microsoft-onecore-console-host-core_31bf3856ad364e35_10.0.22621.2483_none_4d83bf9b9fc4fb79\conhost.exe
```
(...with a possibly different version number on your machine.)

You can probably (90% sure?) restore the original `conhost.exe` to its original state by running `sfc /scannow`. I've been running OpenConsole.exe as my conhost for about a year now and it's been fine, but I'm also running internal dev version of Windows anyways, so... 😅
Do note that if you damage the version in the component store (`...\WinSxS`) you become ineligible for future servicing updates. Replacing just the hardlink is _much better_ in that regard. 😁 
I did get the system booted with 1.19.2310.10001, but sadly my console bug is still there!

I listened to your warning and didn't bother trying to mess with the links, I assume some future update will clobber it with a link, that's okay with me 😄 

I guess there are probably not many people trying to use win32 console apps in WSL, maybe I can try to bisect it at least, even if I don't know what the solution is.

Fix patch:
diff --git a/src/terminal/parser/InputStateMachineEngine.cpp b/src/terminal/parser/InputStateMachineEngine.cpp
index 95fb5a3e509..7ea26ce9c84 100644
--- a/src/terminal/parser/InputStateMachineEngine.cpp
+++ b/src/terminal/parser/InputStateMachineEngine.cpp
@@ -794,7 +794,6 @@ DWORD InputStateMachineEngine::_GetModifier(const size_t modifierParam) noexcept
     // VT Modifiers are 1+(modifier flags)
     const auto vtParam = modifierParam - 1;
     DWORD modifierState = 0;
-    WI_SetFlagIf(modifierState, ENHANCED_KEY, modifierParam > 0);
     WI_SetFlagIf(modifierState, SHIFT_PRESSED, WI_IsFlagSet(vtParam, VT_SHIFT));
     WI_SetFlagIf(modifierState, LEFT_ALT_PRESSED, WI_IsFlagSet(vtParam, VT_ALT));
     WI_SetFlagIf(modifierState, LEFT_CTRL_PRESSED, WI_IsFlagSet(vtParam, VT_CTRL));


Test patch: