你正在寻找一个键盘钩子。
创建一个名为
KeyboardHook
的类,它应该长这样:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public sealed class KeyboardHook : IDisposable
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private class Window : NativeWindow, IDisposable
{
private static int WM_HOTKEY = 0X0312;
public Window()
{
this.CreateHandle(new CreateParams());
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY)
{
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
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()
{
_window.KeyPressed += delegate(object sender, KeyPressedEventArgs args)
{
if (KeyPressed != null)
KeyPressed(this, args);
};
}
public void RegisterHotKey(ModifierKeys modifier, Keys key)
{
_currentId = _currentId + 1;
if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
throw new InvalidOperationException("Couldn’t register the hot key.");
}
public event EventHandler<KeyPressedEventArgs> KeyPressed;
#region IDisposable Members
public void Dispose()
{
for (int i = _currentId; i > 0; i--)
{
UnregisterHotKey(_window.Handle, i);
}
_window.Dispose();
}
#endregion
}
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; }
}
}
[Flags]
public enum ModifierKeys : uint
{
Alt = 1,
Control = 2,
Shift = 4,
Win = 8
}
在你的表格中应该长这样:
KeyboardHook hook = new KeyboardHook();
public Form1()
{
InitializeComponent();
hook.KeyPressed +=
new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
hook.RegisterHotKey(global::ModifierKeys.Control, Keys.A);
}
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
if (e.Modifier == global::ModifierKeys.Control && e.Key == Keys.A)
{
}
if (textBox1 == ActiveControl)
{
}
}
某些键的组合不起作用,并且会在RegisterHotKey(ModifierKeys modifier, Keys key)
方法中抛出异常。
似乎使用Ctrl作为修饰符和任何字母作为键可以正常工作。
享受吧。
OnEvent
变体方法。 - Parimal Raj