KeyDown事件未触发,KeyPreview设置为true

我正在构建一个小型Forms应用程序,我刚刚启动它。 但我有这个问题:如果我把一个控件放到窗体上,KeyDown事件不会触发。 我知道KeyPreview属性,并将其设置为true。 但这没有帮助… :(我也试图把焦点放在主要forms上,也没有成功。

有什么想法吗?

编辑:

public partial class Form1 : Form { public Form1() { InitializeComponent(); KeyDown += new KeyEventHandler(Form1_KeyDown); this.KeyPreview = true; } void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Left: MessageBox.Show("Left"); break; case Keys.Right: MessageBox.Show("Right"); break; } } } 

我已经评论了我的解决方案,但我也将其作为答案发布,因此可以很容易地找到它。

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.Left: // left arrow key pressed return true; case Keys.Right: // right arrow key pressed return true; case Keys.Up: // up arrow key pressed return true; case Keys.Down: // down arrow key pressed return true; } return base.ProcessCmdKey(ref msg, keyData); } 

如果您使用WPF,则可以轻松捕获所需的事件,因为WPF使用路由事件系统来分派事件。 在winforms中,我推荐以下两种方式之一:

1.使用Application.AddMessageFilter Method

定义消息filter类:

 public class KeyMessageFilter : IMessageFilter { private enum KeyMessages { WM_KEYFIRST = 0x100, WM_KEYDOWN = 0x100, WM_KEYUP = 0x101, WM_CHAR = 0x102, WM_SYSKEYDOWN = 0x0104, WM_SYSKEYUP = 0x0105, WM_SYSCHAR = 0x0106, } [DllImport("user32.dll")] private static extern IntPtr GetParent(IntPtr hwnd); // We check the events agains this control to only handle // key event that happend inside this control. Control _control; public KeyMessageFilter() { } public KeyMessageFilter(Control c) { _control = c; } public bool PreFilterMessage(ref Message m) { if (m.Msg == (int)KeyMessages.WM_KEYDOWN) { if (_control != null) { IntPtr hwnd = m.HWnd; IntPtr handle = _control.Handle; while (hwnd != IntPtr.Zero && handle != hwnd) { hwnd = GetParent(hwnd); } if (hwnd == IntPtr.Zero) // Didn't found the window. We are not interested in the event. return false; } Keys key = (Keys)m.WParam; switch (key) { case Keys.Left: MessageBox.Show("Left"); return true; case Keys.Right: MessageBox.Show("Right"); return true; } } return false; } } 

因此,您有一个类,Windows窗体中的每个消息都通过它。 你可以随意为活动做任何事情。 如果PreFilterMessage方法返回true,则表示不应将事件分派给它的respcetive控件。

(请注意, Keys枚举中的值几乎是虚拟键代码的必要条件)

在此之前,您必须将其添加到应用程序的消息filter:

 public partial class Form1 : Form { // We need an instance of the filter class KeyMessageFilter filter; public Form1() { InitializeComponent(); filter = new KeyMessageFilter(panel1); // add the filter Application.AddMessageFilter(filter); } protected override void OnFormClosed(FormClosedEventArgs e) { base.OnFormClosed(e); // remove the filter Application.RemoveMessageFilter(filter); } } 

filter仅在Form1的生命周期内处于活动状态。

注意:这将捕获任何forms的事件! 如果您希望它仅适用于一个表单,请将表单传递给filter类,并将其Handle属性与PreFilterMessage m.HWnd进行比较

2.使用Windows Hooks :

这是一种更先进,更复杂(和低水平)的方法。 它需要更多代码。 我写了一个HookManager类,使得这个过程非常简单。 我要把这个类发布到github并写一篇关于它的文章。

您观察到的行为的原因是特殊键,如TAB,上/下/左/右箭头,PAGE UP / DOWN,HOME,END等,通常被通用控件视为“输入键”。

例如,ARROW键被TabControl视为“输入键”,因为这些键允许您更改选定的TabPage。 多行TextBox存在类似的行为,其中ARROWS键允许您移动文本光标。

我认为由于同样的原因,您所拥有的Rumba Mainframe控件也会做同样的事情。 您可以尝试覆盖它并更改IsInputKey方法的实现或处理PreviewKeyDown事件并将IsInputKey属性设置为true。

有关更多详细信息,请参阅Control.IsInputKey方法和Control.PreviewKeyDown事件的文档

箭头键是一种由控件自动处理的特殊键。 因此,如果您想让他们举起KeyDown事件,您可以:

1)在表单的每个控件中重写isInputKey方法

要么

2)处理PreviewKeyDown事件并将IsInputKey属性设置为true

更多信息可以在这里找到。

我知道WonderCsabo已经解决了他的问题,但是其他人给了它一个赏金,因为它有同样的问题并且没有选择答案。 WonderCsabo也请发布您的解决方案作为答案。