Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keys using escape sequences (arrow keys, home/end) don't work with Bash on Windows #82

Closed
rprichard opened this issue Jun 5, 2016 · 3 comments
Labels

Comments

@rprichard
Copy link
Owner

rprichard commented Jun 5, 2016

When running Bash-on-Windows under winpty, keys encoded using escape sequences have no effect.

I tested with the latest Windows 10 preview release, 14342. It looks like there is a new, undocumented mode flag on the console input handle, 0x200. In that mode, most key-down and key-up key events are still generated as before, but for keys that use escape sequences on Ubuntu, the Windows console instead generates a series of KEY_EVENT records encoding the sequence.

The escape sequence replaces the normal key-down record, which is not generated. The key-up record is generated as before. In the records, the bKeyDown and wRepeatCount fields are TRUE and 1 (as before), and the UnicodeChar has the appropriate character, but all other fields are zeroed. (In particular, wVirtualScanCode and wVirtualKeyCode are 0.)

Example from winpty-agent --show-input --escape-input for Ctrl-Alt-Shift-UP:

key: dn rpt=1 scn=0 0x0 ch=0x1b
key: dn rpt=1 scn=0 0x0 ch='['
key: dn rpt=1 scn=0 0x0 ch='A'
key: up rpt=1 scn=72 Enhanced-LAlt-LCtrl-Shift-UP ch=0

Obviously, if Windows is going to behave this way, then winpty needs to detect the 0x200 console mode and emit escape sequences.

The interesting design question is whether winpty should send its terminal input directly to Ubuntu, or whether it should continue decoding input, but reencode escape sequences.

It looks like Windows currently ignores modifier keys when escaping. If winpty were to match Windows' encoding, it would merely have to map virtual keys to an escape string. Keys include: ESC, F1-F12, INS, DEL, HOME, END, PGUP, PGDN, UP, DN, LEFT, RIGHT. That might be all of them.

@rprichard rprichard added the bug label Jun 5, 2016
rprichard added a commit that referenced this issue Jun 5, 2016
Improve debugging for the case where SetConsoleMode fails.  Apparently
on Windows 7, the call fails, but the 0x200 flag is still enabled.  A
later GetConsoleMode call succeeds, but of course, further calls to
SetConsoleMode will still fail unless they unset 0x200.

Given that the 0x200 flag is undocumented and only respected in preview
releases, I'm hesistant to clear the flag when --escape-input isn't set.

Related to #82
@rprichard
Copy link
Owner Author

winpty must decode sequences if it is to generate the key-up event records, though I suppose bash.exe probably doesn't care about those records.

@mintty
Copy link

mintty commented Aug 10, 2016

I've just tried winpty as a gateway between mintty and Ubuntu bash on Windows, see mintty/mintty#558 (comment).
In this use case, it would not be necessary to generate console-style events for individual key sequences because the client side (Ubuntu on Windows) wants terminal-style interaction just like mintty. Is there a way to pass all escape sequences transparently so they can simply work without reiterating the huge set of terminal controls for winpty?

@rprichard
Copy link
Owner Author

Is there a way to pass all escape sequences transparently so they can simply work without reiterating the huge set of terminal controls for winpty?

There isn't a way to do that now, but it'd be easy to add, and it would make sense to use that option with mintty. Most likely, in that new mode, winpty would not bother generating any KeyUp events. I don't think WSL bash cares about them.

I suspect that mouse input still wouldn't work, though, unless the user manually passed --mouse to winpty to enable mouse input. winpty screen-scrapes a console buffer, so it won't see the escape sequence that an Ubuntu command-line program outputs to enable mouse input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants