首页 > 学院 > 开发设计 > 正文

C#钩子类 几乎捕获键盘鼠标所有事件

2019-11-17 02:49:25
字体:
来源:转载
供稿:网友
C#钩子类 几乎捕获键盘鼠标所有事件
  1. usingSystem;
  2. usingSystem.Text;
  3. usingSystem.Runtime.InteropServices;
  4. usingSystem.Reflection;
  5. usingSystem.Windows.Forms;
  6. namespaceMouseKeyboardLibrary
  7. {
  8. ///<summary>
  9. ///AbstractbaseclassforMouseandKeyboardhooks
  10. ///</summary>
  11. publicabstractclassGlobalHook
  12. {
  13. #regionWindowsAPICode
  14. [StructLayout(LayoutKind.Sequential)]
  15. PRotectedclassPOINT
  16. {
  17. publicintx;
  18. publicinty;
  19. }
  20. [StructLayout(LayoutKind.Sequential)]
  21. protectedclassMouseHookStruct
  22. {
  23. publicPOINTpt;
  24. publicinthwnd;
  25. publicintwHitTestCode;
  26. publicintdwExtraInfo;
  27. }
  28. [StructLayout(LayoutKind.Sequential)]
  29. protectedclassMouseLLHookStruct
  30. {
  31. publicPOINTpt;
  32. publicintmouseData;
  33. publicintflags;
  34. publicinttime;
  35. publicintdwExtraInfo;
  36. }
  37. [StructLayout(LayoutKind.Sequential)]
  38. protectedclassKeyboardHookStruct
  39. {
  40. publicintvkCode;
  41. publicintscanCode;
  42. publicintflags;
  43. publicinttime;
  44. publicintdwExtraInfo;
  45. }
  46. [DllImport("user32.dll",CharSet=CharSet.Auto,
  47. CallingConvention=CallingConvention.StdCall,SetLastError=true)]
  48. protectedstaticexternintSetWindowsHookEx(
  49. intidHook,
  50. HookProclpfn,
  51. IntPtrhMod,
  52. intdwThreadId);
  53. [DllImport("user32.dll",CharSet=CharSet.Auto,
  54. CallingConvention=CallingConvention.StdCall,SetLastError=true)]
  55. protectedstaticexternintUnhookWindowsHookEx(intidHook);
  56. [DllImport("user32.dll",CharSet=CharSet.Auto,
  57. CallingConvention=CallingConvention.StdCall)]
  58. protectedstaticexternintCallNextHookEx(
  59. intidHook,
  60. intnCode,
  61. intwParam,
  62. IntPtrlParam);
  63. [DllImport("user32")]
  64. protectedstaticexternintToAscii(
  65. intuVirtKey,
  66. intuScanCode,
  67. byte[]lpbKeyState,
  68. byte[]lpwTransKey,
  69. intfuState);
  70. [DllImport("user32")]
  71. protectedstaticexternintGetKeyboardState(byte[]pbKeyState);
  72. [DllImport("user32.dll",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
  73. protectedstaticexternshortGetKeyState(intvKey);
  74. protecteddelegateintHookProc(intnCode,intwParam,IntPtrlParam);
  75. protectedconstintWH_MOUSE_LL=14;
  76. protectedconstintWH_KEYBOARD_LL=13;
  77. protectedconstintWH_MOUSE=7;
  78. protectedconstintWH_KEYBOARD=2;
  79. protectedconstintWM_MOUSEMOVE=0x200;
  80. protectedconstintWM_LBUTTONDOWN=0x201;
  81. protectedconstintWM_RBUTTONDOWN=0x204;
  82. protectedconstintWM_MBUTTONDOWN=0x207;
  83. protectedconstintWM_LBUTTONUP=0x202;
  84. protectedconstintWM_RBUTTONUP=0x205;
  85. protectedconstintWM_MBUTTONUP=0x208;
  86. protectedconstintWM_LBUTTONDBLCLK=0x203;
  87. protectedconstintWM_RBUTTONDBLCLK=0x206;
  88. protectedconstintWM_MBUTTONDBLCLK=0x209;
  89. protectedconstintWM_MOUSEWHEEL=0x020A;
  90. protectedconstintWM_KEYDOWN=0x100;
  91. protectedconstintWM_KEYUP=0x101;
  92. protectedconstintWM_SYSKEYDOWN=0x104;
  93. protectedconstintWM_SYSKEYUP=0x105;
  94. protectedconstbyteVK_SHIFT=0x10;
  95. protectedconstbyteVK_CAPITAL=0x14;
  96. protectedconstbyteVK_NUMLOCK=0x90;
  97. protectedconstbyteVK_LSHIFT=0xA0;
  98. protectedconstbyteVK_RSHIFT=0xA1;
  99. protectedconstbyteVK_LCONTROL=0xA2;
  100. protectedconstbyteVK_RCONTROL=0x3;
  101. protectedconstbyteVK_LALT=0xA4;
  102. protectedconstbyteVK_RALT=0xA5;
  103. protectedconstbyteLLKHF_ALTDOWN=0x20;
  104. #endregion
  105. #regionPrivateVariables
  106. protectedint_hookType;
  107. protectedint_handleToHook;
  108. protectedbool_isStarted;
  109. protectedHookProc_hookCallback;
  110. #endregion
  111. #regionProperties
  112. publicboolIsStarted
  113. {
  114. get
  115. {
  116. return_isStarted;
  117. }
  118. }
  119. #endregion
  120. #regionConstructor
  121. publicGlobalHook()
  122. {
  123. application.ApplicationExit+=newEventHandler(Application_ApplicationExit);
  124. }
  125. #endregion
  126. #regionMethods
  127. publicvoidStart()
  128. {
  129. if(!_isStarted&&
  130. _hookType!=0)
  131. {
  132. //Makesurewekeepareferencetothisdelegate!
  133. //Ifnot,GCrandomlycollectsit,andaNullReferenceexceptionisthrown
  134. _hookCallback=newHookProc(HookCallbackProcedure);
  135. _handleToHook=SetWindowsHookEx(
  136. _hookType,
  137. _hookCallback,
  138. Marshal.GetHINSTANCE(Assembly.GetExecutingA
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表