Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4.  
  5. public class KeyboardHook : IDisposable
  6. {
  7. // Registers a hot key with Windows.
  8. [DllImport("user32.dll")]
  9. private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
  10. // Unregisters the hot key with Windows.
  11. [DllImport("user32.dll")]
  12. private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  13.  
  14. /// <summary>
  15. /// Represents the window that is used internally to get the messages.
  16. /// </summary>
  17. private class Window : NativeWindow, IDisposable
  18. {
  19. private static int WM_HOTKEY = 0x0312;
  20.  
  21. public Window()
  22. {
  23. // create the handle for the window.
  24. this.CreateHandle(new CreateParams());
  25. }
  26.  
  27. /// <summary>
  28. /// Overridden to get the notifications.
  29. /// </summary>
  30. /// <param name="m"></param>
  31. protected override void WndProc(ref Message m)
  32. {
  33. base.WndProc(ref m);
  34.  
  35. // check if we got a hot key pressed.
  36. if (m.Msg == WM_HOTKEY)
  37. {
  38. // get the keys.
  39. Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
  40. ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
  41.  
  42. // invoke the event to notify the parent.
  43. if (KeyPressed != null)
  44. KeyPressed(this, new KeyPressedEventArgs(modifier, key));
  45. }
  46. }
  47.  
  48. public event EventHandler<KeyPressedEventArgs> KeyPressed;
  49.  
  50. #region IDisposable Members
  51.  
  52. public void Dispose()
  53. {
  54. this.DestroyHandle();
  55. }
  56.  
  57. #endregion
  58. }
  59.  
  60. private Window _window = new Window();
  61. private int _currentId;
  62.  
  63. public KeyboardHook()
  64. {
  65. // register the event of the inner native window.
  66. _window.KeyPressed += delegate (object sender, KeyPressedEventArgs args)
  67. {
  68. if (KeyPressed != null)
  69. KeyPressed(this, args);
  70. };
  71. }
  72.  
  73. /// <summary>
  74. /// Registers a hot key in the system.
  75. /// </summary>
  76. /// <param name="modifier">The modifiers that are associated with the hot key.</param>
  77. /// <param name="key">The key itself that is associated with the hot key.</param>
  78. public void RegisterHotKey(ModifierKeys modifier, Keys key)
  79. {
  80. // increment the counter.
  81. _currentId = _currentId + 1;
  82.  
  83. // register the hot key.
  84. if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
  85. throw new InvalidOperationException("Couldn’t register the hot key.");
  86. }
  87.  
  88. /// <summary>
  89. /// A hot key has been pressed.
  90. /// </summary>
  91. public event EventHandler<KeyPressedEventArgs> KeyPressed;
  92.  
  93. #region IDisposable Members
  94.  
  95. public void Dispose()
  96. {
  97. // unregister all the registered hot keys.
  98. for (int i = _currentId; i > 0; i--)
  99. {
  100. UnregisterHotKey(_window.Handle, i);
  101. }
  102.  
  103. // dispose the inner native window.
  104. _window.Dispose();
  105. }
  106.  
  107. #endregion
  108. }
  109.  
  110. /// <summary>
  111. /// Event Args for the event that is fired after the hot key has been pressed.
  112. /// </summary>
  113. public class KeyPressedEventArgs : EventArgs
  114. {
  115. private ModifierKeys _modifier;
  116. private Keys _key;
  117.  
  118. internal KeyPressedEventArgs(ModifierKeys modifier, Keys key)
  119. {
  120. _modifier = modifier;
  121. _key = key;
  122. }
  123.  
  124. public ModifierKeys Modifier
  125. {
  126. get { return _modifier; }
  127. }
  128.  
  129. public Keys Key
  130. {
  131. get { return _key; }
  132. }
  133. }
  134.  
  135. /// <summary>
  136. /// The enumeration of possible modifiers.
  137. /// </summary>
  138. [Flags]
  139. public enum ModifierKeys : uint
  140. {
  141. None = 0,
  142. Alt = 1,
  143. Control = 2,
  144. Shift = 4,
  145. Win = 8
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement