|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Drawing; |
| 5 | +using System.Linq; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | +namespace TopMostFriend { |
| 10 | + public sealed class HotKeyWindow : Form { |
| 11 | + public class HotKeyInfo { |
| 12 | + public string Name { get; } |
| 13 | + public int Atom { get; } |
| 14 | + public int Key { get; } |
| 15 | + public Action Action { get; } |
| 16 | + |
| 17 | + public HotKeyInfo(string name, int atom, int key, Action action) { |
| 18 | + Name = name ?? throw new ArgumentNullException(nameof(name)); |
| 19 | + Atom = atom; |
| 20 | + Key = key; |
| 21 | + Action = action ?? throw new ArgumentNullException(nameof(action)); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private readonly List<HotKeyInfo> RegisteredHotKeys = new List<HotKeyInfo>(); |
| 26 | + |
| 27 | + public HotKeyWindow() { |
| 28 | + ShowInTaskbar = false; |
| 29 | + Text = string.Empty; |
| 30 | + FormBorderStyle = FormBorderStyle.None; |
| 31 | + StartPosition = FormStartPosition.Manual; |
| 32 | + Size = new Size(1, 1); |
| 33 | + Location = new Point(-9999, -9999); |
| 34 | + CreateHandle(); |
| 35 | + Hide(); |
| 36 | + } |
| 37 | + |
| 38 | + protected override void OnFormClosing(FormClosingEventArgs e) { |
| 39 | + e.Cancel = e.CloseReason == CloseReason.UserClosing; |
| 40 | + } |
| 41 | + |
| 42 | + protected override void WndProc(ref Message m) { |
| 43 | + base.WndProc(ref m); |
| 44 | + |
| 45 | + if(m.Msg == Win32.WM_HOTKEY) { |
| 46 | + int keyCode = m.LParam.ToInt32(); |
| 47 | + |
| 48 | + lock (RegisteredHotKeys) |
| 49 | + RegisteredHotKeys.FirstOrDefault(x => x.Key == keyCode)?.Action.Invoke(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + protected override void Dispose(bool disposing) { |
| 54 | + lock (RegisteredHotKeys) { |
| 55 | + HotKeyInfo[] hotKeys = RegisteredHotKeys.ToArray(); |
| 56 | + |
| 57 | + foreach(HotKeyInfo hotKey in hotKeys) |
| 58 | + Unregister(hotKey.Atom); |
| 59 | + } |
| 60 | + |
| 61 | + base.Dispose(disposing); |
| 62 | + } |
| 63 | + |
| 64 | + public int Register(string name, Win32ModKeys modifiers, Keys key, Action action) { |
| 65 | + if (action == null) |
| 66 | + throw new ArgumentNullException(nameof(action)); |
| 67 | + if(string.IsNullOrEmpty(name)) |
| 68 | + name = Guid.NewGuid().ToString(); |
| 69 | + |
| 70 | + int atom = Win32.GlobalAddAtom(name); |
| 71 | + int keyCode = ((ushort)key << 16) | (ushort)modifiers; |
| 72 | + |
| 73 | + if (atom == 0) |
| 74 | + throw new Win32Exception(Marshal.GetLastWin32Error(), @"Atom creation failed."); |
| 75 | + |
| 76 | + if (!Win32.RegisterHotKey(Handle, atom, modifiers, key)) { |
| 77 | + Win32.GlobalDeleteAtom((ushort)atom); |
| 78 | + throw new Win32Exception(Marshal.GetLastWin32Error(), @"Hotkey registration failed."); |
| 79 | + } |
| 80 | + |
| 81 | + lock(RegisteredHotKeys) |
| 82 | + RegisteredHotKeys.Add(new HotKeyInfo(name, atom, keyCode, action)); |
| 83 | + |
| 84 | + return atom; |
| 85 | + } |
| 86 | + |
| 87 | + public void Unregister(int id) { |
| 88 | + if (id < 1) |
| 89 | + return; |
| 90 | + |
| 91 | + lock (RegisteredHotKeys) { |
| 92 | + if (!RegisteredHotKeys.Any(x => x.Atom == id)) |
| 93 | + return; |
| 94 | + RegisteredHotKeys.RemoveAll(x => x.Atom == id); |
| 95 | + } |
| 96 | + |
| 97 | + Win32.UnregisterHotKey(Handle, id); |
| 98 | + Win32.GlobalDeleteAtom((ushort)id); |
| 99 | + } |
| 100 | + |
| 101 | + public void Unregister(string name) { |
| 102 | + int atom = 0; |
| 103 | + |
| 104 | + lock (RegisteredHotKeys) |
| 105 | + atom = RegisteredHotKeys.FirstOrDefault(x => x.Name == name)?.Atom ?? 0; |
| 106 | + |
| 107 | + Unregister(atom); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments