如果只是为了快捷键而使用全局键盘钩子,那么注册热键是最好的选择(这是由微软完成的):
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx
下载示例winform应用程序并亲自查看:
https://code.msdn.microsoft.com/CppRegisterHotkey-7bd897a8 C++
https://code.msdn.microsoft.com/CSRegisterHotkey-e3f5061e C#
https://code.msdn.microsoft.com/VBRegisterHotkey-50af3179 VB.Net
Winform应用程序:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CSRegisterHotkey
{
public partial class MainForm : Form
{
HotKeyRegister hotKeyToRegister = null;
Keys registerKey = Keys.None;
KeyModifiers registerModifiers = KeyModifiers.None;
public MainForm()
{
InitializeComponent();
}
private void tbHotKey_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
if (e.Modifiers != Keys.None)
{
Keys key = Keys.None;
KeyModifiers modifiers = HotKeyRegister.GetModifiers(e.KeyData, out key);
if (key != Keys.None)
{
this.registerKey = key;
this.registerModifiers = modifiers;
tbHotKey.Text = string.Format("{0}+{1}",
this.registerModifiers, this.registerKey);
btnRegister.Enabled = true;
}
}
}
private void btnRegister_Click(object sender, EventArgs e)
{
try
{
hotKeyToRegister = new HotKeyRegister(this.Handle, 100,
this.registerModifiers, this.registerKey);
hotKeyToRegister.HotKeyPressed += new EventHandler(HotKeyPressed);
btnRegister.Enabled = false;
tbHotKey.Enabled = false;
btnUnregister.Enabled = true;
}
catch (ArgumentException argumentException)
{
MessageBox.Show(argumentException.Message);
}
catch (ApplicationException applicationException)
{
MessageBox.Show(applicationException.Message);
}
}
void HotKeyPressed(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Activate();
}
private void btnUnregister_Click(object sender, EventArgs e)
{
if (hotKeyToRegister != null)
{
hotKeyToRegister.Dispose();
hotKeyToRegister = null;
}
tbHotKey.Enabled = true;
btnRegister.Enabled = true;
btnUnregister.Enabled = false;
}
protected override void OnClosed(EventArgs e)
{
if (hotKeyToRegister != null)
{
hotKeyToRegister.Dispose();
hotKeyToRegister = null;
}
base.OnClosed(e);
}
}
}
HotKeyRegister.cs
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Security.Permissions;
namespace CSRegisterHotkey
{
public class HotKeyRegister : IMessageFilter, IDisposable
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id,
KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public static KeyModifiers GetModifiers(Keys keydata, out Keys key)
{
key = keydata;
KeyModifiers modifers = KeyModifiers.None;
if ((keydata & Keys.Control) == Keys.Control)
{
modifers |= KeyModifiers.Control;
key = keydata ^ Keys.Control;
}
if ((keydata & Keys.Shift) == Keys.Shift)
{
modifers |= KeyModifiers.Shift;
key = key ^ Keys.Shift;
}
if ((keydata & Keys.Alt) == Keys.Alt)
{
modifers |= KeyModifiers.Alt;
key = key ^ Keys.Alt;
}
if (key == Keys.ShiftKey || key == Keys.ControlKey || key == Keys.Menu)
{
key = Keys.None;
}
return modifers;
}
bool disposed = false;
const int WM_HOTKEY = 0x0312;
public IntPtr Handle { get; private set; }
public int ID { get; private set; }
public KeyModifiers Modifiers { get; private set; }
public Keys Key { get; private set; }
public event EventHandler HotKeyPressed;
public HotKeyRegister(IntPtr handle, int id, KeyModifiers modifiers, Keys key)
{
if (key == Keys.None || modifiers == KeyModifiers.None)
{
throw new ArgumentException("The key or modifiers could not be None.");
}
this.Handle = handle;
this.ID = id;
this.Modifiers = modifiers;
this.Key = key;
RegisterHotKey();
Application.AddMessageFilter(this);
}
private void RegisterHotKey()
{
bool isKeyRegisterd = RegisterHotKey(Handle, ID, Modifiers, Key);
if (!isKeyRegisterd)
{
UnregisterHotKey(IntPtr.Zero, ID);
isKeyRegisterd = RegisterHotKey(Handle, ID, Modifiers, Key);
if (!isKeyRegisterd)
{
throw new ApplicationException("The hotkey is in use");
}
}
}
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_HOTKEY
&& m.HWnd == this.Handle
&& m.WParam == (IntPtr)this.ID
&& HotKeyPressed != null)
{
HotKeyPressed(this, EventArgs.Empty);
return true;
}
return false;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
Application.RemoveMessageFilter(this);
UnregisterHotKey(Handle, ID);
}
disposed = true;
}
}
}
KeyModifiers.cs:
using System;
namespace CSRegisterHotkey
{
[Flags]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
}