When you press Enter anywhere in a HTML form
it triggers its action
, that is equivalent of pressing the submit
button. How to make a window that when I press Enter anywhere it will trigger an event?
Set the IsDefault
property on the button to true to enable the Enter key to activate that button's action. There is also the IsCancel
property that does the same thing for the Escape key.
Assign the PreviewKeyDown
event to the window in XAML then check the KeyEventArgs
in codebehind to determine if the user pressed the Enter key.
XAML code:
<Window [...] PreviewKeyDown="Window_PreviewKeyDown">
Code behind:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { // Whatever code you want if enter key is pressed goes here } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With