简体   繁体   中英

How do I tell when the enter key is pressed in a TextBox?

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?

Give this a shot...

private void input_KeyDown(object sender, KeyEventArgs e) 
{                        
    if(e.KeyData == Keys.Enter)   
    {  
        MessageBox.Show("Pressed enter.");  
    }             
}

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.");
    }
}

You can actually just say

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

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);
    }
}

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)
    }
}

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 (eg CTRL, Shift + A) which you don't need here.

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 (eg CTRL, Shift + A) which you don't need here."

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM