繁体   English   中英

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

[英]KeyDown event is not firing, KeyPreview set to true

我正在构建一个小型Forms应用程序,我刚刚启动它。 但我有这个问题:如果我将一个控件放到窗体上,KeyDown事件不会触发。 我知道KeyPreview属性,并将其设置为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;
        }
    }
}

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

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,则表示不应将事件分派给它的respcetive控件。

(请注意, 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属性与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也请发布您的解决方案作为答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM