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.

TextBox KeyDown event handling

See original GitHub issue

The TextBox control currently only handles certain KeyDown events (like Ctrl+C/V for copy/paste) and lets the rest bubble to its parents (it’s using TextInput events to handle actual text input). I was wondering how much sense it would make to unconditionally handle all key down events so that parent controls don’t get notified about these key events. AFAICT this used to be the behavior until #1357 refactored it. In my application the current behavior causes some issues with modifier-less hotkeys: Pressing space (for instance) on a TextBox will signal the parent control that is executing the hotkey.

PS: I know this question got asked internally, I am just repeating it here for visibility and community feedback.

CC: @grokys, @Gillibald

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
grokyscommented, Mar 12, 2020

Thanks for opening this @pr8x

I’ve just tested on WPF, and WPF indeed seems to have the same behavior as Avalonia:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d" Title="MainWindow">
    <TextBox></TextBox>
</Window>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Key);
            base.OnKeyDown(e);
        }
    }

Typing: test Ctrl+C Ctrl+V Ctrl+Z Ctrl+H outputs:

T
E
S
T
LeftCtrl
LeftCtrl
LeftCtrl
H

So it seems that we’re not doing anything particularly unexpected here. I’m not sure how this problem is handled in WPF applications, or if we need any extra functionality here?

0reactions
grokyscommented, Mar 17, 2020

Need to look into this, but perhaps our key binding stuff needs to be handling the tunneling events and marking them as handled?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keyboard.KeyDown Attached Event (System.Windows.Input)
The following example creates TextBox that attaches an event handler for the KeyDown event. When the Return is pressed, the event handler displays...
Read more >
Element: keydown event - Web APIs | MDN
The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, ......
Read more >
Handling Textbox Keydown Events - I.T Delinquent
Here, I will you how to recognise a keydown event on a textbox and also how to “identify” which key was pressed.
Read more >
onkeydown Event
The onkeydown event occurs when the user presses a key on the keyboard. Keyboard Events. Event, Occurs When. onkeydown, The user presses a...
Read more >
Catch and process KeyPress/KeyDown without modifying ...
Whenever I hit Ctrl+Enter, the event does get handled, but it also registers as an Enter keypress when the focus is in either...
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