Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter Key Issue

I'm currently making a calculator app for my .NET class. The calculator works perfectly fine when I use the buttons. But when I use keys on the keyboard, it doesn't work so well. For example, when I press the Enter key, I want the calculator to calculate the problem in the text box. Instead, the enter presses the button I had last clicked which leads to issues. How can I prevent this from happening?

I know that's a odd explanation, but I don't know how to explain it better.

like image 734
Dhaval Desai Avatar asked Nov 09 '22 10:11

Dhaval Desai


1 Answers

On the root level of your window/user control add an InputBinding for the return keystroke. This will intercept the keystroke and execute your command as if you had clicked the button (assuming it's Command property is also bound to that command).

Example with a UserControl:

<UserControl.InputBindings>
    <KeyBinding Key="Return" Command="{Binding EvaluateCommand}"/>
</UserControl.InputBindings>    
like image 194
BradleyDotNET Avatar answered Nov 14 '22 22:11

BradleyDotNET