如何将未处理的KeyDown事件传播到父控件?

时间:2013-04-04 19:54:18

标签: c# winforms

背景

我有几个相当复杂的C#gui应用程序,控件嵌套在控件内的控件内部,这需要全局处理热键(即,需要一个能够捕获按键的顶级处理程序,而不管重点是)。

我的要求可能有点与众不同,因为有些热键通常是按键,如字母,甚至是空格键。当按下像空格键这样的键时,显然会有某些控件,比如已经处理它的文本框。在聚焦控件处理密钥的情况下,我想避免调用全局热键处理程序。

我目前的解决方案是使用PreFilterMessage来全局处理热键,然后在PreFilterMessage调用的内部,我有代码绕过全局热键,如果已知聚焦控件处理该键。 (但是,由于IsInputKey受到保护,我无法询问控件是否处理密钥,所以我只是有了自己的杂乱逻辑,关于哪些控件应该绕过内部的热键。)

我对PreFilterMessage解决方案不太满意,看起来它们应该是一种更优雅的方式。从概念上讲,我想要的行为非常简单。如果聚焦控件处理KeyDown,那么我不想要任何其他东西来处理它。否则,Parent控件应该尝试处理它,如果该控件不处理该键,它应该尝试其中的父,直到它一直到Form的KeyDown处理程序。

问题

是否有办法在Control上设置KeyDown处理程序,以便它仅在以下情况下接收事件:

  • Control或其中一个后代是专注的,
  • 任何后代控件都没有焦点,或者焦点控件没有处理KeyDown事件

我已经做了尽可能多的研究。我知道PreFilterMessageForm.KeyPreview,但据我所知,当它应该由一些更具体的控件处理时,那些没有一种干净的忽略键的方法,因为他们在专注控制获得之前得到了这个事件。我真正想要的几乎恰恰相反 - 对于直到焦点控制决定是否处理它之后才得到KeyDown的形式。

1 个答案:

答案 0 :(得分:1)

您正在寻找键盘钩。

创建一个名为KeyboardHook的类,如下所示:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public sealed class KeyboardHook : IDisposable
{
    // Registers a hot key with Windows.
    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
    // Unregisters the hot key with Windows.
    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
    /// <summary>
    /// Represents the window that is used internally to get the messages.
    /// </summary>
    private class Window : NativeWindow, IDisposable
    {
        private static int WM_HOTKEY = 0X0312;

        public Window()
        {
            // create the handle for the window.
            this.CreateHandle(new CreateParams());
        }

        /// <summary>
        /// Overridden to get the notifications.
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            // check if we got a hot key pressed.
            if (m.Msg == WM_HOTKEY)
            {
                // get the keys.
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
                ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);

                // invoke the event to notify the parent.
                if (KeyPressed != null)
                    KeyPressed(this, new KeyPressedEventArgs(modifier, key));
            }
        }

        public event EventHandler<KeyPressedEventArgs> KeyPressed;

        #region IDisposable Members

        public void Dispose()
        {
            this.DestroyHandle();
        }

        #endregion
    }

    private Window _window = new Window();
    private int _currentId;

    public KeyboardHook()
    {
        // register the event of the inner native window.
        _window.KeyPressed += delegate(object sender, KeyPressedEventArgs args)
        {
            if (KeyPressed != null)
                KeyPressed(this, args);
        };
    }

    /// <summary>
    /// Registers a hot key in the system.
    /// </summary>
    /// <param name="modifier">The modifiers that are associated with the hot key.</param>
    /// <param name="key">The key itself that is associated with the hot key.</param>
    public void RegisterHotKey(ModifierKeys modifier, Keys key)
    {
        // increment the counter.
        _currentId = _currentId + 1;

        // register the hot key.
        if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
            throw new InvalidOperationException("Couldn’t register the hot key.");
    }

    /// <summary>
    /// A hot key has been pressed.
    /// </summary>
    public event EventHandler<KeyPressedEventArgs> KeyPressed;

    #region IDisposable Members

    public void Dispose()
    {
        // unregister all the registered hot keys.
        for (int i = _currentId; i > 0; i--)
        {
            UnregisterHotKey(_window.Handle, i);
        }

        // dispose the inner native window.
        _window.Dispose();
    }

    #endregion
}

/// <summary>
/// Event Args for the event that is fired after the hot key has been pressed.
/// </summary>
public class KeyPressedEventArgs : EventArgs
{
    private ModifierKeys _modifier;
    private Keys _key;

    internal KeyPressedEventArgs(ModifierKeys modifier, Keys key)
    {
        _modifier = modifier;
        _key = key;
    }

    public ModifierKeys Modifier
    {
        get { return _modifier; }
    }

    public Keys Key
    {
        get { return _key; }
    }
}

/// <summary>
/// The enumeration of possible modifiers.
/// </summary>
[Flags]
public enum ModifierKeys : uint
{
    Alt = 1,
    Control = 2,
    Shift = 4,
    Win = 8
}

在你的表格中应该看起来像这样:

    KeyboardHook hook = new KeyboardHook();

    public Form1()
    {
        InitializeComponent();

        // register the event that is fired after the key press.
        hook.KeyPressed +=
        new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
        // register the control + alt + F12 combination as hot key.
        hook.RegisterHotKey(global::ModifierKeys.Control, Keys.A);
    }

    void hook_KeyPressed(object sender, KeyPressedEventArgs e)
    {
        if (e.Modifier == global::ModifierKeys.Control && e.Key == Keys.A)
        {
             //Some code here when hotkey is pressed.
        }

        if (textBox1 == ActiveControl)
        {
            // if textBox1 is in focus
        }
    }

某些键组合不起作用,会在RegisterHotKey(ModifierKeys modifier, Keys key)方法上抛出异常。

似乎可以很好地使用Ctrl作为修饰符,任何字母作为键。

享受。