在 C# 中输入按键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12318164/
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
Enter key press in C#
提问by Dave Doga Oz
I tried this code:
我试过这个代码:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
MessageBox.Show(" Enter pressed ");
}
}
and this:
和这个:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Enter))
{
MessageBox.Show(" Enter pressed ");
}
}
and this:
和这个:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Keys.Enter)
{
MessageBox.Show(" Enter pressed ");
}
}
but they're not working...
但他们不工作...
When I write something and press Enter, it doesn't work. It only highlights my text.
当我写东西并按 时Enter,它不起作用。它只突出显示我的文字。
采纳答案by Sandeep Pathak
Try this code,might work (Assuming windows form):
试试这个代码,可能会工作(假设窗口形式):
private void CheckEnter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Enter key pressed
}
}
Register the event like this :
像这样注册事件:
this.textBox1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(CheckEnter);
回答by ragatskynet
Try this:
尝试这个:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.Key.ToString())
{
case "Return":
MessageBox.Show(" Enter pressed ");
break;
}
}
回答by ALI VOJDANIANARDAKANI
You must try this in keydown event
您必须在 keydown 事件中尝试此操作
here is the code for that :
这是代码:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter pressed");
}
}
Update :
更新 :
Also you can do this with keypress event.
你也可以用按键事件来做到这一点。
Try This :
尝试这个 :
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Return))
{
MessageBox.Show("Key pressed");
}
}
回答by Mike Taylor
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
MessageBox.Show("Enter Key Pressed");
}
}
This allows you to choose the specific Key you want, without finding the char value of the key.
这允许您选择所需的特定键,而无需查找键的字符值。
回答by Lasse
private void textBoxKontant_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
MessageBox.Show("Enter pressed");
}
}
Screenshot:
截屏:


回答by Peter
private void Input_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
MessageBox.Show("Enter pressed");
}
}
This worked for me.
这对我有用。
回答by Abdul Rehman
Also you can do this with keypress event.
你也可以用按键事件来做到这一点。
private void textBox1_EnterKeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some code what you wanna do
}
}
回答by kanak
You could use this code:
您可以使用此代码:
abc_KeyDown(abc, new KeyEventArgs(Keys.Enter));
回答by Jozek
KeyChar is looking for an integer value, so it needs to be converted as follows:
KeyChar 正在寻找一个整数值,因此需要进行如下转换:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToInt16(Keys.Enter))
{
MessageBox.Show("Testing KeyPress");
}
}
回答by agileDev
private void TextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter pressed");
}
}
Worked for me.
为我工作。

