简体   繁体   中英

SendInput() function doesn't work inside game (C++)

I wrote an AI in C++ to try to play a game (Puyo Puyo Tetris), and to do this, I try to use SendInput() to simulate keyboard inputs to control the game, but for some reason, the inputs don't do anything. I know that the inputs themselves work because they work outside in notepad and browsers, etc. I suspect that that this may be part of the game's anticheat stopping the SendInput() function from going through inside the game.

Here's my code:

INPUT input = {0};

input.type = INPUT_KEYBOARD;

input.ki.wVk = VkKeyScanA('a');

SendInput(1, &input, sizeof(input));

Sleep(100);

ZeroMemory(&input, sizeof(input));

input.ki.dwFlags = KEYEVENTF_KEYUP;

Sleep(5);

Does anyone know how to get past this issue? Is there an alternative to SendInput() I can use? Thank you!

The sample you used SendInput is incorrect.

First, you only sent the keydown input once, and then when you clear the input( ZeroMemory ), you also clear the type and ki.wVk . And you didn't even call SendInput for KEYEVENTF_KEYUP .

You only need to put keydown/up in one SendInput :

INPUT input[2] = { 0 };

input[0].type = input[1].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[1].ki.wVk = VkKeyScanA('a');

input[1].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(2, input, sizeof(INPUT));

In addition, This function is subject to UIPI. Applications are permitted to inject input only into applications that are at an equal or lesser integrity level. For this case, You'll need to set UIAccess in the Application Manifest File .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM