GlobalHotKey.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. using System.Windows.Input;
  6. namespace GeekDesk.Util
  7. {
  8. public class GlobalHotKey
  9. {
  10. public enum HotkeyModifiers
  11. {
  12. MOD_ALT = 0x1,
  13. MOD_CONTROL = 0x2,
  14. MOD_SHIFT = 0x4,
  15. MOD_WIN = 0x8
  16. }
  17. private static int currentID;
  18. private static readonly Dictionary<int, InvisibleWindowForMessages> handleTemp = new Dictionary<int, InvisibleWindowForMessages>();
  19. public delegate void HotKeyCallBackHanlder();
  20. /// <summary>
  21. /// Registers a global hotkey
  22. /// </summary>
  23. /// <param name="aKeyGestureString">e.g. Alt + Shift + Control + Win + S</param>
  24. /// <param name="callback">Action to be called when hotkey is pressed</param>
  25. /// <returns>true, if registration succeeded, otherwise false</returns>
  26. public static int RegisterHotKey(string aKeyGestureString, HotKeyCallBackHanlder callback)
  27. {
  28. var c = new KeyGestureConverter();
  29. KeyGesture aKeyGesture = (KeyGesture)c.ConvertFrom(aKeyGestureString);
  30. return RegisterHotKey((HotkeyModifiers)aKeyGesture.Modifiers, aKeyGesture.Key, callback);
  31. }
  32. public static int RegisterHotKey(HotkeyModifiers aModifier, Key key, HotKeyCallBackHanlder callback)
  33. {
  34. InvisibleWindowForMessages window = new InvisibleWindowForMessages(callback);
  35. currentID += 1;
  36. if (!RegisterHotKey(window.Handle, currentID, aModifier, (uint)KeyInterop.VirtualKeyFromKey(key)))
  37. {
  38. window.Dispose();
  39. throw new Exception("RegisterHotKey Failed");
  40. }
  41. handleTemp.Add(currentID, window);
  42. return currentID;
  43. }
  44. public static void Dispose(int id)
  45. {
  46. try
  47. {
  48. UnregisterHotKey(handleTemp[id].Handle, id);
  49. GlobalHotKey.handleTemp[id].Dispose();
  50. GlobalHotKey.handleTemp.Remove(id);
  51. } catch
  52. {
  53. //nothing
  54. }
  55. }
  56. // Registers a hot key with Windows.
  57. [DllImport("user32.dll")]
  58. private static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers, uint vk);
  59. // Unregisters the hot key with Windows.
  60. [DllImport("user32.dll")]
  61. private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  62. private class InvisibleWindowForMessages : NativeWindow, IDisposable
  63. {
  64. public event HotKeyCallBackHanlder callback;
  65. public InvisibleWindowForMessages(HotKeyCallBackHanlder callback)
  66. {
  67. CreateHandle(new CreateParams());
  68. this.callback += callback;
  69. }
  70. private static readonly int WM_HOTKEY = 0x0312;
  71. protected override void WndProc(ref Message m)
  72. {
  73. base.WndProc(ref m);
  74. if (m.Msg == WM_HOTKEY)
  75. {
  76. callback();
  77. }
  78. }
  79. public void Dispose()
  80. {
  81. this.DestroyHandle();
  82. }
  83. }
  84. }
  85. }