C++鼠标键盘操作自动化

C++鼠标键盘操作自动化

cpp 复制代码
#pragma once
#include <Windows.h> 
enum KEYS{
	A = 65,
	W=87,
	S=83,
	D=68,
	SHIFT=VK_LSHIFT,
	ALT=18,
	Tilde= 126,//~
	TAB=VK_TAB,
	B=66,
	SPACE=VK_SPACE,
	ESC=VK_ESCAPE,
	Q=81
};
enum MOUSE
{
	ML,MW,MR//左,中,右
};
class simulator//模拟器
{
public:
	simulator();
	simulator(const char* exepath);//启动应用程序
	static void Key_Down(KEYS key);//模拟键盘按键按下
	static void Key_UP(KEYS key);//模拟键盘按键抬起
	static void Key_Click(KEYS key, long mintime);//模拟按键瞬间按下一段时间后抬起
	static void Mouse_Down(MOUSE key);//模拟鼠标按下
	static void Mouse_UP(MOUSE key);//模拟鼠标抬起
	static void Mouse_Click(MOUSE key);//模拟鼠标点击
	static void Mouse_Move(int x, int y);//模拟鼠标移动x,y数值的距离
	static void Mouse_Wheel(int value);//模拟鼠标滚轮滚动

	static POINT Get_Mouse_Position();//得到鼠标位置
	static void Set_Mouse_Position(int x,int y);//设置鼠标位置
	static void Get_Mouse_Position(POINT & pos);//得到鼠标位置
	static void unload_devices();//卸载鼠标键盘,使其不可使用
	static void reload_devices();//重新启用鼠标键盘
};
class Debuger
{
public:
	static void print_pos(POINT &pos);
	static void test_mouse();
	static void show_all();
};
cpp 复制代码
#include"simulator.h"
#include<stdlib.h>
#include <shlobj.h>
#include<SetupAPI.h>
#include <devguid.h>

#pragma comment (lib, "setupapi.lib")
#pragma warning(disable:4996)
extern "C"
{
#include<stdio.h>
}
simulator::simulator()
{
}

simulator::simulator(const char * exepath)
{
	ShellExecute(0, "open", exepath, "", "", 1);
}

void simulator::Key_Down(KEYS key)
{
	keybd_event(key, 0, 0, 0);
}

void simulator::Key_UP(KEYS key)
{
	keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
}

void simulator::Key_Click(KEYS key, long mintime)
{
	Key_Down(key);
	if (mintime > 0)
	{
		Sleep(mintime);
	}
	Key_UP(key);
}

void simulator::Mouse_Down(MOUSE key)
{
	switch (key)
	{
	case ML:
	{
		mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
	}
	break;
	case MW:
	{
		mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
	}
	break;
	case MR:
	{
		mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
	}
	break;
	}
}

void simulator::Mouse_UP(MOUSE key)
{
	switch (key)
	{
	case ML:
	{
		mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
	}
	break;
	case MW:
	{
		mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
	}
	break;
	case MR:
	{
		mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
	}
	break;
	}
}

void simulator::Mouse_Click(MOUSE key)
{
	switch (key)
	{
	case ML:
	{
		mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
	}
	break;
	case MW:
	{
		mouse_event(MOUSEEVENTF_MIDDLEDOWN|MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
	}
	break;
	case MR:
	{
		mouse_event(MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
	}
	break;
	}
}

void simulator::Mouse_Move(int x, int y)
{
	mouse_event( MOUSEEVENTF_MOVE, x, y, 0, 0);
}

void simulator::Mouse_Wheel(int value)
{
	mouse_event(MOUSEEVENTF_WHEEL, 0, 0, value, 0);
}

POINT simulator::Get_Mouse_Position()
{
	POINT out;
	GetCursorPos(&out);
	return out;
}

void simulator::Set_Mouse_Position(int x, int y)
{
	SetCursorPos(x, y);
}

void simulator::Get_Mouse_Position(POINT & pos)
{
	GetCursorPos(&pos);
}

void simulator::unload_devices()
{
}

void simulator::reload_devices()
{
}

void Debuger::print_pos(POINT & pos)
{
	printf("x:%d  y:%d", pos.x, pos.y);
}

void Debuger::test_mouse()
{
	POINT pt;
	while (1)
	{
		simulator::Get_Mouse_Position(pt);
		Debuger::print_pos(pt);
		Sleep(1000);
	}
}

void SetDeviceStatus(bool bStatusFlag)
{
	SP_DEVINFO_DATA DeviceInfoData;
	DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	//GUID guid;
	HDEVINFO hDevInfo;
	// 得到设备 HDEVINFO      
	hDevInfo = SetupDiGetClassDevs(&(GUID_DEVCLASS_KEYBOARD), 0, 0, DIGCF_PRESENT );
	if (hDevInfo == INVALID_HANDLE_VALUE)
	{
		return;
	}
	int index = 0;
	while (SetupDiEnumDeviceInfo(hDevInfo, index++, &DeviceInfoData))
	{
		TCHAR szDescBuf[MAX_PATH] = { 0 };
		//获取设备描述信息
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
		{
			///*continue*/;
			return;
		}

		puts(szDescBuf);

		SP_PROPCHANGE_PARAMS propChange;
		propChange.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
		propChange.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
		propChange.Scope = DICS_FLAG_GLOBAL;
		propChange.StateChange = bStatusFlag ? DICS_START : DICS_STOP;
		propChange.HwProfile = 0;
		if (SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER*)&propChange, sizeof(propChange)))
		{
			puts("succ");
			SetupDiChangeState(hDevInfo, &DeviceInfoData);
		}
		printf("%d\n", GetLastError());
	}
	//初始化属性
	
	SetupDiDestroyDeviceInfoList(hDevInfo);
}
void Debuger::show_all()
{
	/*SetDeviceStatus(false);
	Sleep(6000);
	SetDeviceStatus(true);*/
	SP_DEVINFO_DATA DeviceInfoData;
	DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
	GUID guid;
	HDEVINFO hDevInfo;
	// 得到设备 HDEVINFO      
	hDevInfo = SetupDiGetClassDevs(&(GUID_DEVCLASS_KEYBOARD), 0, 0, DIGCF_PRESENT );
	for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)
	{
		TCHAR szClassBuf[MAX_PATH] = { 0 };
		TCHAR szDescBuf[MAX_PATH] = { 0 };
		TCHAR szDriver[MAX_PATH] = { 0 };
		TCHAR szFriName[MAX_PATH] = { 0 };

		// 获取类名  
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS, NULL, (PBYTE)szClassBuf, MAX_PATH - 1, NULL))
		{
			///*continue*/;
			return;
		}

		puts(szClassBuf);

		//获取设备描述信息
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
		{
			///*continue*/;
			return;
		}

		puts(szDescBuf);

		//获取设备驱动名
		if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER, NULL, (PBYTE)szDriver, MAX_PATH - 1, NULL))
		{
			///*continue*/;
			return;
		}

		puts(szDriver);
		
		printf("%x %x %x ", DeviceInfoData.ClassGuid.Data1, DeviceInfoData.ClassGuid.Data2, DeviceInfoData.ClassGuid.Data3);
		for (int i = 0; i < 8; i++)
			printf("%x ", DeviceInfoData.ClassGuid.Data4[i]);
	}
}
相关推荐
Duck Bro几秒前
数据结构:顺序表(动态顺序表)
c语言·数据结构·c++·学习·算法
linhhanpy8 分钟前
自制操作系统(九、操作系统完整实现)
c语言·开发语言·汇编·c++·操作系统·自制操作系统
ACALJJ328 分钟前
STL整理
开发语言·c++
矛取矛求1 小时前
string接口的深度理解(内附思维导图)
c语言·开发语言·c++·接口·string
点云侠1 小时前
二维椭圆拟合算法及推导过程
开发语言·c++·算法·计算机视觉·matlab
想不到好名字了()2 小时前
负载均衡式在线oj项目开发文档2(个人项目)
linux·网络·c++
欢天喜地小姐姐2 小时前
Ubuntu16.04安装并配置Visual Studio调试C++
c++·visual studio
五味香2 小时前
Linux命令学习,git命令
linux·c语言·开发语言·c++·git·学习·算法
努力学算法的蒟蒻2 小时前
牛客小白月赛104 —— C.小红打怪
c++·算法
七夕先生3 小时前
Qt:QPdfDocument渲染PDF文件时的信息丢失问题
c++·qt·pdf