C# 处理功能按键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1707040/
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
handling function key press
提问by Tj.
I have a C# form with 5 buttons. The users enters the information and depending on the press of a function key, a specific action is performed. F9-Execute Order, F6-Save, F3-LookUp.
我有一个带有 5 个按钮的 C# 表单。用户输入信息,并根据功能键的按下,执行特定操作。F9-执行订单,F6-F3保存,-查找。
I have added the foolowing code:
我添加了愚蠢的代码:
OnForm_Load
OnForm_Load
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyEvent);
and
和
private void KeyEvent(object sender, KeyEventArgs e) //Keyup Event
{
if (e.KeyCode == Keys.F9)
{
MessageBox.Show("Function F9");
}
if (e.KeyCode == Keys.F6)
{
MessageBox.Show("Function F6");
}
else
MessageBox.Show("No Function");
}
but nothing happens
但什么也没发生
Thanks
谢谢
采纳答案by yu_sha
You need to set
你需要设置
KeyPreview=True
for your form. Otherwise key press is swallowed by the control that has focus.
为您的表格。否则按键被具有焦点的控件吞下。