How can I fire a key press event on a keyboard when a user presses enter button wpf

c#, widget, wpf

Solution

Set the `IsDefault` property of the `Button` to `True`

Problem

I have a login button in wpf login form. I want that user has two options, the first one is to have user fill the username and password and press the login button using mouse, and the second option is that a user can press the ENTER key from keyboard. How can I get this to work? The mouse click does the job, but pressing ENTER does not work. ``` <Button Canvas.Left="157" Canvas.Top="292" Height="24" BorderThickness="0" BorderBrush="White" Content="Login" Name="btnLogin" Width="99" Click="btnLogin_Click" Keyboard.KeyDown="bttnLogin_Enter" /> ``` And My code for this is :--- ``` private void bttnLogin_Enter(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { try { /// My code here } catch() { } } } ```

Original source