Skip to content

Ctrl+Alt+Shift+2 doesn't send NUL in VT input mode #5205

Closed

Description

Mike Griese (@zadjii-msft):

Okay so Ctrl+Alt+Space was fixed for conhost, but it looks like it's still broken in the Terminal 😢 Now it's just generating a ^@, a single NUL.

I think the Terminal is synthesizing the right sequence, but maybe conpty is generating the wrong input for it now, or maybe the input that's generated by conpty doesn't get re-translated back to ^[^@ correctly. I can try to keep investigating to figure out where the miscommunication is, if you need help.

Leonard Hecker (@lhecker):

Mike Griese (@zadjii-msft) Yeah I was just about to say that as well.

As I wrote above the most puzzling thing for me though is if you change this if condition (line 510) to a simple ch != 0 - which should be correct as well.
If you attach a debugger you'll see that TerminalInput definitely invokes _pfnWriteEvents with a \x1b\x00 sequence.
But then showkey shows a ^[^R sequence instead. How can this happen? > I don't understand how such a benign change to the if condition can cause this. 😞

Okay I maybe saw what happened here.

  1. On a en-us keyboard, '@' is shift+2.
  2. When conpty sees a \x1b\x0, it converts it to [Shift_down, Ctrl_down, Alt_down, 2_down, 2_up, Alt_up, Ctrl_up, Shift_up].
    • I did not pay a ton of attention to the keys that got synthesized here. I need to re-investigate the [2_down, 2_up]
  3. When we get back to TerminalInput::HandleKey, we end up skipping that branch, because the ch was 0n50, which is 0x32, which is '2'.

I'm not sure what's exactly the right behavior here. I'd think we probably should send a different key, but I'll need to investigate more. It's possible that the ch != 0 change would work just fine, but I want to make sure that the upstream input from conpty is actually correct.
I've got a braindead fix that would fix showkey -a, but that doesn't really resolve the issue here for Win32 applications unfortunately, so I'm gonna file a separate issue to track this investigation.

Originally posted by Mike Griese (@zadjii-msft) in #4192 (comment)

Activity

  1. ghost added
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Apr 1, 2020
  2. ghost added
    Needs-Tag-FixDoesn't match tag requirements
    on Apr 1, 2020
  3. added
    Area-InputRelated to input processing (key presses, mouse, etc.)
    Product-ConptyFor console issues specifically related to conpty
    Issue-BugIt either shouldn't be doing this or needs an investigation.
    on Apr 1, 2020
  4. ghost removed
    Needs-Tag-FixDoesn't match tag requirements
    on Apr 1, 2020
  5. zadjii-msft commented on Apr 1, 2020

    @zadjii-msft
    MemberAuthor

    master, Conhost+conpty, Ctrl+alt+space, (i/o modes):(0x 1f7, 0x f) (Win32 input)

    Down: 1 Repeat: 1 KeyCode: 0x10 ScanCode: 0x2a Char: \0 (0x0) KeyState: 0x10
    Down: 1 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x12
    Down: 1 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x1a
    Down: 1 Repeat: 1 KeyCode: 0x32 ScanCode: 0x3 Char: \0 (0x0) KeyState: 0x1a
    Down: 0 Repeat: 1 KeyCode: 0x32 ScanCode: 0x3 Char: \0 (0x0) KeyState: 0x1a
    Down: 0 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x12
    Down: 0 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x10
    Down: 0 Repeat: 1 KeyCode: 0x10 ScanCode: 0x2a Char: \0 (0x0) KeyState: 0x0
    

    The interesting input here is Down: 1 Repeat: 1 KeyCode: 0x32 ScanCode: 0x3 Char: \0 (0x0) KeyState: 0x1a

    master, Conhost+conpty, Ctrl+alt+space, (i/o modes):(0x 3f7, 0x f) (VT input)

    Down: 1 Repeat: 1 KeyCode: 0x10 ScanCode: 0x2a Char: \0 (0x0) KeyState: 0x10
    Down: 1 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x12
    Down: 1 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x1a
    Down: 1 Repeat: 1 KeyCode: 0x32 ScanCode: 0x0 Char: \0 (0x0) KeyState: 0x1a
    Down: 0 Repeat: 1 KeyCode: 0x32 ScanCode: 0x3 Char: \0 (0x0) KeyState: 0x1a
    Down: 0 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x12
    Down: 0 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x10
    Down: 0 Repeat: 1 KeyCode: 0x10 ScanCode: 0x2a Char: \0 (0x0) KeyState: 0x0
    

    master, Conhost, Ctrl+alt+space, (i/o modes):(0x 1f7, 0x f) (Win32 input)

    Down: 1 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x28
    Down: 1 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x2a
    Down: 1 Repeat: 1 KeyCode: 0x20 ScanCode: 0x39 Char: \0 (0x0) KeyState: 0x2a
    Down: 0 Repeat: 1 KeyCode: 0x20 ScanCode: 0x39 Char: \0 (0x0) KeyState: 0x2a
    Down: 0 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x22
    Down: 0 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x20
    

    master, Conhost, Ctrl+alt+space, (i/o modes):(0x 3f7, 0x f) (VT input)

    Down: 1 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x28
    Down: 1 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x2a
    Down: 1 Repeat: 1 KeyCode: 0x0 ScanCode: 0x0 Char: ^[ (0x1b) KeyState: 0x0
    Down: 1 Repeat: 1 KeyCode: 0x0 ScanCode: 0x0 Char: \0 (0x0) KeyState: 0x0
    Down: 0 Repeat: 1 KeyCode: 0x20 ScanCode: 0x39 Char: \0 (0x0) KeyState: 0x2a
    Down: 0 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x22
    Down: 0 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x20
    

    master, Conhost, Ctrl+alt+shift+2, (i/o modes):(0x 1f7, 0x f) (Win32 input)

    Down: 1 Repeat: 1 KeyCode: 0x10 ScanCode: 0x2a Char: \0 (0x0) KeyState: 0x30
    Down: 1 Repeat: 1 KeyCode: 0x11 ScanCode: 0x1d Char: \0 (0x0) KeyState: 0x38
    Down: 1 Repeat: 1 KeyCode: 0x12 ScanCode: 0x38 Char: \0 (0x0) KeyState: 0x3a
    Down: 1 Repeat: 1 KeyCode: 0x32 ScanCode: 0x3 Char: \0 (0x0) KeyState: 0x3a
    Down: 0 Repeat: 1 KeyCode: 0x32 ScanCode: 0x3 Char: \0 (0x0) KeyState: 0x3a
    Down: 0 Repeat: 1 KeyCode: 0x10 ScanCode: 0x2a Char: \0 (0x0) KeyState: 0x20
    

    NOTE: this keystroke is what conpty is attempting to replicate

  6. zadjii-msft commented on Apr 1, 2020

    @zadjii-msft
    MemberAuthor

    Okay I see what the problem is - I think it is the think Leonard Hecker (@lhecker) called out in #4192. Even in straight-up conhost, Ctrl+alt+shift+2 does not generate \x1b\x0.

    IMO, Leonard Hecker (@lhecker) if you want to do the fix proposed in #4192, the ch != 0 fix, then I say go for it, and add this to the growing list of bugs that PR fixes 😆

    I'll submit the other thing I found during the course of this investigation, but that will effectively hide the showkey -a repro case in the Terminal.

  7. changed the title [-]Investigate Ctrl+Alt+Space in conpty[/-] [+]Ctrl+Alt+Shift+2 doesn't send NUL in VT input mode[/+] on Apr 1, 2020
  8. lhecker commented on Apr 1, 2020

    @lhecker
    Member

    Mike Griese (@zadjii-msft) I believe we're misunderstanding each other.

    If I press Ctrl+Alt+Space or Ctrl+Alt+Shift+2, showkey -a will show the following output:

    Ctrl+Alt condition showkey -a output
    ch >= 0x40 && ch < 0x7F ^@
    ch != 0 ^[^R

    But as far as I can see it should be ^[^@, right?
    Or am I misunderstanding you and ^[^R is actually correct? (...because, as you mentioned above, there's another bug involved here, which distorts the showkey output?)

    That said, would you'd like me to modify #4192 to use a ch != 0 condition?
    I can't decide by myself if I should. 😄

  9. zadjii-msft commented on Apr 1, 2020

    @zadjii-msft
    MemberAuthor

    Oh jeez, you should be seeing ^[^@. Now I'm not sure what the right fix is.

  10. lhecker commented on Apr 1, 2020

    @lhecker
    Member

    I figured it out. I didn't realize I had to manually attach myself to OpenConsole.exe for debugging. 🙈
    You basically already said all the right things (especially steps 2 and 3 above), but I didn't understand you, because I've been lacking the knowledge how all the code works together.

    Anyways... I fixed the problem. (lol)
    Turns out if you use MapVirtualKeyW you're probably doing something wrong. I guess? 😄

    I replaced this:

    auto wchPressedChar = gsl::narrow_cast<wchar_t>(MapVirtualKeyW(keyEvent.GetVirtualKeyCode(), MAPVK_VK_TO_CHAR));

    with a call to ToUnicodeEx, similar to Terminal::_CharacterFromKeyEvent.
    This call allows us to reliably turn the input event Alt+Ctrl+Shift+2 into an Alt+Ctrl+@ event, which then gets translated to Alt+0x00 and sent away. 🙂

    Would you like me to add it to #4192 or open a separate PR afterwards?
    I believe it makes sense to fix it separately.

  11. zadjii-msft commented on Apr 1, 2020

    @zadjii-msft
    MemberAuthor

    I didn't realize I had to manually attach myself to OpenConsole.exe for debugging.

    Literally my least favorite thing about using VS. JUST ATTACH TO CHILD PROCESSES. Or at least give me an option to auto-attach, sheesh.

    Turns out if you use MapVirtualKeyW you're probably doing something wrong

    You're sure right about that 😄 Glad you got that figured out!

    Would you like me to add it to #4192 or open a separate PR afterwards?

    Let's open another PR, since this has gotten bigger than a oneline fix ☺️

  12. 5 remaining items

  13. DHowett-MSFT commented on Apr 3, 2020

    @DHowett-MSFT
    Contributor

    Milestoning this for 1.x and taking off Triage

  14. ghost added
    In-PRThis issue has a related PR
    on Apr 7, 2020
  15. ghost closed this as completedin #5272on Feb 8, 2021
  16. ghost removed
    In-PRThis issue has a related PR
    on Feb 8, 2021
  17. ghost added
    Resolution-Fix-CommittedFix is checked in, but it might be 3-4 weeks until a release.
    on Feb 8, 2021
  18. ghost added a commit that references this issue on Feb 8, 2021
    b009d06
  19. added a commit that references this issue on Feb 11, 2021
    38da2ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area-InputRelated to input processing (key presses, mouse, etc.)Issue-BugIt either shouldn't be doing this or needs an investigation.Priority-2A description (P2)Product-ConptyFor console issues specifically related to conptyResolution-Fix-CommittedFix is checked in, but it might be 3-4 weeks until a release.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions