使用ProcessCmdKey按住箭头键向下事件

时间:2014-06-19 20:43:48

标签: c# events keyboard key

只要按住箭头键,我就需要执行某种方法。现在,对于Visual Studio 2012中的常规箭头键按下,使用OnKeyDown不起作用,所以我需要使用ProcessCmdKey,它就像一个魅力。有没有办法使用ProcessCmdKey来检测箭头何时被释放/被按下?


我已经同时采取了Markus'和汉斯'建议,并结合这两个想法。我使用了ProcessKeyPreview方法,但我仍然遇到了一些问题。当我按住任何箭头键时,此方法不会检测到WM_KEYDOWN已发生...但是一旦我从键中释放我的手指,它实际上会注意到WM_KEYUP已经发生。

有趣的是,当我按下任何其他键(即字母' S')时,它会正确识别何时按下并释放它。我在下面发布了我的代码片段:

const int WM_KEYUP = 0x0101;
const int WM_KEYDOWN = 0x0100;

    protected override bool ProcessKeyPreview(ref Message m)
            {
                int msgVal = m.WParam.ToInt32();            
                if (m.Msg == WM_KEYDOWN)
                {
                    switch ((Keys)msgVal) {
                        case Keys.Down:
                            Console.WriteLine("down pressed"); //not detected
                            break;
                        case Keys.S:
                            Console.WriteLine("S pressed!"); //detected
                            break;
                    }
                }
                if (m.Msg == WM_KEYUP)
                {
                    switch ((Keys)msgVal)
                    {
                        case Keys.Down:
                            Console.WriteLine("down released"); //detected
                            break;
                        case Keys.S:
                            Console.WriteLine("s released!"); //detected
                            break;
                    }
                }
                return base.ProcessKeyPreview(ref m);
            }        

2 个答案:

答案 0 :(得分:4)

您无法通过ProcessCmdKey()看到KeyUp事件,只能处理KeyDown事件。如果表单包含控件,则需要在更低的级别处理此问题。诀窍是在Winforms通过正常的密钥处理和WndProc链发送它之前拦截消息。这需要实现IMessageFilter接口。像这样:

public partial class Form1 : Form, IMessageFilter {  // NOTE: added IMessageFilter
    public Form1() {
        InitializeComponent();
        Application.AddMessageFilter(this);
    }
    protected override void OnFormClosed(FormClosedEventArgs e) {
        Application.RemoveMessageFilter(this);
        base.OnFormClosed(e);
    }

    bool IMessageFilter.PreFilterMessage(ref Message m) {
        // Trap WM_KEYUP/DOWN for Keys.Down key
        if ((m.Msg == 0x100 || m.Msg == 0x101) && (Keys)m.WParam.ToInt32() == Keys.Down) {
            bool repeat = (m.LParam.ToInt32() & (1 << 30)) != 0;
            bool down = m.Msg == 0x100;
            // But only for this form
            Form form = null;
            var ctl = Control.FromHandle(m.HWnd);
            if (ctl != null) form = ctl.FindForm();
            if (form == this) {
                OnCursorDown(down, repeat & down);
                return true;
            }
        }
        return false;
    }
    private void OnCursorDown(bool pressed, bool repeat) {
        // etc..
    }
}

答案 1 :(得分:2)

您可以重载ProcessKeyPreview,它可以让您看到WM_KEYDOWN消息以及WM_KEYUP消息。请务必为表单启用KeyPreview。

WM_KEYDOWN:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646280(v=vs.85).aspx

WM_KEYUP:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646281(v=vs.85).aspx

示例:

public partial class TestForm : Form
{
    public TestForm()
    {
        InitializeComponent();
        this.KeyPreview = true;
    }

    const int WM_KEYUP = 0x0101;
    const int WM_KEYDOWN = 0x0100;

    protected override bool ProcessKeyPreview(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_KEYDOWN:
                Console.WriteLine(m.LParam);
                break;

            case WM_KEYUP:
                Console.WriteLine(m.LParam);
                break;
        }
        return base.ProcessKeyPreview(ref m);
    }
}