My form has several buttons such as “Scan” and “Exit”. I have seen in many programs where buttons will be useable by keypress. Lots of times the unique key to press is underlined in the text on the button (I don’t know how to use an underline option on these forums!). I went to the form and added a keypress event:
private void Form1_KeyPress(object sender, KeyPressEventArgs key)
{
switch (key.ToString())
{
case "s":
Run_Scan();
break;
case "e":
Application.Exit();
break;
default:
MessageBox.Show("I'll only accept 's' or 'e'.");
break;
}
}
But then pressing ‘s’ or ‘e’ on the form doesn’t do anything. Not sure where I’m going wrong here?
Overriding ProcessKeyCommand will accept input from anywhere on the form. You should add a modifier however, since pressing ‘s’ or ‘e’ in a textbox, for example, will also trigger the action.