It is not clear to me what you mean by input keyboard. There are two possible meanings.
You can choose one character within the Content attribute to be used as the AccessKey, as described in #838 – Using a Label’s Access Key to Give Focus to Another Control. So in other words if you change (for the relevant button) Content="1"
to Content="_1"
then when the "1" key is pressed then that will work the exact same way as if the "1" button is pressed.
If you do that then you normally would have a separate event handler for each button. For your form here you have NumberButton_Click
as the event handler for every number. You could create a separate event handler for each number but if you want you can modify NumberButton_Click
so that it can determine which number was pressed, as in the following:
private void NumberButton_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
string s = button.Content.ToString();
if (s.Length != 2)
{
System.Diagnostics.Debug.WriteLine($"NumberButton content is not valid");
return;
}
if (calcDone) //calculation already done
{
result.Content = s[1];
calcDone = false;
}
else //calculation not yet done
{
if (result.Content.ToString() == "0")
{
result.Content = s[1];
}
else
{
result.Content = result.Content.ToString() + s[1];
}
}
}
That is if the Content is in the format "_n" where n is a number. You can use the similar code for OperationButton_Click
. For the AC button you need to choose one character, either A or C, to be used as the access key. I am not sure what the "+/-" button is for but you will need to choose one character to be used as the access key for it. If it is for toggling the number between positive and negative then I am not sure what to do; the Windows Calculator accessory has such a button and as best as I can tell it cannot be used from the keyboard.
Another possibility requires the understanding of focus. Unless a key is specified for use as an accelerator or (as in the preceding) an access key, Windows normally sends keyboard input to the control with focus. So you need a text box that has the focus to get keyboard input. I am sure there have been times when you type something into an application and nothing happens until you click something like a text box. When you do you are setting the text box as the control with the focus. You might not need to use a text box for that if everything can be done using access keys but note that the Windows Calculator accessory has such a text box.