如何将未处理的KeyDown事件传播到父控件
本文关键字:传播 控件 事件 KeyDown 未处理 | 更新日期: 2023-09-27 18:07:31
背景:
我有几个相当复杂的C#gui应用程序,控件嵌套在控件内部的控件中,这需要全局处理热键(即,需要有一个顶级处理程序,无论焦点在哪里,都可以捕捉按键(。
我的要求可能有点与众不同,因为有些热键通常是按字母键,甚至空格键。当按下空格键时,显然会有一些控件,比如文本框,已经可以处理它了。在焦点控件处理键的情况下,我希望避免调用全局热键处理程序。
我目前的解决方案是使用PreFilterMessage全局处理热键,然后在PreFilterMessage调用内部,如果已知聚焦控件处理该键,我有绕过全局热键的代码。(但是,由于IsInputKey是受保护的,我无法询问控件是否处理该键,所以我对热键应该绕过哪些控件有自己的混乱逻辑(。
我对PreFilterMessage解决方案不太满意,他们似乎应该是一种更优雅的方式。从概念上讲,我想要的行为非常简单。如果焦点控件处理KeyDown,那么我不希望其他任何东西来处理它。否则,Parent控件应该尝试处理它,如果该控件不处理键,它应该尝试它的父控件,直到它一直到达Form的KeyDown处理程序。
问题:
是否有一种方法可以在控件上设置KeyDown处理程序,使其仅在以下情况下接收事件:
- 控件或其子控件之一是焦点,并且
- 子控件都没有焦点,或者焦点控件不处理KeyDown事件
我已经对此做了尽可能多的研究。我知道PreFilterMessage和Form.KeyPreview,但据我所知,当密钥应该由一些更具体的控件处理时,它们并没有一种干净的方法来忽略密钥,因为他们在焦点控件得到事件之前就得到了它。我真正想要的几乎是相反的——在焦点控件决定是否处理之前,表单不会得到KeyDown。
您正在寻找一个键盘挂钩。
创建一个名为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作为修饰符和任何字母作为键。
享受吧。