ConPTY does not translate arrow keys with +ENHANCED_KEY in their modifiers #3364

Closed
opened 2026-01-30 23:19:35 +00:00 by claunia · 11 comments
Owner

Originally created by @SummerGift on GitHub (Aug 12, 2019).

Originally assigned to: @carlos-zamora on GitHub.

Today I updated Windows to 1903 and found that a python program I had written earlier ran incorrectly.

The brief test code is as follows:

import msvcrt
ch1 = msvcrt.getch()
ch2 = msvcrt.getch()
print(ch1)
print(ch2)

When I run the above code in native powershell, press ← key when collecting characters, and it will print b'\xe0' B 'K', and the same code running in the terminal returns b'\x00' b'K'.
This problem didn't appear in the last version of Windows (18.xx).

Steps to Reproduce:

  1. Run this code on the powershell terminal of vscode using Python3
  2. press ← key,record print information
  3. Run this code directly from your powershell on Windows using Python3
  4. press ← key,record print information
  5. Look at different outputs

A related issue on vscode:

https://github.com/microsoft/vscode/issues/78682

Thank you for your attention.

Originally created by @SummerGift on GitHub (Aug 12, 2019). Originally assigned to: @carlos-zamora on GitHub. Today I updated Windows to 1903 and found that a python program I had written earlier ran incorrectly. The brief test code is as follows: ``` import msvcrt ch1 = msvcrt.getch() ch2 = msvcrt.getch() print(ch1) print(ch2) ``` When I run the above code in native powershell, press ← key when collecting characters, and it will print `b'\xe0' B 'K'`, and the same code running in the terminal returns `b'\x00' b'K'`. This problem didn't appear in the last version of Windows (18.xx). - OS Version: windows 10 1903 - [terminal version](https://www.microsoft.com/en-us/p/windows-terminal-preview/9n0dx20hk701?activetab=pivot:overviewtab) : preview Steps to Reproduce: 1. Run this code on the powershell terminal of vscode using Python3 2. press ← key,record print information 3. Run this code directly from your powershell on Windows using Python3 4. press ← key,record print information 5. Look at different outputs A related issue on vscode: https://github.com/microsoft/vscode/issues/78682 Thank you for your attention.
Author
Owner

@SummerGift commented on GitHub (Aug 19, 2019):

Any help?

@SummerGift commented on GitHub (Aug 19, 2019): Any help?
Author
Owner

@zadjii-msft commented on GitHub (Aug 19, 2019):

We believe this is a bug, but we're not really sure what the root cause is. Presumably something about how ConPTY builds INPUT_RECORDs compared to how we build them from window input directly.

We're also very confused that ch1 in the above sample is b'\xe0' b'K' - we really didn't expect that. So we just need someone to really debug into this.

@zadjii-msft commented on GitHub (Aug 19, 2019): We believe this is a bug, but we're not really sure what the root cause is. Presumably something about how ConPTY builds `INPUT_RECORD`s compared to how we build them from window input directly. We're also _very_ confused that `ch1` in the above sample is `b'\xe0' b'K'` - we _really_ didn't expect that. So we just need someone to really debug into this.
Author
Owner

@SummerGift commented on GitHub (Sep 2, 2019):

Before the 1903 version of Windows, the return value was b'\xe0' b'K', which became b'\x00' b'K' when upgraded to 1903, causing some programs to fail.

@SummerGift commented on GitHub (Sep 2, 2019): Before the 1903 version of Windows, the return value was b'\xe0' b'K', which became b'\x00' b'K' when upgraded to 1903, causing some programs to fail.
Author
Owner

@DHowett-MSFT commented on GitHub (Oct 1, 2019):

Eureka!

Under normal circumstances:

  • ↓ wch:0x0000 '\0' mod:EnhancedKey (0x00000100) repeat:0x0001 vk:0x0025 vsc:0x004b

Under ConPTY:

  • ↓ wch:0x0000 '\0' mod:None (0x00000000) repeat:0x0001 vk:0x0025 vsc:0x004b

Critical here is mod:EnhancedKey. ConPTY isn't setting it. I'm not sure how it could know to set it, because we usually derive its value from the Win32 window proc...
ConPTY only sets it if there's a modifier on the arrow key.

The documentation on ENHANCED_KEY says:

Enhanced keys for the IBM® 101- and 102-key keyboards are the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and direction keys in the clusters to the left of the keypad; and the divide (/) and ENTER keys in the keypad.

@DHowett-MSFT commented on GitHub (Oct 1, 2019): Eureka! Under normal circumstances: * `↓ wch:0x0000 '\0' mod:EnhancedKey (0x00000100) repeat:0x0001 vk:0x0025 vsc:0x004b` Under ConPTY: * `↓ wch:0x0000 '\0' mod:None (0x00000000) repeat:0x0001 vk:0x0025 vsc:0x004b` Critical here is `mod:EnhancedKey`. ConPTY isn't setting it. I'm not sure how it could know _to_ set it, because we usually derive its value from the Win32 window proc... ConPTY only sets it if there's a modifier _on_ the arrow key. The [documentation](https://docs.microsoft.com/en-us/windows/console/key-event-record-str) on `ENHANCED_KEY` says: > Enhanced keys for the IBM® 101- and 102-key keyboards are the INS, DEL, HOME, END, PAGE UP, PAGE DOWN, and direction keys in the clusters to the left of the keypad; and the divide (/) and ENTER keys in the keypad.
Author
Owner

@DHowett-MSFT commented on GitHub (Oct 1, 2019):

And now that we have a root cause, I'm finally renaming this issue.

@DHowett-MSFT commented on GitHub (Oct 1, 2019): And now that we have a root cause, I'm finally renaming this issue.
Author
Owner

@DHowett-MSFT commented on GitHub (Oct 1, 2019):

Yanking the triage tag after a discussion with @carlos-zamora and @zadjii-msft. Carlos is down to look at this 😄

@DHowett-MSFT commented on GitHub (Oct 1, 2019): Yanking the triage tag after a discussion with @carlos-zamora and @zadjii-msft. Carlos is down to look at this :smile:
Author
Owner

@wmcbrine commented on GitHub (Nov 3, 2019):

This also prevents PDCurses from recognizing the cursor keys, as I just discovered.

@wmcbrine commented on GitHub (Nov 3, 2019): This also prevents PDCurses from recognizing the cursor keys, as I just discovered.
Author
Owner

@SummerGift commented on GitHub (Nov 4, 2019):

This also prevents PDCurses from recognizing the cursor keys, as I just discovered.

So, the program written before runs unnormal.

@SummerGift commented on GitHub (Nov 4, 2019): > This also prevents PDCurses from recognizing the cursor keys, as I just discovered. So, the program written before runs unnormal.
Author
Owner

@ghost commented on GitHub (Apr 22, 2020):

:tada:This issue was addressed in #5021, which has now been successfully released as Windows Terminal Preview v0.11.1121.0.🎉

Handy links:

@ghost commented on GitHub (Apr 22, 2020): :tada:This issue was addressed in #5021, which has now been successfully released as `Windows Terminal Preview v0.11.1121.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v0.11.1121.0) * [Store Download](https://www.microsoft.com/store/apps/9n0dx20hk701?cid=storebadge&ocid=badge)
Author
Owner

@thomthom commented on GitHub (Apr 22, 2020):

Thank you! I can finally get rid of cmder now and switch to Terminal full time! 🎉

@thomthom commented on GitHub (Apr 22, 2020): Thank you! I can finally get rid of `cmder` now and switch to Terminal full time! 🎉
Author
Owner

@DHowett-MSFT commented on GitHub (Apr 22, 2020):

Great!

@DHowett-MSFT commented on GitHub (Apr 22, 2020): Great!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#3364