Cod_Ubau
Cod_Ubau

Reputation: 37

Using Keyboard as input for XAML and C# Calculator

I am creating a calculator using Visual Studio (C# and XAML). I want to be able to type the numbers and operation to do the calculation. without having to click on the result screen (Just like the windows default calculator). How can I achieve that? I am creating the calculator with MVVM concepts just as exercise to learn MVVM.

Upvotes: 0

Views: 1428

Answers (2)

Lee O.
Lee O.

Reputation: 3322

You will want to use a KeyBinding in your XAML and bind a command to the key.

<Window.InputBindings>
   <KeyBinding Key="D1"
               Command="AddOneToEntryCommand" />
   <KeyBinding Key="Add"
               Command="AddCommand" />
   <KeyBinding Key="Subtract"
               Command="SubtractCommand" />
   <!-- Add bindings for all the keys and handle the logic in your commands -->
</Window.InputBindings>

Upvotes: 3

Slashy
Slashy

Reputation: 1881

You could simply use the KeyUp event to capture the keys the user entered.

Add it to your xaml window:

 KeyUp="Window_KeyUp"

private void Window_KeyUp(object sender, KeyEventArgs e)
    {
      string str=   e.Key.ToString();
        //do whateveer you want with that.

    }

Goodluck.

Upvotes: 1

Related Questions