C# 鼠标连击源码 转
private void timer1_Tick(object sender, EventArgs e) { if (GetAsyncKeyState(0x01) > 0) { keydown(); } //按下的时候开启 /* 开启时钟 */ //松开的时候关闭 /* 关闭时钟 */ }
[DllImport("kernel32.dll")] static extern uint GetTickCount(); /// <summary> /// 程序等待延迟执行 /// </summary> /// <param name="ms"></param> static void MySleep(uint ms) { uint start = GetTickCount(); //获取系统运行了多长时间 , 返回值,毫秒 while (GetTickCount() - start < ms) //当前运行时间 和之前运行时间做比较 { //Application.DoEvents(); } }
//键盘按下的时候执行 public void keydown() { //测试结果 是 当GetAsyncKeyState(0x01) > 0 的时候,是鼠标左键按下. //mouse_event()按下的时候;会更改当GetAsyncKeyState(0x01)的状态; //鼠标按下时候;也会更改当GetAsyncKeyState(0x01)的状态 //按下松开时候,GetKeyState(0x01)返回为1或0交替; try { //左键抬起 MySleep(10); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); MySleep(10); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); MySleep(10); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button2_Click(object sender, EventArgs e) { timer1.Interval = 10; timer1.Enabled = true; }
const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下 const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起 const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起 const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;// 模拟鼠标中键按下 const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起 [DllImport("user32.dll")] public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); [DllImport("user32.dll")] public static extern int GetAsyncKeyState(int vKey); [DllImport("user32.dll")] public static extern int GetKeyState(int vKey); [DllImport("user32.dll")] public static extern int GetKeyboardState(byte[] pbKeyState); /* * 调用 byte[] bs = new byte[256]; GetKeyboardState(bs); //数组中存的是键盘的按键状态 */
转自: 前辈们.....
附: VK_LBUTTON 鼠标左键 0x01 VK_RBUTTON 鼠标右键 0x02 VK_CANCEL Ctrl + Break 0x03 VK_MBUTTON 鼠标中键 0x04 VK_BACK Backspace 键 0x08 VK_TAB Tab 键 0x09 VK_RETURN 回车键 0x0D VK_SHIFT Shift 键 0x10 VK_CONTROL Ctrl 键 0x11 VK_MENU Alt 键 0x12 VK_pause Pause 键 0x13 VK_CAPITAL Caps Lock 键 0x14 VK_ESCAPE Esc 键 0x1B VK_SPACE 空格键 0x20 VK_PRIOR Page Up 键 0x21 VK_NEXT Page Down 键 0x22 VK_END End 键 0x23 VK_HOME Home 键 0x24 VK_LEFT 左箭头键 0x25 VK_UP 上箭头键 0x26 VK_RIGHT 右箭头键 0x27 VK_DOWN 下箭头键 0x28 VK_SNAPSHOT print screen 键 0x2C VK_Insert Insert 键 0x2D VK_Delete Delete 键 0x2E '0' – '9' 数字 0 - 9 0x30 - 0x39 'A' – 'Z' 字母 A - Z 0x41 - 0x5A VK_LWIN 左WinKey(104键盘才有) 0x5B VK_RWIN 右WinKey(104键盘才有) 0x5C VK_APPS AppsKey(104键盘才有) 0x5D VK_NUMPAD0 小键盘 0 键 0x60 VK_NUMPAD1 小键盘 1 键 0x61 VK_NUMPAD2 小键盘 2 键 0x62 VK_NUMPAD3 小键盘 3 键 0x63 VK_NUMPAD4 小键盘 4 键 0x64 VK_NUMPAD5 小键盘 5 键 0x65 VK_NUMPAD6 小键盘 6 键 0x66 VK_NUMPAD7 小键盘 7 键 0x67 VK_NUMPAD8 小键盘 8 键 0x68 VK_NUMPAD9 小键盘 9 键 0x69 VK_F1 - VK_F24 功能键F1 – F24 0x70 - 0x87 VK_NUMLOCK Num Lock 键 0x90 VK_SCROLL Scroll Lock 键 0x91
//钩子代码
private MouseButtons GetButton(int wParam) { switch (wParam) { case 513: case 514: case 515: return MouseButtons.Left; case 516: case 517: case 518: return MouseButtons.Right; case 519: case 520: case 521: return MouseButtons.Middle; default: return MouseButtons.None; } } private MouseEventType GetEventType(int wParam) { switch (wParam) { case 513: case 516: case 519: return MouseEventType.MouseDown; case 514: case 517: case 520: return MouseEventType.MouseUp; case 515: case 518: case 521: return MouseEventType.DoubleClick; case 522: return MouseEventType.MouseWheel; case 512: return MouseEventType.MouseMove; default: return MouseEventType.None; } }
关于 0x201 参数的宏定义 所在头文件 是winuser.h
#define WM_MOUSEFIRST 0x0200 #define WM_MOUSEMOVE 0x0200 #define WM_LBUTTONDOWN 0x0201 #define WM_LBUTTONUP 0x0202 #define WM_LBUTTONDBLCLK 0x0203 #define WM_RBUTTONDOWN 0x0204 #define WM_RBUTTONUP 0x0205 #define WM_RBUTTONDBLCLK 0x0206 #define WM_MBUTTONDOWN 0x0207 #define WM_MBUTTONUP 0x0208 #define WM_MBUTTONDBLCLK 0x0209 #define WM_MOUSELAST 0x0209