窗体未响应的KeyDown事件 [英] Forms not responding to KeyDown events

查看:252
本文介绍了窗体未响应的KeyDown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的Windows窗体项目时,我决定尝试使用键盘快捷键。经过一番阅读,我算了一下,只写一个事件处理程序,并将其绑定到窗体的KeyDown事件:

I've been working for a while on my Windows Forms project, and I decided to experiment with keyboard shortcuts. After a bit of reading, I figured I had to just write an event handler and bind it to the form's KeyDown event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.Alt && e.KeyCode == Keys.O)
    {
        MessageBox.Show("Ctrl+Alt+O: magic!");
    }
}

我这样做,打开Visual Studio设计,在我的形式产生KeyDown事件,然后双击 Form1_KeyDown 的属性面板的好醇'的方式事件处理程序。但在测试我的应用程序,形式不给<大骨节病>控制 + <大骨节病>替代 + <大骨节病> 0 键盘快捷键在所有响应。 Visual Studio设计确实产生code到虽然事件处理程序的形式绑定:

I did that the good ol' way of opening the Properties panel of the Visual Studio designer, then double-clicking on the KeyDown event of my form to generate the Form1_KeyDown event handler. But on testing my application, the form doesn't respond at all to the Ctrl+Alt+O keyboard shortcut. The Visual Studio designer did generate the code to bind the event handler to the form though:

private void InitializeComponent()
{
    // ...

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

    // ...
}

所以,我想加入 Console.WriteLine()打电话给处理程序,以检查它被调用,但没有任何运气

So I tried adding a Console.WriteLine() call to the handler to check that it was being called at all, but no luck on that either.

另外,我试图设置在事件绑定调用(只是如上所示)断点,发现程序到达该断点就好了。但我的方法定义本身中设置的任何断点从来没有达到过。

Also, I tried to set a breakpoint on the event binding call (shown just above) and found that the program reaches that breakpoint just fine. But any breakpoints I set within the method definition itself are never reached.

要确保我在做正确的前几个步骤,我试着重复他们:

To make sure I was doing the first few steps correctly, I tried repeating them with:

  • 在同一个解决方案的新形式。
    同一个问题:表单没有响应,当我preSS我的<大骨节病>控制 + <大骨节病>替代 + <大骨节病> 0 键盘快捷键和调试器甚至没有步入事件处理程序。再次尝试这样做,它的工作原理。

  • A new form in the same solution.
    Same issue: the form doesn't respond when I press my Ctrl+Alt+O keyboard shortcut and the debugger isn't even stepping into the event handler. Tried this again and it works.

一个全新的WinForms的解决方案。
它完美的作品:消息对话框出现( Console.WriteLine()通话也适用)

A brand new WinForms solution.
It works perfectly: the message dialog appears (the Console.WriteLine() call also works).

所以,我很丢在这里。什么是preventing一切形式在这一个项目接收KEYDOWN事件?

So I'm quite lost here. What's preventing all the forms in this one project from receiving KeyDown events?

推荐答案

请问您的形式主要有preVIEW属性设置为true?

Does your form have KeyPreview property set to true?

Form.Key preVIEW地产

获取或设置一个值,指示该窗体是否将获得关键   在事件发生前的事件被传递给具有焦点的控制。

Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.key$p$pview.aspx">http://msdn.microsoft.com/en-us/library/system.windows.forms.form.key$p$pview.aspx

这篇关于窗体未响应的KeyDown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆