I am trying to include HotKeys in my program but I don't know how to execute this code:
private void Form_KeyDown(object data, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
timer1.Stop();
}
}
I am trying to include HotKeys in my program but I don't know how to execute this code:
private void Form_KeyDown(object data, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
timer1.Stop();
}
}
Have you bound that event? Sounds like it is not wired up.
public Form()
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);
}
You can bind event that way, or doubleclick the KeyDown
event in the Properties window in Visual Studio.
If you choose the point and click way, the event will bound in the Form.Designer.cs
file.
The complete code constructor and method would look like this:
public Form()
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);
}
private void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
timer1.Stop();
}
}
Just copy&paste that code to your form (I find this usage easier)
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
timer1.Stop();
}
}
EDIT
BTW: Don't forget to set true to KeyPreview
property of the form.
Per my comment:
I'm not sure about the Insert key, but you're looking for Mnemonics. On your form, use the "&" character before the character you want to shortcut. For example, on any button, menu, label etc... that says "Open", change the text to "&Open" and it will do what you want.
Edit: Keep in mind, this binds the Alt+yourCharacter key combination, not just the single key. If you're looking specifically to do special keys (insert, F1 etc...) you will need to implement a solution from the other answers (I think @QtX's solution will do what you want)