Handling KeyDown events does not prevent TextInput events
See original GitHub issueDescribe 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:
- Created 2 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

It’s more complicated than that.
So we should return the “handled” state to the system when possible, but that kinda conflicts with dispatcher priorities.
Ok, so it seems that the way to do this is to not call
TranslateMessageif theWM_KEYDOWNis handled, however we have a problem:TranslateMessageis called before theDispatchMessagewhich dispatches theWM_KEYDOWNto theWindowImplso we don’t know whether to callTranslateMessageor not until after we’ve calledDispatchMessage, at which point it’s too late!Looks like WPF does this by first calling the
OnPreprocessMessagemethod outside of theWndProc, which handles key down messages and returns the handled status.