From 4ca78a5c287cb4907a3b6cff82671069750d3ec5 Mon Sep 17 00:00:00 2001 From: lhiven <236881222@qq.com> Date: Mon, 11 Dec 2023 12:14:55 +0900 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=B3=BB=E7=BB=9F=E7=83=AD?= =?UTF-8?q?=E9=94=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rs.Framework/HotKeyManager.cs | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Rs.Framework/HotKeyManager.cs b/Rs.Framework/HotKeyManager.cs index ad2626a..9aec534 100644 --- a/Rs.Framework/HotKeyManager.cs +++ b/Rs.Framework/HotKeyManager.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; namespace Rs.Framework { @@ -11,5 +13,53 @@ namespace Rs.Framework /// public class HotKeyManager { + private const int MOD_ALT = 0x0001; // Alt键 + private const int MOD_CTRL = 0x0002; // Ctrl键 + private const int MOD_SHIFT = 0x0004; // Shift键 + private const int MOD_WIN = 0x0008; // Windows键 + private const int WM_HOTKEY = 0x0312; + + private Action hotkeyAction; + private int id; + + [DllImport("user32.dll")] + private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, Keys vk); + + [DllImport("user32.dll")] + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + public HotKeyManager(Keys key, int modifier, Action action) + { + hotkeyAction = action; + id = this.GetHashCode(); + + RegisterHotKey(Application.OpenForms[0].Handle, id, modifier, key); + Application.AddMessageFilter(new MessageFilter(this)); + } + + public void Unregister() + { + UnregisterHotKey(Application.OpenForms[0].Handle, id); + } + + private class MessageFilter : IMessageFilter + { + private HotKeyManager hotkey; + + public MessageFilter(HotKeyManager hotkey) + { + this.hotkey = hotkey; + } + + public bool PreFilterMessage(ref Message m) + { + if (m.Msg == WM_HOTKEY && (int)m.WParam == hotkey.id) + { + hotkey.hotkeyAction(null, EventArgs.Empty); + return true; + } + return false; + } + } } }