《C#Hook键盘鼠标连按连点器》由会员分享,可在线阅读,更多相关《C#Hook键盘鼠标连按连点器(26页珍藏版)》请在金锄头文库上搜索。
1、Hook_键盘鼠标连按连点by Meteor 当初是为了我玩的游戏做的一个自动采集功能,功能是实现鼠标和键盘自动操作根据动作,设置的时间隔保存后自动运行直接上图Form1 主窗体Loading 只是一个等待提示效果UserActivityHook 类代码using System;using System.Runtime.InteropServices;using System.Reflection;using System.Windows.Forms;namespace Patcher_采集 public class UserActivityHook : object public UserA
2、ctivityHook() tryStart();catch UserActivityHook() Stop(); public event MouseEventHandler OnMouseActivity;public event KeyEventHandler KeyDown;public event KeyPressEventHandler KeyPress;public event KeyEventHandler KeyUp;public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);static int
3、hMouseHook = 0; static int hKeyboardHook = 0;public const int WH_MOUSE_LL = 14;public const int WH_KEYBOARD_LL = 13;HookProc MouseHookProcedure; HookProc KeyboardHookProcedure; StructLayout(LayoutKind.Sequential)public class POINT public int x;public int y;StructLayout(LayoutKind.Sequential)public c
4、lass MouseHookStruct public POINT pt;public int hwnd;public int wHitTestCode;public int dwExtraInfo;StructLayout(LayoutKind.Sequential)public class KeyboardHookStructpublic int vkCode;public int scanCode;public int flags; public int time; public int dwExtraInfo; DllImport(user32.dll,CharSet=CharSet.
5、Auto,CallingConvention=CallingConvention.StdCall)public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);DllImport(user32.dll,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)public static extern bool UnhookWindowsHookEx(int idHook);DllImp
6、ort(user32.dll,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); public void Start()if(hMouseHook = 0)MouseHookProcedure = new HookProc(MouseHookProc);hMouseHook = SetWindowsHookEx( WH_MOUSE_L
7、L,MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()0),0);if(hMouseHook = 0 ) Stop();throw new Exception(SetWindowsHookEx failed.);if(hKeyboardHook = 0)KeyboardHookProcedure = new HookProc(KeyboardHookProc);hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,KeyboardH
8、ookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()0),0);if(hKeyboardHook = 0 ) Stop();throw new Exception(SetWindowsHookEx ist failed.);public void Stop()bool retMouse =true;bool retKeyboard = true;if(hMouseHook != 0)retMouse = UnhookWindowsHookEx(hMouseHook);hMouseHook
9、= 0; if(hKeyboardHook != 0)retKeyboard = UnhookWindowsHookEx(hKeyboardHook);hKeyboardHook = 0; if (!(retMouse & retKeyboard) throw new Exception(UnhookWindowsHookEx failed.);private const int WM_MOUSEMOVE = 0x200;private const int WM_LBUTTONDOWN = 0x201;private const int WM_RBUTTONDOWN = 0x204;priva
10、te const int WM_MBUTTONDOWN = 0x207;private const int WM_LBUTTONUP = 0x202;private const int WM_RBUTTONUP = 0x205;private const int WM_MBUTTONUP = 0x208; private const int WM_LBUTTONDBLCLK = 0x203;private const int WM_RBUTTONDBLCLK = 0x206;private const int WM_MBUTTONDBLCLK = 0x209;private int Mouse
11、HookProc(int nCode, Int32 wParam, IntPtr lParam)if (nCode = 0) & (OnMouseActivity!=null) MouseButtons button=MouseButtons.None;switch (wParam)case WM_LBUTTONDOWN: button=MouseButtons.Left; break;case WM_RBUTTONDOWN: button=MouseButtons.Right; break;int clickCount=0;if (button!=MouseButtons.None)if (
12、wParam=WM_LBUTTONDBLCLK | wParam=WM_RBUTTONDBLCLK) clickCount=2;else clickCount=1;MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct);MouseEventArgs e=new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0 );O
13、nMouseActivity(this, e);return CallNextHookEx(hMouseHook, nCode, wParam, lParam); DllImport(user32) public static extern int ToAscii(int uVirtKey, int uScanCode,byte lpbKeyState,byte lpwTransKey,int fuState); DllImport(user32) public static extern int GetKeyboardState(byte pbKeyState);private const
14、int WM_KEYDOWN = 0x100;private const int WM_KEYUP = 0x101;private const int WM_SYSKEYDOWN = 0x104;private const int WM_SYSKEYUP = 0x105;private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)if (nCode = 0) & (KeyDown!=null | KeyUp!=null | KeyPress!=null)KeyboardHookStruct MyKeyboardHook
15、Struct = (KeyboardHookStruct) Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct);if ( KeyDown!=null & ( wParam =WM_KEYDOWN | wParam=WM_SYSKEYDOWN )Keys keyData=(Keys)MyKeyboardHookStruct.vkCode;KeyEventArgs e = new KeyEventArgs(keyData);KeyDown(this, e);if ( KeyPress!=null & wParam =WM_KEYDOW
16、N )byte keyState = new byte256;GetKeyboardState(keyState);byte inBuffer= new byte2;if (ToAscii(MyKeyboardHookStruct.vkCode,MyKeyboardHookStruct.scanCode,keyState,inBuffer,MyKeyboardHookStruct.flags)=1) KeyPressEventArgs e = new KeyPressEventArgs(char)inBuffer0);KeyPress(this, e);if ( KeyUp!=null & ( wParam =WM_KEYUP