1 Star 0 Fork 0

GPRO / 游戏按键模拟

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.cpp 3.30 KB
一键复制 编辑 原始数据 按行查看 历史
gpro131 提交于 2023-04-11 14:43 . update game simulate
// GameOperationSimulator.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
#include <thread>
#include <windows.h>
using namespace std;
bool isAltDown = false;
bool isGameSimulateThreadRunning = false;
enum EKeyState
{
KeyDown,
KeyUp
};
struct KeyActionFrame
{
int time;
int key;
EKeyState keyState;
void ParseKeyCode(int keyCode,int flags)
{
if(flags == 128)
{
keyState = KeyDown;
}else{
keyState = KeyDown;
}
if(keyCode >= 'a' && key <= 'z')
{
key = keyCode;
}
if(keyCode >= 'A' && key <= 'Z')
{
key = keyCode;
}
}
};
vector<KeyActionFrame> recordFrames;
bool isGameKeyRecordRunning = false;
void StartGameSimulateThread()
{
isGameSimulateThreadRunning = true;
while (isGameSimulateThreadRunning) {
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
::Sleep(5);
keybd_event('R', 0, 0, 0);
::Sleep(26);
keybd_event('R', 0, KEYEVENTF_KEYUP, 0);
::Sleep(15);
//cout << ".";
}
}
LRESULT CALLBACK LowLevelKeyboardProc(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lPARAM) {
KBDLLHOOKSTRUCT* ks = (KBDLLHOOKSTRUCT*)lPARAM;
cout << ks->vkCode << endl;
if (ks->vkCode == VK_LMENU)
{
if (ks->flags == 128)
{
isAltDown = false;
}
else
//if (ks->flags == 0)
{
isAltDown = true;
}
cout << "ks flags:" << ks->flags << endl;
return CallNextHookEx(NULL, nCode, wParam, lPARAM);
}
if (ks->vkCode == VK_F5)
{
if (ks->flags == 128)
{
}
else
//if (ks->flags == 0)
{
isGameKeyRecordRunning = !isGameKeyRecordRunning;
}
return;
}
if(isGameKeyRecordRunning)
{
KeyActionFrame frame;
frame.ParseKeyCode(ks->vkCode,ks->flags);
recordFrames.push_back(frame);
}
if (ks->flags == 128 || ks->flags == 129)
{
switch (ks->vkCode)
{
case 0x30:
case 0x60:
cout << "检测到按键0" << endl;
break;
case 0x31:
case 0x61:
cout << "检测到按键1" << endl;
default:
break;
}
if (ks->vkCode == VK_ESCAPE)
{
isGameSimulateThreadRunning = false;
exit(0);
}
if (isAltDown && ks->vkCode == VK_SPACE)
{
//cout <<"ks flags:"<< ks->flags << endl;
if (isGameSimulateThreadRunning)
{
isGameSimulateThreadRunning = false;
cout << "模拟游戏结束" << endl;
}
else {
std::thread th(StartGameSimulateThread);
th.detach();
cout << "模拟游戏开始" << endl;
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lPARAM);
}
int main()
{
//::SetCursorPos(10, 10);
::Sleep(3000);
/*std::thread th(StartGameSimulateThread);
th.detach();*/
HHOOK keyboard = SetWindowsHookEx(
WH_KEYBOARD_LL,
LowLevelKeyboardProc,
GetModuleHandleA(NULL), NULL
);
if (keyboard == 0)
{
cout << "hook load failed" << endl;
return 0;
}
MSG msg;
while (true)
{
if (PeekMessageA(&msg, NULL, NULL, NULL,
PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
else
Sleep(0);
/*if(GetAsyncKeyState(13) &0x8000 )
break;*/
}
UnhookWindowsHookEx(keyboard);
return 0;
}
C++
1
https://gitee.com/GPRO/game-button-simulation.git
git@gitee.com:GPRO/game-button-simulation.git
GPRO
game-button-simulation
游戏按键模拟
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891