4

背景

我有几个相当复杂的 C# gui 应用程序,它们的控件嵌套在控件内部的控件中,这需要全局处理热键(即,需要有一个顶级处理程序,无论焦点在哪里都可以捕获按键是)。

我的要求在这里可能有点不寻常,因为一些热键是常用的按键,如字母,甚至是空格键。当按下空格键之类的键时,显然会有某些控件(例如文本框)已经处理它。在焦点控件处理键的情况下,我想避免调用全局热键处理程序。

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

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

问题

有没有办法在 Control 上设置 KeyDown 处理程序,使其仅在且仅在以下情况下接收事件:

  • 控制或其后代之一被聚焦,并且
  • 任何后代控件都没有焦点,或者焦点控件不处理 KeyDown 事件

我对此做了尽可能多的研究。我知道PreFilterMessageForm.KeyPreview,但据我所知,当它应该由一些更具体的控件处理时,它们没有一种干净的方式来忽略键,因为它们之前得到了事件重点控制得到它。我真正想要的几乎是相反的——在焦点控件决定是否处理它之前,表单不会获得 KeyDown。

4

1 回答 1

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 作为修饰符和任何字母作为键。

享受。

于 2013-04-04T21:57:19.183 回答