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]);
	}
}
相关推荐
脉牛杂德39 分钟前
多项式加法——C语言
数据结构·c++·算法
legend_jz41 分钟前
STL--哈希
c++·算法·哈希算法
CSUC43 分钟前
【C++】父类参数有默认值时子类构造函数列表中可以省略该参数
c++
Vanranrr1 小时前
C++ QT
java·c++·qt
鸿儒5171 小时前
C++ lambda 匿名函数
开发语言·c++
van叶~2 小时前
算法妙妙屋-------1.递归的深邃回响:二叉树的奇妙剪枝
c++·算法
knighthood20012 小时前
解决:ros进行gazebo仿真,rviz没有显示传感器数据
c++·ubuntu·ros
flashman9113 小时前
python在word中插入图片
python·microsoft·自动化·word
半盏茶香3 小时前
【C语言】分支和循环详解(下)猜数字游戏
c语言·开发语言·c++·算法·游戏
小堇不是码农3 小时前
在VScode中配置C_C++环境
c语言·c++·vscode