Avalonia Keyboard Input

December 1, 2024

I've been playing around with Avalonia cross platform UI framework and having a torrid time trying to get it to accept keyboard input.

I had one window, that opened a secondary dialog window fullscreen, and I wanted keypresses to go to the second window when it was open. I was finding that the keypresses were all going to the first window, and the second just ignored them.

Here's the code I was using that wasn't working.

public SecondaryWindow()
{
    InitializeComponent();
    AddHandler(InputElement.KeyUpEvent, HandleKey, RoutingStrategies.Tunnel);
}

private void HandleKey(object? sender, KeyEventArgs e)
{
    throw new NotImplementedException();
}

If I added the same handler to the first window, then that always received the keypresses.

I fixed it by adding `Focusable="True" into my Window definition in the XAML. I don't really understand why this defaulted to false, but I'm pleased to have something that works now

References

  • https://avaloniaui.net/