C# 如何判断何时在 TextBox 中按下 Enter 键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11806166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I tell when the enter key is pressed in a TextBox?
提问by Jon
Basically, I want to be able to trigger an event when the ENTERkey is pressed. I tried this already:
基本上,我希望能够在ENTER按下键时触发事件。我已经试过了:
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?
但是 MessageBox 永远不会出现。我怎样才能做到这一点?
采纳答案by Chris Gessler
Give this a shot...
试一试这个...
private void input_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Enter)
{
MessageBox.Show("Pressed enter.");
}
}
回答by Willy David Jr
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 KeyCodevs KeyDatais that KeyCodecan detect modifiers combination with KeyCode(e.g. CTRL, Shift + A) which you don't need here.
与KeyCodevs的唯一区别KeyData是KeyCode可以检测KeyCode您在此处不需要的修饰符组合(例如 CTRL、Shift + A)。
回答by Mantas Da?kevi?ius
To add to @Willy David Jr answer: you also can use actual Key codes.
添加到@Willy David Jr 的答案:您也可以使用实际的密钥代码。
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Pressed enter.");
}
}
回答by Makusium
You can actually just say
你实际上可以说
private void input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Pressed enter.");
}
}
回答by OldDog
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 anyof 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:
您可以使用 Keypress 事件。如果您只是在寻找“Enter”按键,那么您可能不关心修饰键(例如 Shift 和/或 Ctrl),这就是为什么大多数人会使用 KeyDown 而不是 Keypress。第二个好处是回答了在实施任何其他答案后几乎总是被问到的问题:“当我使用引用的代码时,为什么按“Enter”会导致蜂鸣声?” 这是因为需要处理 Keypress 事件。通过使用 Keypress,您可以在一个地方解决这两个问题:
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)
}
}
回答by AecorSoft
If your Form has AcceptButton defined, you won't be able to use KeyDown to capture the Enter.
如果您的 Form 定义了 AcceptButton,您将无法使用 KeyDown 来捕获 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);
}
}

