游戏中模拟键盘输入,有时回被系统屏蔽,Java等语言都试过很多方法,好像都没用,所以下面给出一种C++实现方法
#include <iostream>
#include <windows.h>
// for key pushing
BYTE scan_code(DWORD pKey)
{
const DWORD result = MapVirtualKey(pKey, MAPVK_VK_TO_VSC);
return static_cast<BYTE>(result);
}
void press_key(DWORD pKey)
{
keybd_event(static_cast<BYTE>(pKey), scan_code(pKey), 0, 0);
}
void release_key(DWORD pKey)
{
keybd_event(static_cast<BYTE>(pKey), scan_code(pKey), KEYEVENTF_KEYUP, 0);
}
// for testing
#define PRESS(x) press_key(x); std::cout << "Press: " #x << std::endl
#define RELEASE(x) release_key(x); std::cout << "Release: " #x << std::endl
// test
int main(void)
{
Sleep(3000);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
Sleep(10);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
int len = 10;
int i=0;
for (i=0;i<len;i++)
{
PRESS(65);
Sleep(100);
}
}