Skip to content

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

Merged
1 commit merged into
microsoft:masterfrom
lhecker:issue-7064-fix
Aug 6, 2020
Merged

1 commit merged into
microsoft:masterfrom
lhecker:issue-7064-fix

Conversation

@lhecker

@lhecker Leonard Hecker (lhecker) commented Aug 1, 2020 •

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member Author

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

@miniksa

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Member Author

PR fixed. 👍

@zadjii-msft Mike Griese (zadjii-msft) left a comment

Copy link
Copy Markdown
Member

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 ☺️

@DHowett

Copy link
Copy Markdown
Member

Okay, let's scream test this!

@ghost
ghost requested a review from PankajBhojwani August 6, 2020 01:01
@ghost
ghost requested a review from Dustin L. Howett (DHowett) August 6, 2020 01:03
@DHowett Dustin L. Howett (DHowett) added the AutoMerge Marked for automatic merge by the bot when requirements are met label Aug 6, 2020
@ghost

Copy link
Copy Markdown

Hello Dustin L. Howett (@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
Copy Markdown
Member

As always, thank you Leonard Hecker (@lhecker)!

Dustin L. Howett (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
Copy Markdown

🎉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
Dustin L. Howett (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)
Mike Griese (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