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 #7064: Ignore key events without scan code #7145

Merged
1 commit merged into from
Aug 6, 2020
Merged

Fix #7064: Ignore key events without scan code #7145

1 commit merged into from
Aug 6, 2020

Conversation

lhecker
Copy link
Member

@lhecker lhecker commented Aug 1, 2020

Up until #4999 we deferred all key events to the character event handler
for which ToUnicodeEx returned a valid character and alternatively
those who aren't a special key combination as listed in
TerminalInput's implementation.

Since #4999 we started acknowledging/handling all key events no matter
whether they're actually a known key combination. Given non-ASCII inputs
the Win32 SendInput() method generates certain sequences that aren't
recognizable combinations though and if they're handled by the key event
handler no follow up character event is sent containing the unicode
character.

This PR adds another condition and defers all key events without scan
code (i.e. those not representable by the current keyboard layout) to
the character event handler.

I'm absolutely not certain that this PR doesn't have a negative effect
on other kinds of inputs.

Is it common for key events to not contain a scan code? I personally
haven't seen it happen before AutoHotKey/SendInput.

Before this PR is merged it'd be nice to have a good testing plan in
place in order to ensure nothing breaks.

Validation Steps Performed

Remapped AltGr+8 to » using AutoHotKey using <^>!8::SendInput {Raw}».
Ensured » is printed if AltGr+8 is pressed.

Closes #7064
Closes #7120

@ghost ghost added Area-Input Related to input processing (key presses, mouse, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Terminal The new Windows Terminal. labels Aug 1, 2020
@zadjii-msft
Copy link
Member

This seems reasonable to me, but I also don't really know if there are other kinds of inputs without scan codes. I guess I haven't seen it, but I'm also not usually looking for that.

I'm not sure how we could be more pre-emptive about other input methods that we don't know, but we could make sure to add tests to cover this case, so that we don't regress it again in the future. Maybe something like just testing that HandleKey with a 0 scancode returns false? I know that doesn't check that we still get a character event afterwards, but it's something...

@lhecker
Copy link
Member Author

lhecker commented Aug 3, 2020

I‘ll add a unit test as soon as I can. 🙂

@miniksa
Copy link
Member

miniksa commented Aug 4, 2020

I don't really see a usage of 0 scan code that would turn into something we could know how to handle without further user32 intervention.

So I'm okay with this being "it's better than it was and if something proves us wrong, we'll refine further then."

@lhecker
Copy link
Member Author

lhecker commented Aug 4, 2020

PR fixed. 👍

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

Sure let's try it out, and refine if someone screams in the future ☺️

@zadjii-msft zadjii-msft added the Needs-Second It's a PR that needs another sign-off label Aug 5, 2020
@ghost ghost requested review from miniksa, carlos-zamora, DHowett and leonMSFT August 5, 2020 14:56
@DHowett
Copy link
Member

DHowett commented Aug 6, 2020

Okay, let's scream test this!

@ghost ghost requested a review from PankajBhojwani August 6, 2020 01:01
@ghost ghost requested a review from DHowett August 6, 2020 01:03
@DHowett DHowett added the AutoMerge Marked for automatic merge by the bot when requirements are met label Aug 6, 2020
@ghost
Copy link

ghost commented Aug 6, 2020

Hello @DHowett!

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 b617c43 into microsoft:master Aug 6, 2020
@DHowett
Copy link
Member

DHowett commented Aug 6, 2020

As always, thank you @lhecker!

DHowett pushed a commit that referenced this pull request Aug 11, 2020
Up until #4999 we deferred all key events to the character event handler
for which `ToUnicodeEx` returned a valid character and alternatively
those who aren't a special key combination as listed in
`TerminalInput`'s implementation.

Since #4999 we started acknowledging/handling all key events no matter
whether they're actually a known key combination. Given non-ASCII inputs
the Win32 `SendInput()` method generates certain sequences that aren't
recognizable combinations though and if they're handled by the key event
handler no follow up character event is sent containing the unicode
character.

This PR adds another condition and defers all key events without scan
code (i.e. those not representable by the current keyboard layout) to
the character event handler.

I'm absolutely not certain that this PR doesn't have a negative effect
on other kinds of inputs.

Is it common for key events to not contain a scan code? I personally
haven't seen it happen before AutoHotKey/SendInput.

Before this PR is merged it'd be nice to have a good testing plan in
place in order to ensure nothing breaks.

## Validation Steps Performed

Remapped `AltGr+8` to `»` using AutoHotKey using `<^>!8::SendInput {Raw}»`.
Ensured `»` is printed if `AltGr+8` is pressed.

Closes #7064
Closes #7120

(cherry picked from commit b617c43)
@ghost
Copy link

ghost commented Aug 13, 2020

🎉Windows Terminal Preview v1.2.2234.0 has been released which incorporates this pull request.:tada:

Handy links:

ghost pushed a commit that referenced this pull request Aug 14, 2020
#7145 introduced a check so that we wouldn't dispatch keys unless they
actually had a scancode. Our synthetic events actually _didn't_ have
scancodes. Not because they couldn't--just because they didn't.

Fixes #7297
DHowett added a commit that referenced this pull request Aug 24, 2020
#7145 introduced a check so that we wouldn't dispatch keys unless they
actually had a scancode. Our synthetic events actually _didn't_ have
scancodes. Not because they couldn't--just because they didn't.

Fixes #7297

(cherry picked from commit aecd99e)
zadjii-msft pushed a commit that referenced this pull request Aug 8, 2023
…virtual keycode and a scan code of 0 (#15753)

## Summary
Applications like PowerToys, with their keyboard remapping features
frequently (i.e whenever a remapped shortcut is triggerred) send
`KeyEvent` with out-of-range virtual keycode values (E.g. 0xFF). This is
fixed for WT in #7145, we just needed it in our good ol' `conhost`.

After this PR, Key events with an invalid virtual keycode and
scancode==0 are ignored, and are not added to the `InputBuffer`. Incase,
only virtual keycode is valid but not scancode, we will try to infer the
correct scancode using the virtual keycode mapping.

## References and Relevant Issues
#7145 
#7064 

## Validation Steps Performed

- Triggered a remapped shortcut and verified that `showkey -a` doesn't
output `^@` unexpectedly.
- Key events with an Invalid virtual Keycode and Scancode == 0 are
ignored.
- This PR doesn't include any changes for `WM_[SYS][DEAD]CHAR` messages,
they are left unchanged.
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.) AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Second It's a PR that needs another sign-off Priority-2 A description (P2) Product-Terminal The new Windows Terminal.
Projects
None yet
4 participants