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

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

#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();
};

#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]);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值