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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Input; using System.Windows.Interop; using System.Windows; using System.Diagnostics;
namespace QuickPopupSnippet { public class GlobalHotkey { const int WM_HOTKEY = 0x312; static int keyid = 10; static Dictionary<int, HotKeyCallBackHanlder> keymap = new();
public delegate void HotKeyCallBackHanlder();
#region API
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")] static extern bool UnregisterHotKey(IntPtr hWnd, int id);
#endregion
public static void Regist( Window window, uint fsModifiers, Key key, HotKeyCallBackHanlder callBack ) { var hwnd = new WindowInteropHelper(window).Handle; var _hwndSource = HwndSource.FromHwnd(hwnd); _hwndSource.AddHook(WndProc);
int id = keyid++;
var vk = KeyInterop.VirtualKeyFromKey(key); if (!RegisterHotKey(hwnd, id, fsModifiers, (uint)vk)) throw new Exception("regist hotkey fail."); keymap[id] = callBack; }
static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_HOTKEY) { int id = wParam.ToInt32(); if (keymap.TryGetValue(id, out var callback)) { callback(); } } return IntPtr.Zero; }
public static void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) { if (var.Value == callBack) UnregisterHotKey(hWnd, var.Key); } }
public static void UnRegistNoChecking(IntPtr hWnd) { foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) { UnregisterHotKey(hWnd, var.Key); } }
public static void UnRegistNoChecking(Window window) { var hwnd = new WindowInteropHelper(window).Handle; foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) { UnregisterHotKey(hwnd, var.Key); } }
public enum HotkeyModifiers { Alt = 0x1, Ctrl = 0x2, Shift = 0x4, Win = 0x8 }
static Dictionary<string, HotkeyModifiers> STR_HOTKEY_MAP = new() { { "Alt", HotkeyModifiers.Alt }, { "alt", HotkeyModifiers.Alt }, { "Ctrl", HotkeyModifiers.Ctrl }, { "ctrl", HotkeyModifiers.Ctrl }, { "Control", HotkeyModifiers.Ctrl }, { "control", HotkeyModifiers.Ctrl }, { "Shift", HotkeyModifiers.Shift }, { "shift", HotkeyModifiers.Shift }, { "Win", HotkeyModifiers.Win }, { "win", HotkeyModifiers.Win }, { "windows", HotkeyModifiers.Win }, { "Windows", HotkeyModifiers.Win } };
public static uint GetModifiersFromStrings(string[] texts) { uint result = 0x0; foreach (var key in texts) { if (STR_HOTKEY_MAP.TryGetValue(key, out var value)) { result |= (uint)value; Debug.WriteLine($"hotkey modifier: {key} parsed as {value}"); } } Debug.WriteLine($"hotkey modifier: final is {result}"); return result; }
public static Key GetKeyFromString(string key) { return (Key)Enum.Parse(typeof(Key), key, true); } } }
|