在wpf中是没有注册热键的API的
需要调用到以下两个函数
//返回值不为0即注册成功
[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);//注销热键
RegisterHotKey函数参考
https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-registerhotkey
UnregisterHotKey函数参考
https://learn.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-unregisterhotkey
注册热键
private IntPtr _windowHandle;
private HwndSource _source;
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_windowHandle = new WindowInteropHelper(this).Handle;
_source = HwndSource.FromHwnd(_windowHandle);
_source.AddHook(HwndHook);
RegisterHotKey(_windowHandle, HOTKEY_ID, 0, 0x73);
//_windowHandle: 窗口句柄,当触发热键时,会被该窗口接收系统生成的消息
//HOTKEY_ID: 热键id
/*0: 组合键, 0代表不使用组合键,还可以是以下的值
private const uint MOD_NONE = 0x0000; //[NONE]
private const uint MOD_ALT = 0x0001; //ALT
private const uint MOD_CONTROL = 0x0002; //CTRL
pri