上、下、左、右方向键不触发 KeyDown 事件
我正在构建一个应用程序,其中所有按键输入都必须由窗口本身处理。
我将 tabstop 设置为 false,因为每个控制女巫都可以抓住除面板之外的焦点(但我不知道它是否有效)。
我将 KeyPreview 设置为 true,并在此表单上处理 KeyDown 事件。
我的问题是,有时箭头键不再响应:
当我只按箭头键时,不会触发 keydown 事件。
如果我按下带有控制修饰符的箭头键,则会触发 keydown 事件。
你知道为什么我的箭头键突然停止触发事件吗?
I am building an application where all the key input must be handled by the windows itself.
I set tabstop to false for each control witch could grab the focus except a panel (but I don't know if it has effect).
I set KeyPreview to true and I am handling the KeyDown event on this form.
My problem is that sometimes the arrow key aren't responsive anymore:
The keydown event is not fired when I pressed only an arrow key.
The keydown event is fired if I press an arrow key with the control modifier.
Have you an idea why my arrow key suddenly stop firing event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我遇到了完全相同的问题。我考虑了@Snarfblam 提供的答案;但是,如果您阅读 MSDN 上的文档,就会发现 ProcessCMDKey 方法旨在覆盖应用程序中菜单项的按键事件。
我最近偶然发现了微软的这篇文章,它看起来很有前途: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx。根据微软的说法,最好的办法是在检测到箭头键后在
PreviewKeyDown
事件中设置e.IsInputKey=true;
。这样做将触发KeyDown
事件。这对我来说非常有效,并且比覆盖 ProcessCMDKey 更不那么黑客化。
I was having the exact same problem. I considered the answer @Snarfblam provided; however, if you read the documentation on MSDN, the ProcessCMDKey method is meant to override key events for menu items in an application.
I recently stumbled across this article from microsoft, which looks quite promising: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx. According to microsoft, the best thing to do is set
e.IsInputKey=true;
in thePreviewKeyDown
event after detecting the arrow keys. Doing so will fire theKeyDown
event.This worked quite well for me and was less hack-ish than overriding the ProcessCMDKey.
我正在使用 PreviewKeyDown
I'm using PreviewKeyDown
请参阅 Rodolfo Neuber 的回复以获得最佳答案
(我的原始答案):
从控件类派生,您可以覆盖 ProcessCmdKey 方法。 Microsoft 选择从 KeyDown 事件中省略这些键,因为它们会影响多个控件并移动焦点,但这使得应用程序很难以任何其他方式对这些键做出反应。
See Rodolfo Neuber's reply for the best answer
(My original answer):
Derive from a control class and you can override the ProcessCmdKey method. Microsoft chose to omit these keys from KeyDown events because they affect multiple controls and move the focus, but this makes it very difficult to make an app react to these keys in any other way.
不幸的是,由于 KeyDown 事件的限制,使用箭头键很难完成此操作。但是,有几种方法可以解决这个问题:
我建议尝试使用该类。这样做非常简单:
如果您将其与 KeyDown 事件结合使用,我认为您可以可靠地实现您的目标。
Unfortunately, it is quite difficult to accomplish this with the arrow keys, due to restrictions in KeyDown events. However, there are a few ways to get around this:
I recommend trying to use that class. It's quite simple to do so:
If you use this in combination with the KeyDown event, I think you can reliably accomplish your goal.
从 WinForms 调用 WPF 窗口时,我遇到了类似的问题。
但是,将窗口显示为对话框,它有效
希望这会有所帮助。
I had a similar issue when calling the WPF window out of WinForms.
However, showing window as a dialog, it worked
Hope this helps.
为了捕获 Forms 控件中的击键,必须根据所需控件的类派生一个新类,并重写 ProcessCmdKey()。
示例:
完整源代码...C# 中的箭头键
Vayne
In order to capture keystrokes in a Forms control, you must derive a new class that is based on the class of the control that you want, and you override the ProcessCmdKey().
Example :
Full source...Arrow keys in C#
Vayne
我认为最好的方法就是像 MSDN 上所说的那样处理它
http://msdn.microsoft.com/en -us/library/system.windows.forms.control.previewkeydown.aspx
但是按照您真正需要的方式处理它。我的方法(在下面的示例中)是捕获每个 KeyDown ;-)
The best way to do, I think, is to handle it like the MSDN said on
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
But handle it, how you really need it. My way (in the example below) is to catch every KeyDown ;-)
我遇到了同样的问题,并且已经在使用所选答案中的代码。这个链接就是我的答案;也许对其他人也是如此。
如何在 C# 中使用箭头禁用 WinForm 上的导航?< /a>
i had the same problem and was already using the code in the selected answer. this link was the answer for me; maybe for others also.
How to disable navigation on WinForm with arrows in C#?