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);
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);
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))
{
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()
{
SP_DEVINFO_DATA DeviceInfoData;
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
GUID guid;
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))
{
return;
}
puts(szClassBuf);
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))
{
return;
}
puts(szDescBuf);
if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER, NULL, (PBYTE)szDriver, MAX_PATH - 1, NULL))
{
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]);
}
}