WPF 全局快捷键
示例:按ESC快捷键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #region 注册全局快捷键 //引入Winows API [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 const int HOTKEY_ID = 9527; //Modifiers: private const uint MOD_NONE = 0x0000; //(none) private const uint MOD_ALT = 0x0001; //ALT private const uint MOD_CONTROL = 0x0002; //CTRL private const uint MOD_SHIFT = 0x0004; //SHIFT private const uint MOD_WIN = 0x0008; //WINDOWS //CAPS LOCK: private const uint VK_CAPITAL = 0x14; 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, 第一个键, 第二个键); RegisterHotKey(_windowHandle, HOTKEY_ID, 0, 0x1B); } private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { const int WM_HOTKEY = 0x0312; //热键消息 switch (msg) { case WM_HOTKEY: switch (wParam.ToInt32()) { case HOTKEY_ID: int vkey = ((( int )lParam >> 16) & 0xFFFF); if (vkey == 0x1B) { this .Topmost = false ; this .WindowStyle = System.Windows.WindowStyle.None; this .WindowState = System.Windows.WindowState.Normal; } handled = true ; break ; } break ; } return IntPtr.Zero; } protected override void OnClosed(EventArgs e) { _source.RemoveHook(HwndHook); UnregisterHotKey(_windowHandle, HOTKEY_ID); base .OnClosed(e); } #endregion |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2023-05-23 SQL 同步更新 记录20230523
2021-05-23 WPF根据窗口内容自动调整窗口大小
2021-05-23 WPF MainWindow设置背景透明仍然显示黑色Black问题