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

本文关键字:KeyPreview 设置 true 事件 KeyDown | 更新日期: 2023-09-27 18:31:57

我正在构建一个小的Forms应用程序,我刚刚开始它。但我有这个问题:如果我将控件放在窗体上,则 KeyDown 事件不会触发。我知道密钥预览属性,并将其设置为 true。但这并没有帮助... :(我还尝试设置专注于主要形式,也没有成功。

有什么想法吗?

编辑:

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;
        }
    }
}

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

我已经

评论了我的解决方案,但我也将其作为答案发布,因此可以轻松找到。

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

定义邮件过滤器类:

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,则表示不应将事件调度到其临时控件。

(请注意,Keys枚举中的值几乎与虚拟键代码相同)

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

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);
    }
}

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

注意:这将捕获任何形式的事件!如果希望它只适用于一个窗体,请将该窗体传递给筛选器类,并将其 Handle 属性与 m.HWnd 进行比较 PreFilterMessage

2. 使用窗口挂钩:

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

您观察到的行为的原因是特殊键,如 TAB、UP/DOWN/LEFT/RIGHT ARROW、PAGE UP/DOWN、HOME、END 等,通常被常用控件视为"输入键"。

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

我假设您拥有的 Rumba 大型机控件出于同样的原因执行相同的操作。您可以尝试重写它并更改 IsInputKey 方法的实现或处理 PreviewKeyDown 事件并将 IsInputKey 属性设置为 true。

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

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

1) 在窗体的每个控件中重写 isInputKey 方法。

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

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

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