33

Basically, I want to be able to trigger an event when the ENTER key is pressed. I tried this already:

private void input_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Equals("{ENTER}"))
        {
            MessageBox.Show("Pressed enter.");
        }
    }

But the MessageBox never shows up. How can I do this?

Jade
  • 3,156
  • 1
  • 34
  • 44
Jon
  • 2,566
  • 6
  • 32
  • 52
  • 9
    It might be a good idea to do this on KeyUp (depending on your circumstances). KeyUp is called once for every time a key is released.. however keydown is called constantly while the key is down.. – Simon Whitehead Aug 04 '12 at 05:32

7 Answers7

44

Give this a shot...

private void input_KeyDown(object sender, KeyEventArgs e) 
{                        
    if(e.KeyData == Keys.Enter)   
    {  
        MessageBox.Show("Pressed enter.");  
    }             
}
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • 1
    Thank you, that was very useful, enough so to find out the actual syntax for myself. The syntax for me was: if(e.Key == Key.Enter) // using System.Windows.Input – user2010136 Jul 28 '16 at 22:04
  • @user2010136: This question is about Windows Forms, it sounds like you were using WPF. – Ben Voigt Apr 23 '21 at 18:05
5

To add to @Willy David Jr answer: you also can use actual Key codes.

private void input_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyChar == 13)
    {
        MessageBox.Show("Pressed enter.");
    }
}
3

You can actually just say

private void input_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        MessageBox.Show("Pressed enter.");
    }
}
Makusium
  • 201
  • 1
  • 2
  • 15
  • This is the exact same answer as the one given in 2012. – Jon Jun 05 '18 at 16:08
  • It is actually not the _exact_ same answer, it uses `e.Key` where the others use `e.KeyCode`, `e.KeyData` and `e.KeyChar`. It would be good to have answer that addresses all the very slightly different Properties and suggested the best one to use. – Jade Jun 05 '18 at 16:53
  • 3
    This answer is applicable to WPF, the rest so far are for WinForms. – Jade Jun 05 '18 at 17:01
3

You can use the Keypress event. If you are just looking for the "Enter" keypress, then you probably don't care about modifier keys (such as Shift and/or Ctrl), which is why most would use KeyDown instead of Keypress. A second benefit is to answer the question that is almost always asked after implementing any of the other answers: "When I use the referenced code, why does pressing "Enter" cause a beep?" It is because the Keypress event needs to be handled. By using Keypress, you solve both in one place:

private void input_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter)
    {
        // Your logic here....
        e.Handled = true; //Handle the Keypress event (suppress the Beep)
    }
}
OldDog
  • 334
  • 4
  • 12
3

If your Form has AcceptButton defined, you won't be able to use KeyDown to capture the Enter.

What you should do is to catch it at the Form level. Add this code to the Form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
    {
        //do something
        return true;
    }
    else
    {
        return base.ProcessCmdKey(ref msg, keyData);
    }
}
AecorSoft
  • 414
  • 4
  • 10
0

You can also do this:

  private void input_KeyDown(object sender, KeyEventArgs e) 
  {                        
    if(e.KeyCode== Keys.Enter)   
    {  
        //Your business logic here.
    }             
  }

The only difference with KeyCode vs KeyData is that KeyCode can detect modifiers combination with KeyCode (e.g. CTRL, Shift + A) which you don't need here.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
-1

Fast forward 2022, the following statement above is completely other way around.

"The only difference with KeyCode vs KeyData is that KeyCode can detect modifiers combination with KeyCode (e.g. CTRL, Shift + A) which you don't need here."

the KeyDown event e.KeyCode does not trigger Keys.Enter

Kevin
  • 1
  • Sorry I've misread the answer above. I haven't try WPF. – Kevin Mar 19 '22 at 19:21
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 20 '22 at 07:52