KeyboardListener.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. namespace MediaBrowser.UI.UserInput
  6. {
  7. /// <summary>
  8. /// Provides a basic low-level keyboard listener
  9. /// Inspired by http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
  10. /// Use the KeyDown event to listen for keys.
  11. /// Make sure to detach from the event when not needed.
  12. /// </summary>
  13. public static class KeyboardListener
  14. {
  15. #region KeyDown EventHandler
  16. /// <summary>
  17. /// The _ key down
  18. /// </summary>
  19. static volatile EventHandler<KeyEventArgs> _KeyDown;
  20. /// <summary>
  21. /// Fires whenever CurrentItem changes
  22. /// </summary>
  23. public static event EventHandler<KeyEventArgs> KeyDown
  24. {
  25. add
  26. {
  27. if (_KeyDown == null)
  28. {
  29. StartListening();
  30. }
  31. _KeyDown += value;
  32. }
  33. remove
  34. {
  35. _KeyDown -= value;
  36. if (_KeyDown == null && _hookID != IntPtr.Zero)
  37. {
  38. StopListening();
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// Raises the <see cref="E:KeyDown" /> event.
  44. /// </summary>
  45. /// <param name="e">The <see cref="KeyEventArgs" /> instance containing the event data.</param>
  46. private static void OnKeyDown(KeyEventArgs e)
  47. {
  48. e.SuppressKeyPress = false;
  49. if (_KeyDown != null)
  50. {
  51. // For now, don't async this
  52. // This will give listeners a chance to modify SuppressKeyPress if they want
  53. try
  54. {
  55. _KeyDown(null, e);
  56. }
  57. catch (Exception ex)
  58. {
  59. }
  60. }
  61. }
  62. #endregion
  63. /// <summary>
  64. /// The W h_ KEYBOAR d_ LL
  65. /// </summary>
  66. private const int WH_KEYBOARD_LL = 13;
  67. /// <summary>
  68. /// The W m_ KEYDOWN
  69. /// </summary>
  70. private const int WM_KEYDOWN = 0x0100;
  71. /// <summary>
  72. /// The W m_ SYSKEYDOWN
  73. /// </summary>
  74. private const int WM_SYSKEYDOWN = 0x0104;
  75. /// <summary>
  76. /// The _hook ID
  77. /// </summary>
  78. private static IntPtr _hookID = IntPtr.Zero;
  79. /// <summary>
  80. /// The _proc
  81. /// </summary>
  82. private static LowLevelKeyboardProc _proc = HookCallback;
  83. /// <summary>
  84. /// Starts the listening.
  85. /// </summary>
  86. private static void StartListening()
  87. {
  88. _hookID = SetHook(_proc);
  89. }
  90. /// <summary>
  91. /// Stops the listening.
  92. /// </summary>
  93. private static void StopListening()
  94. {
  95. UnhookWindowsHookEx(_hookID);
  96. _hookID = IntPtr.Zero;
  97. }
  98. /// <summary>
  99. /// Sets the hook.
  100. /// </summary>
  101. /// <param name="proc">The proc.</param>
  102. /// <returns>IntPtr.</returns>
  103. private static IntPtr SetHook(LowLevelKeyboardProc proc)
  104. {
  105. using (var curProcess = Process.GetCurrentProcess())
  106. using (var curModule = curProcess.MainModule)
  107. {
  108. return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  109. GetModuleHandle(curModule.ModuleName), 0);
  110. }
  111. }
  112. /// <summary>
  113. /// Hooks the callback.
  114. /// </summary>
  115. /// <param name="nCode">The n code.</param>
  116. /// <param name="wParam">The w param.</param>
  117. /// <param name="lParam">The l param.</param>
  118. /// <returns>IntPtr.</returns>
  119. private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  120. {
  121. var suppressKeyPress = false;
  122. if (nCode >= 0)
  123. {
  124. if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
  125. {
  126. var vkCode = Marshal.ReadInt32(lParam);
  127. var keyData = (Keys)vkCode;
  128. var e = new KeyEventArgs(keyData);
  129. OnKeyDown(e);
  130. suppressKeyPress = e.SuppressKeyPress;
  131. }
  132. }
  133. if (suppressKeyPress)
  134. {
  135. return IntPtr.Zero;
  136. }
  137. return CallNextHookEx(_hookID, nCode, wParam, lParam);
  138. }
  139. /// <summary>
  140. /// Delegate LowLevelKeyboardProc
  141. /// </summary>
  142. /// <param name="nCode">The n code.</param>
  143. /// <param name="wParam">The w param.</param>
  144. /// <param name="lParam">The l param.</param>
  145. /// <returns>IntPtr.</returns>
  146. private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  147. #region Imports
  148. /// <summary>
  149. /// Sets the windows hook ex.
  150. /// </summary>
  151. /// <param name="idHook">The id hook.</param>
  152. /// <param name="lpfn">The LPFN.</param>
  153. /// <param name="hMod">The h mod.</param>
  154. /// <param name="dwThreadId">The dw thread id.</param>
  155. /// <returns>IntPtr.</returns>
  156. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  157. private static extern IntPtr SetWindowsHookEx(int idHook,
  158. LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  159. /// <summary>
  160. /// Unhooks the windows hook ex.
  161. /// </summary>
  162. /// <param name="hhk">The HHK.</param>
  163. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  164. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  165. [return: MarshalAs(UnmanagedType.Bool)]
  166. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  167. /// <summary>
  168. /// Calls the next hook ex.
  169. /// </summary>
  170. /// <param name="hhk">The HHK.</param>
  171. /// <param name="nCode">The n code.</param>
  172. /// <param name="wParam">The w param.</param>
  173. /// <param name="lParam">The l param.</param>
  174. /// <returns>IntPtr.</returns>
  175. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  176. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  177. IntPtr wParam, IntPtr lParam);
  178. /// <summary>
  179. /// Gets the module handle.
  180. /// </summary>
  181. /// <param name="lpModuleName">Name of the lp module.</param>
  182. /// <returns>IntPtr.</returns>
  183. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  184. private static extern IntPtr GetModuleHandle(string lpModuleName);
  185. #endregion
  186. }
  187. }