在keydown事件中传递了错误的键值 [英] False keyvalues are passed on keydown event
问题描述
我有一个MDI父母表格.当用户按下Enter键时,我希望该应用程序关闭.
I have a MDI parent form. When user presses Enter I want the Application to shut down.
我按如下检查keydown事件:
I check the keydown event as follows:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (int)Keys.Enter)
{
Application.Exit();
}
}
现在,当我在窗体上没有任何可单击的控件(按钮,文本框等)时,它可以正常工作. e.KeyValue
具有Enter键(13)的(int)
值.但是,如果我在MDI表单上放置了一些按钮或文本框,则e.KeyValue
会带来Alt
密钥的键值,即18..为什么会这样?
Now it works fine, when I don't have any clickable controls on form (Button, TextBox etc). The e.KeyValue
has the (int)
value of Enter Key (13). But if I put some buttons or textboxes on to MDI Form, e.KeyValue
brings the keyvalue of Alt
Key i.e. 18. Why so ??
现在,如果我按Alt+Enter
键,该表格将关闭;但不仅是在Enter键上
So now if I press Alt+Enter
, the form closes; but not only on Enter Key
预先感谢
推荐答案
您需要尽快处理KeyPress.以下代码将为您工作:
You need to process the KeyPress a little sooner. The following code will work for you:
protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)
{
int _ENTER = 13;
int _KEYUP = 257;
if (m.Msg == _ENTER && (int)m.WParam == _KEYUP)
{
Application.Exit();
}
return base.ProcessKeyPreview(ref m);
}
这篇关于在keydown事件中传递了错误的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!