MouseHook.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace Veldrid.Common
  6. {
  7. class KeyboardHook
  8. {
  9. #region 常数和结构
  10. #region wParam对应的按钮事件
  11. public const int WM_KEYDOWN = 0x100; // 键盘被按下
  12. public const int WM_KEYUP = 0x101; // 键盘被松开
  13. public const int WM_SYSKEYDOWN = 0x104; // 键盘被按下,这个是系统键被按下,例如Alt、Ctrl等键
  14. public const int WM_SYSKEYUP = 0x105; // 键盘被松开,这个是系统键被松开,例如Alt、Ctrl等键
  15. #endregion
  16. public const int WH_KEYBOARD_LL = 13;
  17. [StructLayout(LayoutKind.Sequential)] //声明键盘钩子的封送结构类型
  18. public class KeyboardHookStruct
  19. {
  20. public int vkCode; //表示一个在1到254间的虚似键盘码
  21. public int scanCode; //表示硬件扫描码
  22. public int flags;
  23. public int time;
  24. public int dwExtraInfo;
  25. }
  26. #endregion
  27. #region 成员变量、委托、事件
  28. private static int hHook;
  29. private static HookProc KeyboardHookDelegate;
  30. // 键盘回调委托
  31. public delegate void KeyboardHandler(Int32 wParam, KeyboardHookStruct keyboardHookStruct);
  32. // 键盘回调事件
  33. private static event KeyboardHandler Handlers;
  34. // 锁
  35. private readonly object lockObject = new object();
  36. // 当前状态,是否已经启动
  37. private volatile bool isStart = false;
  38. #endregion
  39. #region Win32的Api
  40. [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
  41. private static extern int GetWindowThreadProcessId(System.IntPtr hWnd, IntPtr intPtr);
  42. private delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
  43. //安装钩子的函数
  44. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =CallingConvention.StdCall)]
  45. private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  46. //卸下钩子的函数
  47. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =CallingConvention.StdCall)]
  48. private static extern bool UnhookWindowsHookEx(int idHook);
  49. //下一个钩挂的函数
  50. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  51. private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam,IntPtr lParam);
  52. [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  53. private static extern IntPtr GetModuleHandle(string lpModuleName);
  54. #endregion
  55. #region 单例模式
  56. private static volatile KeyboardHook MyKeyboard;
  57. private readonly static object createLock = new object();
  58. private KeyboardHook() { }
  59. public static KeyboardHook GetKeyboardHook()
  60. {
  61. if (MyKeyboard == null)
  62. {
  63. lock (createLock)
  64. {
  65. if (MyKeyboard == null)
  66. {
  67. MyKeyboard = new KeyboardHook();
  68. }
  69. }
  70. }
  71. return MyKeyboard;
  72. }
  73. #endregion
  74. /// <summary>
  75. /// 安装钩子
  76. /// </summary>
  77. public void Start(IntPtr hwnd)
  78. {
  79. if (isStart)
  80. {
  81. return;
  82. }
  83. lock (lockObject)
  84. {
  85. if (isStart)
  86. {
  87. return;
  88. }
  89. if (Handlers == null)
  90. {
  91. throw new Exception("Please set handler first!Then run Start");
  92. }
  93. KeyboardHookDelegate = new HookProc(KeyboardHookProc);
  94. var handle = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
  95. hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookDelegate, IntPtr.Zero, handle);
  96. isStart = true;
  97. }
  98. }
  99. /// <summary>
  100. /// 卸载钩子
  101. /// </summary>
  102. public void Stop()
  103. {
  104. if (!isStart)
  105. {
  106. return;
  107. }
  108. lock (lockObject)
  109. {
  110. if (!isStart)
  111. {
  112. return;
  113. }
  114. UnhookWindowsHookEx(hHook);
  115. // 清除所有事件
  116. Handlers = null;
  117. isStart = false;
  118. }
  119. }
  120. /// <summary>
  121. /// 键盘的系统回调函数
  122. /// </summary>
  123. /// <param name="nCode"></param>
  124. /// <param name="wParam"></param>
  125. /// <param name="lParam"></param>
  126. /// <returns></returns>
  127. private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
  128. {
  129. //如果该消息被丢弃(nCode<0)或者没有事件绑定处理程序则不会触发事件
  130. if ((nCode >= 0) && Handlers != null)
  131. {
  132. KeyboardHookStruct KeyDataFromHook =(KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
  133. Handlers(wParam, KeyDataFromHook);
  134. }
  135. return CallNextHookEx(hHook, nCode, wParam, lParam);
  136. }
  137. /// <summary>
  138. /// 添加按键的回调函数
  139. /// </summary>
  140. /// <param name="handler"></param>
  141. public void AddKeyboardHandler(KeyboardHandler handler)
  142. {
  143. Handlers += handler;
  144. }
  145. /// <summary>
  146. /// 删除指定按键的回调函数
  147. /// </summary>
  148. /// <param name="handler"></param>
  149. public void RemoveKeyboardHandler(KeyboardHandler handler)
  150. {
  151. if (Handlers != null)
  152. {
  153. Handlers -= handler;
  154. }
  155. }
  156. }
  157. class MouseHook
  158. {
  159. #region 常量
  160. public const int WM_MOUSEMOVE = 0x200; // 鼠标移动
  161. public const int WM_LBUTTONDOWN = 0x201;// 鼠标左键按下
  162. public const int WM_RBUTTONDOWN = 0x204;// 鼠标右键按下
  163. public const int WM_MBUTTONDOWN = 0x207;// 鼠标中键按下
  164. public const int WM_LBUTTONUP = 0x202;// 鼠标左键抬起
  165. public const int WM_RBUTTONUP = 0x205;// 鼠标右键抬起
  166. public const int WM_MBUTTONUP = 0x208;// 鼠标中键抬起
  167. public const int WM_LBUTTONDBLCLK = 0x203;// 鼠标左键双击
  168. public const int WM_RBUTTONDBLCLK = 0x206;// 鼠标右键双击
  169. public const int WM_MBUTTONDBLCLK = 0x209;// 鼠标中键双击
  170. public const int WH_MOUSE_LL = 14; //可以截获整个系统所有模块的鼠标事件。
  171. #endregion
  172. #region 成员变量、回调函数、事件
  173. /// <summary>
  174. /// 钩子回调函数
  175. /// </summary>
  176. /// <param name="nCode">如果代码小于零,则挂钩过程必须将消息传递给CallNextHookEx函数,而无需进一步处理,并且应返回CallNextHookEx返回的值。此参数可以是下列值之一。(来自官网手册)</param>
  177. /// <param name="wParam">记录了按下的按钮</param>
  178. /// <param name="lParam"></param>
  179. /// <returns></returns>
  180. public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
  181. /// <summary>
  182. /// 全局的鼠标事件
  183. /// </summary>
  184. /// <param name="wParam"> 代表发生的鼠标的事件 </param>
  185. /// <param name="mouseMsg">钩子的结构体,存储着鼠标的位置及其他信息</param>
  186. public delegate void MyMouseEventHandler(Int32 wParam, MouseHookStruct mouseMsg);
  187. private event MyMouseEventHandler OnMouseActivity;
  188. // 声明鼠标钩子事件类型
  189. private HookProc _mouseHookProcedure;
  190. private static int _hMouseHook = 0; // 鼠标钩子句柄
  191. // 锁
  192. private readonly object lockObject = new object();
  193. // 当前状态,是否已经启动
  194. private bool isStart = false;
  195. #endregion
  196. #region Win32的API
  197. /// <summary>
  198. /// 钩子结构体
  199. /// </summary>
  200. [StructLayout(LayoutKind.Sequential)]
  201. public class MouseHookStruct
  202. {
  203. public POINT pt; // 鼠标位置
  204. public int hWnd;
  205. public int wHitTestCode;
  206. public int dwExtraInfo;
  207. }
  208. //声明一个Point的封送类型
  209. [StructLayout(LayoutKind.Sequential)]
  210. public class POINT
  211. {
  212. public int x;
  213. public int y;
  214. }
  215. // 装置钩子的函数
  216. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  217. private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  218. // 卸下钩子的函数
  219. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  220. private static extern bool UnhookWindowsHookEx(int idHook);
  221. [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
  222. private static extern int GetWindowThreadProcessId(System.IntPtr hWnd, IntPtr intPtr);
  223. // 下一个钩挂的函数
  224. [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  225. private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
  226. #endregion
  227. #region 构造(单例模式)与析构函数
  228. private static volatile MouseHook MyMouseHook;
  229. private readonly static object createLock = new object();
  230. private MouseHook() { }
  231. public static MouseHook GetMouseHook()
  232. {
  233. if (MyMouseHook == null)
  234. {
  235. lock (createLock)
  236. {
  237. if (MyMouseHook == null)
  238. {
  239. MyMouseHook = new MouseHook();
  240. }
  241. }
  242. }
  243. return MyMouseHook;
  244. }
  245. /// <summary>
  246. /// 析构函数
  247. /// </summary>
  248. ~MouseHook()
  249. {
  250. Stop();
  251. }
  252. #endregion
  253. /// <summary>
  254. /// 启动全局钩子
  255. /// </summary>
  256. public void Start(IntPtr intPtr)
  257. {
  258. if (isStart)
  259. {
  260. return;
  261. }
  262. lock (lockObject)
  263. {
  264. if (isStart)
  265. {
  266. return;
  267. }
  268. if (OnMouseActivity == null)
  269. {
  270. throw new Exception("Please set handler first!Then run Start");
  271. }
  272. // 安装鼠标钩子
  273. if (_hMouseHook == 0)
  274. {
  275. var handle = GetWindowThreadProcessId(intPtr, IntPtr.Zero);
  276. // 生成一个HookProc的实例.
  277. _mouseHookProcedure = new HookProc(MouseHookProc);
  278. _hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, _mouseHookProcedure, IntPtr.Zero, handle);
  279. //假设装置失败停止钩子
  280. if (_hMouseHook == 0)
  281. {
  282. Stop();
  283. throw new Exception("SetWindowsHookEx failed.");
  284. }
  285. }
  286. isStart = true;
  287. }
  288. }
  289. /// <summary>
  290. /// 停止全局钩子
  291. /// </summary>
  292. public void Stop()
  293. {
  294. if (!isStart)
  295. {
  296. return;
  297. }
  298. lock (lockObject)
  299. {
  300. if (!isStart)
  301. {
  302. return;
  303. }
  304. bool retMouse = true;
  305. if (_hMouseHook != 0)
  306. {
  307. retMouse = UnhookWindowsHookEx(_hMouseHook);
  308. _hMouseHook = 0;
  309. }
  310. // 假设卸下钩子失败
  311. if (!(retMouse))
  312. throw new Exception("UnhookWindowsHookEx failed.");
  313. // 删除所有事件
  314. OnMouseActivity = null;
  315. // 标志位改变
  316. isStart = false;
  317. }
  318. }
  319. /// <summary>
  320. /// 鼠标钩子回调函数
  321. /// </summary>
  322. private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
  323. {
  324. // 假设正常执行而且用户要监听鼠标的消息
  325. if ((nCode >= 0) && (OnMouseActivity != null))
  326. {
  327. MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
  328. OnMouseActivity(wParam, MyMouseHookStruct);
  329. }
  330. // 启动下一次钩子
  331. return CallNextHookEx(_hMouseHook, nCode, wParam, lParam);
  332. }
  333. /// <summary>
  334. /// 注册全局鼠标事件
  335. /// </summary>
  336. /// <param name="handler"></param>
  337. public void AddMouseHandler(MyMouseEventHandler handler)
  338. {
  339. OnMouseActivity += handler;
  340. }
  341. /// <summary>
  342. /// 注销全局鼠标事件
  343. /// </summary>
  344. /// <param name="handler"></param>
  345. public void RemoveMouseHandler(MyMouseEventHandler handler)
  346. {
  347. if (OnMouseActivity != null)
  348. {
  349. OnMouseActivity -= handler;
  350. }
  351. }
  352. }
  353. }