/* Name: 模拟鼠标点击事件 Copyright: Author: yifi Date: 23/03/17 14:31 Description: mouse_event(MOUSEEVENTF_XDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_XUP,0,0,0,0); */ #include<iostream> #include<conio.h> #include<windows.h> #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) //必要的,我是背下来的 using namespace std; void inputKey(); int main(){ char a; int now=0; printf("按Q开始左键点击 "); printf("按W开始右键点击 "); printf("按空格停止点击 "); while(1){ if(KEY_DOWN(VK_SPACE)){//VK_SPACE 是空格的虚拟键值 now=0; Sleep(100);//你的手不会再一瞬间送开,所以要处理一下 } if(KEY_DOWN('Q')){ now=1; Sleep(100); } if(KEY_DOWN('W')){ now=2; Sleep(100); } if(now==1){//模拟输入'A' keybd_event(65,MapVirtualKey(0x41,0),0,0); //输入'A' } if(now==2){//模拟输入'A' inputKey(); // :: SendMessage(NULL,WM_CHAR,WPARAM('a'),0); // mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0); // Sleep(10); // mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0); } Sleep(20);//点击间隔 单位是毫秒 } } void inputKey() { //::SetForegroundWindow(hWnd);//把接收窗口放在最前,同时使窗口获取焦点(好吧,说的像Android,获取焦点。。。) INPUT keyEvent = {0}; keyEvent.type = INPUT_KEYBOARD;//如果是鼠标设置鼠标的INPUT_MOUSE keyEvent.ki.wVk = 0x41;//字符A keyEvent.ki.wScan = MapVirtualKey(0x41, 0);//又像前面别人说的,可能一般不需要设置吧,我觉得还是这样靠谱, //之前我没映射就没成功,不信你试试,you can you up!! :: SendInput(1, &keyEvent, sizeof(keyEvent)); Sleep(1000); //别为为啥,就想让他睡会 keyEvent.ki.dwFlags = KEYEVENTF_KEYUP;//默认keyEvent.ki.dwFlags = 0 ,即 KEYEVENTF_KEYDOWN ::SendInput(1, &keyEvent, sizeof(keyEvent)); }