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

Fix VT sequences for Ctrl+Alt+? input #4947

Merged
4 commits merged into from
Mar 18, 2020
Merged

Conversation

zadjii-msft
Copy link
Member

@zadjii-msft zadjii-msft commented Mar 16, 2020

Summary of the Pull Request

Ctrl+/ and Ctrl-? are complicated in VT input.

  • C-/ is supposed to be ^_ (the C0 character US)
  • C-? is supposed to be DEL
  • C-M-/ is supposed to be ^[^_ (ESC US)
  • C-M-? is supposed to be ^[^? (ESC DEL)

The astute reader will note that these characters also share the same key on a
standard en-us keyboard layout, which makes the problem even more complicated.
This PR does a better job of handling these weird cases.

References

PR Checklist

Validation Steps Performed

  • ran tests
  • checked showkey -a in gnome-terminal, which gives you the wrong results for C-M-/, C-M-?
  • checked showkey -a in xterm, which gives you the right results for C-M-/, C-M-?
  • checked showkey -a in conhost
  • checked showkey -a in Windows Terminal

## Summary of the Pull Request

Ctrl+/ and Ctrl-? are complicated in VT input.

* C-/ is supposed to be `^_` (the C0 charater US)
* C-? is supposed to be `DEL`
* C-M-/ is supposed to be `^[^/`
* C-M-? is supposed to be `^[?`

The astute reader will note that these characters also share the same key on a
standard en-us keyboard layout, which makes the problem even more complicated.
This PR does a better job of handling these weird cases.

## PR Checklist
* [x] Closes #3079
* [x] I work here
* [x] Tests added/passed
* [n/a] Requires documentation to be updated

## Validation Steps Performed

* ran tests
* checked `showkey -a` in gnome-terminal
* checked `showkey -a` in conhost
* checked `showkey -a` in Windows Terminal
@zadjii-msft zadjii-msft added Area-Input Related to input processing (key presses, mouse, etc.) Area-VT Virtual Terminal sequence support Product-Conhost For issues in the Console codebase labels Mar 16, 2020
src/terminal/input/terminalInput.cpp Outdated Show resolved Hide resolved
Copy link
Member

@miniksa miniksa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check this with an alternate keyboard layout? I hope so. It looks like it will be robust enough to handle it with the VkKeyScan matching.

The only other comment I had was the one Carlos flagged with the typo. So approving.

Co-Authored-By: Carlos Zamora <carlos.zamora@microsoft.com>
const bool shift = keyEvent.IsShiftPressed();

// From the KeyEvent we're translating, synthesize the equivalent VkKeyScan result
const short keyScanFromEvent = keyEvent.GetVirtualKeyCode() |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is quite clever!

should it be a local static function that takes a keyEvent instead? 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i don't think it should be a member on keyevent because that's too win32-specific)

const bool shift = keyEvent.IsShiftPressed();

// From the KeyEvent we're translating, synthesize the equivalent VkKeyScan result
const short keyScanFromEvent = keyEvent.GetVirtualKeyCode() |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i don't think it should be a member on keyevent because that's too win32-specific)

src/terminal/input/terminalInput.cpp Outdated Show resolved Hide resolved
@ghost ghost added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 17, 2020
@j4james
Copy link
Collaborator

j4james commented Mar 18, 2020

C-/ is supposed to be ^_ (the C0 charater US)
C-? is supposed to be DEL
C-M-/ is supposed to be ^[^/
C-M-? is supposed to be ^[?

For the record, I think this is wrong. If you look at Mintty, or Xterm (with the meta sends escape mode set), you'll get different results:

Ctl+/ is ^_ (US)
Ctl+? is ^? (DEL)
Ctl+Atl+/ is ^[^_ (ESC US)
Ctl+Atl+? is ^[^? (ESC DEL)
Alt+/ is ^[/ (ESC /)
Alt+? is ^[? (ESC ?)

Notice how it all fits a pattern. Gnome-terminal doesn't distinguish between Ctl+Alt and Alt, which definitely seems like a bug to me.

@zadjii-msft
Copy link
Member Author

@j4james great catch! I was using gnome-terminal to test, not xterm. That certainly does make more sense. I'll make sure that roundtrips as nicely through conpty as the other ones did

@ghost ghost removed the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 18, 2020
@zadjii-msft
Copy link
Member Author

Gah okay. I concur that the fixed behavior is more correct, but now ctrl+alt+? falls into the #879 category of keys. We can't differentiate between that and Alt+Bksp, but that's probably okay.

@DHowett-MSFT
Copy link
Contributor

In the future, I'd like to see if we can't just not special-case these keys.

@DHowett-MSFT DHowett-MSFT added the AutoMerge Marked for automatic merge by the bot when requirements are met label Mar 18, 2020
@ghost
Copy link

ghost commented Mar 18, 2020

Hello @DHowett-MSFT!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit d50409b into master Mar 18, 2020
@ghost ghost deleted the dev/migrie/b/3079-C-M-questionmark branch March 18, 2020 22:38
DHowett-MSFT pushed a commit that referenced this pull request Mar 19, 2020
## Summary of the Pull Request

Ctrl+/ and Ctrl-? are complicated in VT input.

* C-/ is supposed to be `^_` (the C0 character US)
* C-? is supposed to be `DEL`
* C-M-/ is supposed to be `^[^_` (ESC US)
* C-M-? is supposed to be `^[^?` (ESC DEL)

The astute reader will note that these characters also share the same key on a
standard en-us keyboard layout, which makes the problem even more complicated.
This PR does a better job of handling these weird cases.

# References
* #3079 - At first, I thought this PR would close this issue, but as I've learned below, this won't solve that one. This bug was merely found during that investigation.

## PR Checklist
* [x] Related to #3079
* [x] I work here
* [x] Tests added/passed
* [n/a] Requires documentation to be updated

## Validation Steps Performed

* ran tests
* checked `showkey -a` in gnome-terminal, which gives you the wrong results for C-M-/, C-M-?
* checked `showkey -a` in xterm, which gives you the _right_ results for C-M-/, C-M-?
* checked `showkey -a` in conhost
* checked `showkey -a` in Windows Terminal

(cherry picked from commit d50409b)
uiKeystate = LEFT_CTRL_PRESSED | LEFT_ALT_PRESSED;
vkey = LOBYTE(VkKeyScan(L'/'));
s_pwszInputExpected = L"\x1b\x1f";
TestKey(pInput, LEFT_CTRL_PRESSED | LEFT_ALT_PRESSED, vkey, L'/');
Copy link
Member

@lhecker lhecker Mar 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW This particular call fails the test if you've got a non-US keyboard attached. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that bit is unfortunate. We've got a couple other tests that make assumptions about the environment they're running in (mostly around language and system codepage) ☹️

@ghost
Copy link

ghost commented Mar 20, 2020

🎉Windows Terminal Preview v0.10.781.0 has been released which incorporates this pull request.:tada:

Handy links:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Input Related to input processing (key presses, mouse, etc.) Area-VT Virtual Terminal sequence support AutoMerge Marked for automatic merge by the bot when requirements are met Product-Conhost For issues in the Console codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants