Terminal: ConPTY does not translate arrow keys with +ENHANCED_KEY in their modifiers

Created on 12 Aug 2019  ·  11Comments  ·  Source: microsoft/terminal

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.

Area-Input Issue-Bug Priority-2 Product-Conpty Resolution-Fix-Committed v1-Scrubbed

Most helpful comment

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

Handy links:

All 11 comments

Any help?

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.

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.

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.

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

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

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

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

So, the program written before runs unnormal.

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

Handy links:

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

Great!

Was this page helpful?
0 / 5 - 0 ratings