When I open a dialogue (window) in Avalonia I'd like to close it with the Esc button. What is a (the canonical) way to implement this?
I have looked through the documentation and done the regular search.
The canonical way to close a dialog window is different than registering keybindings. The typical method of closing the dialog without action is to set the cancel button's IsCancel property to true. This will automatically intercept the escape key and route it to the button's click event or command binding.
If you do not have a Cancel button, then you will need to use keybindings as explained in other answers.
public void OnCancel(object? sender, RoutedEventArgs args)
{
this.Close();
}
<Button Content="Cancel" Click="OnCancel" IsCancel="True"/>
Likewise, you can do the same for the confirmation button ("OK") using the IsDefault property, which intercept the Enter Key (or Ctrl + Enter if you are focused on a control that accepts the key, such as a multiline text box).