-1

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();
    }
}
user3002135
  • 237
  • 1
  • 4
  • 15

3 Answers3

1

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();
    }
}
scheien
  • 2,457
  • 1
  • 19
  • 22
  • i don't know why but nothing happens when i press insert. maybe im to stupid for this :/ – user3002135 Jan 29 '14 at 19:42
  • @user3002135 - you shold add the `this.KeyDown += ....` after the `InitializeComponents()` inside the constructor `public Form1() {...}` – scheien Jan 30 '14 at 07:20
  • i did but still wont work. it seems strange to me because i tried everything after all the instructions i have found and none of those worked... – user3002135 Jan 30 '14 at 11:56
  • @user3002135 - Can you update your question with code related to the wire up? Hard to tell what's wrong when you don't supply any more code than the method you are trying to call. – scheien Jan 30 '14 at 12:06
1

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.

EZI
  • 15,209
  • 2
  • 27
  • 33
  • @user3002135 Of course it works. `This isn't working` doesn't give any info. How did you test it. Did you get an error? Where is your code? – EZI Jan 29 '14 at 19:58
  • well when i use the key it wont even enter the void. – user3002135 Jan 30 '14 at 11:57
  • @user3002135 You insist on not posting any more info other than saying *not working*. This is all I can do to help you with the available info. – EZI Jan 30 '14 at 21:51
0

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)

Brandon
  • 4,491
  • 6
  • 38
  • 59