Code
const int WM_HOTKEY = 0x0312; // 热键消息
const int ALT_TAB= 100;//自定义
/// <summary>
/// 注册系统所需热键
/// </summary>
private void RegisterHotKey()
{
this.SetHotKey(Keys.TAB, ALT_TAB);
}
protected override void WndProc(ref Message msg)
{
if (msg.Msg != WM_HOTKEY)
{
base.WndProc(ref msg);
}
}
private void SetHotKey(Keys nowKey, int id,)
{
KeyModifiers modifier;
modifier |= WinApi.KeyModifiers.Control;
WinApi.RegisterHotKey(Handle, id, modifier, nowKey);
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
Keys vk
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}