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]);
	}
}
相关推荐
工业3D_大熊5 分钟前
3D可视化引擎HOOPS Luminate场景图详解:形状的创建、销毁与管理
java·c++·3d·docker·c#·制造·数据可视化
暮色_年华19 分钟前
Modern Effective C++ Item 11:优先考虑使用deleted函数而非使用未定义的私有声明
c++
流星白龙21 分钟前
【C++习题】10.反转字符串中的单词 lll
开发语言·c++
Smile丶凉轩44 分钟前
微服务即时通讯系统的实现(服务端)----(1)
c++·git·微服务·github
萝卜兽编程1 小时前
优先级队列
c++·算法
努力的悟空2 小时前
国土变更调查拓扑错误自动化修复工具的研究
运维·自动化
珹洺2 小时前
C语言数据结构——详细讲解 双链表
c语言·开发语言·网络·数据结构·c++·算法·leetcode
孙同学要努力3 小时前
C++知识整理day1——前置基础知识整理(命名空间、输入输出、函数重载、引用)
开发语言·c++
沐泽Mu3 小时前
嵌入式学习-C嘎嘎-Day05
开发语言·c++·学习
计算机毕设孵化场3 小时前
计算机毕设-基于springboot的高校网上缴费综合务系统视频的设计与实现(附源码+lw+ppt+开题报告)
java·spring boot·计算机外设·音视频·课程设计·高校网上缴费综合务系统视频·计算机毕设ppt