question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling KeyDown events does not prevent TextInput events

See original GitHub issue

Describe the bug In WPF when one handles KeyDown then TextInput event won’t be received. This is useful when handling single character shortcuts and avoiding weird side effects. For example in my app user can rebind their shortcuts, one of such shortcuts focuses an input text box. When such letter shortcut is handled we will mark KeyEventArgs as handled and focus the text box. After text box gets focused it receives text input for the handled letter and immediately types something in it.

Expected behavior I would expect WPF like behavior where handled KeyDown does not produce TextInput.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Windows
  • Version: master

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kekekekscommented, May 17, 2021

It’s more complicated than that.

  1. KeyboardDevice doesn’t necessary have the required information to decide if the event should be ignored
  2. Depending on the system text input might be decoupled from actual event presses. Sometimes the app won’t see the key press at all

So we should return the “handled” state to the system when possible, but that kinda conflicts with dispatcher priorities.

0reactions
grokyscommented, Jan 12, 2022

Ok, so it seems that the way to do this is to not call TranslateMessage if the WM_KEYDOWN is handled, however we have a problem: TranslateMessage is called before the DispatchMessage which dispatches the WM_KEYDOWN to the WindowImpl so we don’t know whether to call TranslateMessage or not until after we’ve called DispatchMessage, at which point it’s too late!

Looks like WPF does this by first calling the OnPreprocessMessage method outside of the WndProc, which handles key down messages and returns the handled status.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent JavaScript keydown event from being handled ...
In a keyDown function (I have added the document.addEventListener code). Now it works just fine, and does exactly what I want it to...
Read more >
Keyboard: keydown and keyup
Keyboard events should be used when we want to handle keyboard actions (virtual keyboard also counts). For instance, to react on arrow keys...
Read more >
Event: preventDefault() method - Web APIs | MDN
... event does not get explicitly handled, its default action should not ... autocomplete to prevent the browser from filling in the input...
Read more >
KeyboardEvent: key property - Web APIs | MDN
The beforeinput and input events are fired next because a character key has been produced. As we keep holding the key, the keydown...
Read more >
Modify keyboard key events - Windows Forms .NET
Setting the Handled property in the KeyDown event handler does not prevent the KeyPress and KeyUp events from being raised for the current ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found