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

TerminalControl handles ALT+NUMPAD input poorly #1401

Closed
leoniDEV opened this issue Jun 22, 2019 · 17 comments · Fixed by #4965
Closed

TerminalControl handles ALT+NUMPAD input poorly #1401

leoniDEV opened this issue Jun 22, 2019 · 17 comments · Fixed by #4965
Assignees
Labels
Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Help Wanted We encourage anyone to jump in on these. Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.
Milestone

Comments

@leoniDEV
Copy link

leoniDEV commented Jun 22, 2019

When the user enters Alt-123 it looks like we're doing this:

  • Sending ESC 1
  • Sending 2
  • Sending 3
  • Sending whichever character Alt-123 corresponds to.

Powershell and bash (readline) interpret alt-number as "enter digit-argument mode." Digit argument mode usually means "repeat the first character N times" ... so this results in character 123 being added to the buffer 123 times.

We should definitely fix that.

original content

Environment

Windows build number: 10.0.18922.1000 and 10.0.18362.175
Windows Terminal version (if applicable): 0.2.1715.0

Any other software?
Powershell Core 7.0.0-preview.1
Powershell Core 6.2.1
Windows Powershell 5.1

Steps to reproduce

  • Open Powershell (whatever version listed above) session in the Terminal
  • try to insert an ascii char by:
    • press and hold ALT key
    • digit whatever number (for example 126 corresponding to ~)

Expected behavior

a tilde (~) is inserted
WinTerPS7ok

Actual behavior

127 tildes are inserted
WinTerPS7

The same thing happens for all the versions of Powershell listed above

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jun 22, 2019
@Shorotshishir
Copy link

Weird behavior in all terminals: Numpad on/off state changes the output

  • WSL is making multiples of the corresponding ASCII character while Numpad is active (i.e. num lock on) ↓↓

wsl alt+5

  • CMD is making adding the number corresponding ASCII character while Numpad is active (i.e. num lock on) ↓↓

cmd alt+5

  • PowerShell is making multiples of the corresponding ASCII character while Numpad is active (i.e. num lock on) character won't show. ↓↓

powershell alt+5

@DHowett-MSFT DHowett-MSFT changed the title Wired behavior when insert characters using ALT+ASCII CODE in Powershell TerminalControl handles ALT+NUMPAD input poorly Jun 26, 2019
@DHowett-MSFT DHowett-MSFT added Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Help Wanted We encourage anyone to jump in on these. Issue-Bug It either shouldn't be doing this or needs an investigation. Product-Terminal The new Windows Terminal. and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Jun 26, 2019
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Jun 26, 2019
@DHowett-MSFT
Copy link
Contributor

When the user enters Alt-123 it looks like we're doing this:

  • Sending ESC 1
  • Sending 2
  • Sending 3
  • Sending whichever character Alt-123 corresponds to.

Powershell and bash (readline) interpret alt-number as "enter digit-argument mode." Digit argument mode usually means "repeat the first character N times" ... so this results in character 123 being added to the buffer 123 times.

We should definitely fix that.

@lhecker
Copy link
Member

lhecker commented Jun 26, 2019

If someone (potentially inexperienced with this project) wants to debug this issue they might find the following line of interest:

_SendEscapedInputSequence(keyEvent.GetCharData());

It's the one responsible for generating the escape codes.

The following callback then sends these further to the shell:

auto passAlongInput = [&](std::deque<std::unique_ptr<IInputEvent>>& inEventsToWrite) {
if (!_pfnWriteInput)
{
return;
}
std::wstring wstr = _KeyEventsToText(inEventsToWrite);
_pfnWriteInput(wstr);
};

(_pfnWriteInput goes straight to TermControl::_SendInputToConnection in our case which is then the last stage before the shell I suppose).
If you hold Alt and enter 64 on the Numpad it will then generate the following input sequence for the shell: \x1b6\x1b4@ (\x1b being the escape character).

I'm personally not entirely certain where the @ is coming from, but I suspect this is generated by the Operating System and sent as a WM_CHAR message to the application here:

void TermControl::_CharacterHandler(winrt::Windows::Foundation::IInspectable const& /*sender*/,
Input::CharacterReceivedRoutedEventArgs const& e)

If I'm correct, then to go fully UNIXy we might want to prevent the OS from translating Alt+Numpad for us (however one would do that). Otherwise we should disable the call to _SendEscapedInputSequence I guess.

@zadjii-msft zadjii-msft added this to the Terminal v1.0 milestone Jul 2, 2019
@zadjii-msft zadjii-msft added the Priority-2 A description (P2) label Jan 22, 2020
@ghost ghost added the In-PR This issue has a related PR label Mar 17, 2020
@ghost ghost closed this as completed in #4965 Mar 18, 2020
ghost pushed a commit that referenced this issue Mar 18, 2020
## Summary of the Pull Request
Alt-Numpad# input would be escaping each numkey before sending it through. This would result in some weird behavior, for example, in powershell, where the first alt-numpad# would start digit argument and once the user releases alt, a character is sent through and digit argument would repeat that character X times. To resolve this, we simply need to ignore KeyDowns where Alt is held and a Numpad# is pressed. 

Once Alt is released, we'll receive a character through `TSFInputControl`, not `TermControl::CharacterHandler`. It seems that the `CoreTextEditContext` in `TSFInputControl` intercepts the character before it gets to `TermControl`. TSF will then send the received character through as normal.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [x] Closes #1401 
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [x] Tests added/passed

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
Testing various combinations of Alt-Numpad# consistently sends through only one instance of the expected symbols.
@ghost ghost added Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. and removed In-PR This issue has a related PR labels Mar 18, 2020
DHowett-MSFT pushed a commit that referenced this issue Mar 19, 2020
## Summary of the Pull Request
Alt-Numpad# input would be escaping each numkey before sending it through. This would result in some weird behavior, for example, in powershell, where the first alt-numpad# would start digit argument and once the user releases alt, a character is sent through and digit argument would repeat that character X times. To resolve this, we simply need to ignore KeyDowns where Alt is held and a Numpad# is pressed.

Once Alt is released, we'll receive a character through `TSFInputControl`, not `TermControl::CharacterHandler`. It seems that the `CoreTextEditContext` in `TSFInputControl` intercepts the character before it gets to `TermControl`. TSF will then send the received character through as normal.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [x] Closes #1401
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [x] Tests added/passed

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
Testing various combinations of Alt-Numpad# consistently sends through only one instance of the expected symbols.

(cherry picked from commit cddac25)
@ghost
Copy link

ghost commented Mar 20, 2020

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

Handy links:

@ilqvya
Copy link

ilqvya commented Apr 10, 2020

Still reproduces:
image

@ilqvya
Copy link

ilqvya commented Apr 10, 2020

bug

@ilqvya
Copy link

ilqvya commented Apr 10, 2020

@ilqvya ilqvya mentioned this issue Apr 10, 2020
@zadjii-msft zadjii-msft reopened this Apr 10, 2020
@leonMSFT
Copy link
Contributor

Sorry about it still happening @effolkronium! From your repro, it almost looks like your Terminal never got the PR that fixed the thing where digit-argument starts #4965. I tried to repro this on a bunch of my machines. A couple of them are running a version of Terminal that's further ahead, but I have a couple that are also running 0.10.781.0, and it's working fine for me (so that's a real head scratcher). 😢 I'm also asking the rest of the team to see if they can repro what you're seeing, and so far no luck, I can't get a reliable repro for what you're seeing. For now the only thing I can suggest is I'd like for you to try reinstalling Terminal if you get a chance (this is a wild guess that may or may not work).

@ilqvya
Copy link

ilqvya commented Apr 11, 2020

@leonMSFT

I think ALT worked well day or week ago for me.

as i can see terminal got the update at the end of last month. So i think it was ok with this version some time

I just re -installed the Terminal but the bug still reproduces. ^(((

...

Also it doesn't work with update before current. Maybe some OS or third party app impact...

I'll try to investigate a bit, maybe even debug the terminal by myself

@ilqvya
Copy link

ilqvya commented Apr 11, 2020

Hmm, seems it do not recognize alt at all (the breakpoint when i press alt and then number)

image

@ilqvya
Copy link

ilqvya commented Apr 11, 2020

Ok, so this is my VirtKey for number 2

image

The check for NumberPad0 - NumberPad9 fails. I think it's because i press numbers below F row. not numpad.

@ilqvya
Copy link

ilqvya commented Apr 11, 2020

Well, ignoring VirtualKey::Number0 - VirtualKey::Number9 doesn't help

it passes check but tab switch still no happening.

@leonMSFT

could you suggest what i should debug or look to for fix ?

@DHowett-MSFT
Copy link
Contributor

dude, Alt NUMPAD input only works if you use the number pad. The numbers below the F keys aren’t the number pad.

@ilqvya
Copy link

ilqvya commented Apr 11, 2020

dude, Alt NUMPAD input only works if you use the number pad. The numbers below the F keys aren’t the number pad.

Man, it worked well all the time when I used this app in past. I have no numpud on my keyboard

@lhecker
Copy link
Member

lhecker commented Apr 11, 2020

Correct me if I'm wrong, but...

@effolkronium Unfortunately if that has ever worked it must've been a bug anyways. Entering Alt codes with regular number keys most definitely wasn't a feature. I'm not aware of Windows supporting Alt codes with regular number keys in the first place.
Even DEC's later terminals (which this project is replicating to a large extent) only supported composition using the numeric keypad.

Some keyboards with Fn keys support entering numeric keypad codes using the main keyboard.
On one keyboard I had pressing FnM was the same as pressing Numpad0.
If your keyboard doesn't support that either you'll unfortunately not be able to enter Alt codes.
Your only alternative is Win.. As I said above, entering Alt codes with regular number keys most likely is not something this project will support in the future, as Windows itself normally doesn't support that either.

@ilqvya
Copy link

ilqvya commented Apr 11, 2020

oK, so it works if i press alt+ctr+num.

i think ticket should be closed

@DHowett-MSFT
Copy link
Contributor

@lhecker thanks for the excellent write up
@effolkronium thanks for confirming that you found a solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Help Wanted We encourage anyone to jump in on these. Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Terminal The new Windows Terminal. Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants