|
| 1 | +// <copyright file="HotKey.cs" company="the-prism"> |
| 2 | +// Copyright (c) the-prism. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +#pragma warning disable SA1121 // Use built-in type alias |
| 7 | +namespace UnManaged |
| 8 | +{ |
| 9 | + using System; |
| 10 | + using System.Collections.Generic; |
| 11 | + using System.Diagnostics; |
| 12 | + using System.Runtime.InteropServices; |
| 13 | + using System.Windows.Input; |
| 14 | + using System.Windows.Interop; |
| 15 | + |
| 16 | + /// <summary>Keymodifiers flags</summary> |
| 17 | + [Flags] |
| 18 | + public enum KeyModifier |
| 19 | + { |
| 20 | + /// <summary>No modifiers</summary> |
| 21 | + None = 0x0000, |
| 22 | + |
| 23 | + /// <summary>Alt key</summary> |
| 24 | + Alt = 0x0001, |
| 25 | + |
| 26 | + /// <summary>Ctrl key</summary> |
| 27 | + Ctrl = 0x0002, |
| 28 | + |
| 29 | + /// <summary>No repeat key</summary> |
| 30 | + NoRepeat = 0x4000, |
| 31 | + |
| 32 | + /// <summary>Shift key</summary> |
| 33 | + Shift = 0x0004, |
| 34 | + |
| 35 | + /// <summary>Windows key</summary> |
| 36 | + Win = 0x0008, |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary>Class to handle win32 hotkeys</summary> |
| 40 | + public class HotKey : IDisposable |
| 41 | + { |
| 42 | + /// <summary>Hotkey event id</summary> |
| 43 | + public const int WmHotKey = 0x0312; |
| 44 | + |
| 45 | + private static Dictionary<int, HotKey> dictHotKeyToCalBackProc; |
| 46 | + private bool disposed = false; |
| 47 | + |
| 48 | + /// <summary>Initializes a new instance of the <see cref="HotKey"/> class.</summary> |
| 49 | + /// <param name="k">Key to register</param> |
| 50 | + /// <param name="keyModifiers">Modifiers to register</param> |
| 51 | + /// <param name="action">Callback to register</param> |
| 52 | + /// <param name="register">Should the keybind be registered with the system</param> |
| 53 | + public HotKey(Key k, KeyModifier keyModifiers, Action<HotKey> action, bool register = true) |
| 54 | + { |
| 55 | + this.Key = k; |
| 56 | + this.KeyModifiers = keyModifiers; |
| 57 | + this.Action = action; |
| 58 | + if (register) |
| 59 | + { |
| 60 | + this.Register(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary>Gets key pressed</summary> |
| 65 | + public Key Key { get; private set; } |
| 66 | + |
| 67 | + /// <summary>Gets modfiers pressed</summary> |
| 68 | + public KeyModifier KeyModifiers { get; private set; } |
| 69 | + |
| 70 | + /// <summary>Gets callback for the keypress</summary> |
| 71 | + public Action<HotKey> Action { get; private set; } |
| 72 | + |
| 73 | + /// <summary>Registered id for the keybind</summary> |
| 74 | + public int Id { get; set; } |
| 75 | + |
| 76 | + /// <summary>Register the keybind</summary> |
| 77 | + /// <returns>If the registration was successfull</returns> |
| 78 | + public bool Register() |
| 79 | + { |
| 80 | + int virtualKeyCode = KeyInterop.VirtualKeyFromKey(this.Key); |
| 81 | + this.Id = virtualKeyCode + ((int)this.KeyModifiers * 0x10000); |
| 82 | + bool result = RegisterHotKey(IntPtr.Zero, this.Id, (UInt32)this.KeyModifiers, (UInt32)virtualKeyCode); |
| 83 | + |
| 84 | + if (dictHotKeyToCalBackProc == null) |
| 85 | + { |
| 86 | + dictHotKeyToCalBackProc = new Dictionary<int, HotKey>(); |
| 87 | + ComponentDispatcher.ThreadFilterMessage += new ThreadMessageEventHandler(ComponentDispatcherThreadFilterMessage); |
| 88 | + } |
| 89 | + |
| 90 | + dictHotKeyToCalBackProc.Add(this.Id, this); |
| 91 | + |
| 92 | + Debug.Print(result.ToString() + ", " + this.Id + ", " + virtualKeyCode); |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary>Unregister the keybind</summary> |
| 97 | + public void Unregister() |
| 98 | + { |
| 99 | + HotKey hotKey; |
| 100 | + if (dictHotKeyToCalBackProc.TryGetValue(this.Id, out hotKey)) |
| 101 | + { |
| 102 | + UnregisterHotKey(IntPtr.Zero, this.Id); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Implement IDisposable. |
| 108 | + /// Do not make this method virtual. |
| 109 | + /// A derived class should not be able to override this method. |
| 110 | + /// </summary> |
| 111 | + public void Dispose() |
| 112 | + { |
| 113 | + this.Dispose(true); |
| 114 | + // This object will be cleaned up by the Dispose method. |
| 115 | + // Therefore, you should call GC.SupressFinalize to |
| 116 | + // take this object off the finalization queue |
| 117 | + // and prevent finalization code for this object |
| 118 | + // from executing a second time. |
| 119 | + GC.SuppressFinalize(this); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Dispose(bool disposing) executes in two distinct scenarios. |
| 124 | + /// If disposing equals true, the method has been called directly |
| 125 | + /// or indirectly by a user's code. Managed and unmanaged resources |
| 126 | + /// can be _disposed. |
| 127 | + /// If disposing equals false, the method has been called by the |
| 128 | + /// runtime from inside the finalizer and you should not reference |
| 129 | + /// other objects. Only unmanaged resources can be _disposed. |
| 130 | + /// </summary> |
| 131 | + /// <param name="disposing"></param> |
| 132 | + protected virtual void Dispose(bool disposing) |
| 133 | + { |
| 134 | + // Check to see if Dispose has already been called. |
| 135 | + if (!this.disposed) |
| 136 | + { |
| 137 | + // If disposing equals true, dispose all managed |
| 138 | + // and unmanaged resources. |
| 139 | + if (disposing) |
| 140 | + { |
| 141 | + // Dispose managed resources. |
| 142 | + this.Unregister(); |
| 143 | + } |
| 144 | + |
| 145 | + // Note disposing has been done. |
| 146 | + this.disposed = true; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + [DllImport("user32.dll")] |
| 151 | + private static extern bool RegisterHotKey(IntPtr hWnd, int id, UInt32 fsModifiers, UInt32 vlc); |
| 152 | + |
| 153 | + [DllImport("user32.dll")] |
| 154 | + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); |
| 155 | + |
| 156 | + // ****************************************************************** |
| 157 | + private static void ComponentDispatcherThreadFilterMessage(ref MSG msg, ref bool handled) |
| 158 | + { |
| 159 | + if (!handled) |
| 160 | + { |
| 161 | + if (msg.message == WmHotKey) |
| 162 | + { |
| 163 | + HotKey hotKey; |
| 164 | + |
| 165 | + if (dictHotKeyToCalBackProc.TryGetValue((int)msg.wParam, out hotKey)) |
| 166 | + { |
| 167 | + if (hotKey.Action != null) |
| 168 | + { |
| 169 | + hotKey.Action.Invoke(hotKey); |
| 170 | + } |
| 171 | + |
| 172 | + handled = true; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +#pragma warning restore SA1121 // Use built-in type alias |
0 commit comments