-->

KeyDown事件不点火,的KeyPreview设置为true(KeyDown event is n

2019-07-29 02:55发布

我建立一个小窗体应用程序,我刚开始它。 但我有这样的问题:如果我把一个控件的形式,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;
        }
    }
}

Answer 1:

我已经评论我的解决方案,但我还张贴作为一个答案,因此它可以很容易找到。

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


Answer 2:

如果你是在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枚举几乎idential到虚拟键码 )

在此之前的作品,你必须将它添加到应用程序的消息过滤器:

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.HWndPreFilterMessage

2.使用windows钩子 :

这是一种更先进,更复杂(低电平)的方法。 它需要更多的代码。 我已经写了一个HookManager类,使这一过程非常简单。 我要去发布类Github上,写的一篇文章。



Answer 3:

你观察到的行为的原因是,像TAB特殊键,上/下/左/右方向,PAGE UP / DOWN,HOME,END等往往是由共同的控制被认为是“输入键”。

例如,箭头键被认为是“输入键”被作为TabControl的这些键允许您更改选定的TabPage。 类似的行为也同时出现在多行TextBox,其中的箭头键可以移动文本光标。

我假设你有伦巴主机控制做同样的事情出于同样的原因。 你可以尝试覆盖它并改变IsInputKey方法的实现或处理PreviewKeyDown事件和IsInputKey属性设置为true。

请参阅的文件Control.IsInputKey方法和Control.PreviewKeyDown事件的进一步详情



Answer 4:

箭头键是一种由控件自动处理专用密钥。 所以,如果你想使他们提高KeyDown事件你可以:

1)在窗体的每一个控制重写IsInputKey方法

要么

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

更多信息可以发现在这里 。

我知道WonderCsabo已经解决了他的问题,而是别人把赏金就可以了,因为有同样的问题,并没有选择答案。 WonderCsabo请发表您的解决方案的回答为好。



文章来源: KeyDown event is not firing, KeyPreview set to true