using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace SysFuns { /// /// 功能键 /// [Flags()] public enum KeyModifiers { /// /// 无 /// None = 0, /// /// Alt键 /// Alt = 1, /// /// Ctrl键 /// Ctrl = 2, /// /// Shift键 /// Shift = 4, /// /// Win键 /// WindowsKey = 8 } /// /// 热键类 /// public class WinHotReg : System.Windows.Forms.IMessageFilter { //如果函数执行成功,返回值不为0。 //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。 [DllImport("user32.dll", SetLastError = true)] private static extern bool RegisterHotKey( IntPtr hWnd, //要定义热键的窗口的句柄 int id, //定义热键ID(不能与其它ID重复) KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 Keys vk //定义热键的内容 ); [DllImport("user32.dll", SetLastError = true)] private static extern bool UnregisterHotKey( IntPtr hWnd, //要取消热键的窗口的句柄 int id //要取消热键的ID ); readonly IntPtr handle; /// /// 热键类 /// /// public WinHotReg(IntPtr _handle) { handle = _handle; System.Windows.Forms.Application.AddMessageFilter(this); } /// /// 热键注销 /// ~WinHotReg() { UnHotKey(); System.Windows.Forms.Application.RemoveMessageFilter(this); } /// /// 热键是否有效 /// public bool Enabled { get; set; } = true; /// /// 释放热键 /// public void Free() { UnHotKey(); System.Windows.Forms.Application.RemoveMessageFilter(this); } List list_hotkey = new List(); /// /// 注册热键 /// /// /// /// /// public bool RegHotKey(int id, KeyModifiers fsModifiers, Keys vk) { bool result = RegisterHotKey(handle, id, fsModifiers, vk); if (result) { list_hotkey.Add(new HotKeyType(id, fsModifiers, vk)); } return result; } /// /// 注册热键 /// /// /// /// /// public bool RegHotKey(int id, int fsModifiers, Keys vk) { if (!Enum.IsDefined(typeof(KeyModifiers), fsModifiers)) { return false; } bool result = RegisterHotKey(handle, id, (KeyModifiers)fsModifiers, vk); if (result) { list_hotkey.Add(new HotKeyType(id, (KeyModifiers)fsModifiers, vk)); } return result; } /// /// 删除热键 /// /// /// public bool UnHotKey(int id) { bool result = UnregisterHotKey(handle, id); if (result) { for (int i = list_hotkey.Count - 1; i >= 0; i--) { HotKeyType item = (HotKeyType)list_hotkey[i]; if (item.id == id) { list_hotkey.RemoveAt(i); break; } } } return result; } /// /// 删除所有热键 /// public void UnHotKey() { for (int i = list_hotkey.Count - 1; i >= 0; i--) { HotKeyType item = (HotKeyType)list_hotkey[i]; bool result = UnregisterHotKey(handle, item.id); if (result) { list_hotkey.RemoveAt(i); } } } /// /// 热键事件 /// /// public delegate void HotkeyEventHandler(int HotKeyID); /// /// 当使用热键时发生的事件 /// public event HotkeyEventHandler OnHotkey; /// /// 热键消息过滤 /// /// /// public bool PreFilterMessage(ref System.Windows.Forms.Message m) { if (m.Msg == 0x312) { if (OnHotkey != null) { if (!Enabled) { return true; } for (int i = list_hotkey.Count - 1; i >= 0; i--) { HotKeyType item = (HotKeyType)list_hotkey[i]; if ((UInt32)m.WParam == item.id) { OnHotkey((int)m.WParam); return true; } //OnHotkey((int)m.WParam); } //OnHotkey((int)m.WParam); } } return false; } } /// /// 热键类型 /// public class HotKeyType { /// /// 热键id /// public int id; /// /// 功能键 /// public KeyModifiers fsModifiers; /// /// 普通键 /// public Keys vk; /// /// 热键类型 /// /// /// /// public HotKeyType(int id, KeyModifiers fsModifiers, Keys vk) { this.id = id; this.fsModifiers = fsModifiers; this.vk = vk; } } }