GlobalHotKey.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  52. catch
  53. {
  54. //nothing
  55. }
  56. }
  57. // Registers a hot key with Windows.
  58. [DllImport("user32.dll")]
  59. private static extern bool RegisterHotKey(IntPtr hWnd, int id, HotkeyModifiers fsModifiers, uint vk);
  60. // Unregisters the hot key with Windows.
  61. [DllImport("user32.dll")]
  62. private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  63. private class InvisibleWindowForMessages : NativeWindow, IDisposable
  64. {
  65. public event HotKeyCallBackHanlder callback;
  66. public InvisibleWindowForMessages(HotKeyCallBackHanlder callback)
  67. {
  68. CreateHandle(new CreateParams());
  69. this.callback += callback;
  70. }
  71. private static readonly int WM_HOTKEY = 0x0312;
  72. protected override void WndProc(ref Message m)
  73. {
  74. base.WndProc(ref m);
  75. if (m.Msg == WM_HOTKEY)
  76. {
  77. callback();
  78. }
  79. }
  80. public void Dispose()
  81. {
  82. this.DestroyHandle();
  83. }
  84. }
  85. }
  86. }