简体   繁体   English

SendInput() function 在游戏中不起作用(C++)

[英]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.我在 C++ 中写了一个 AI 来尝试玩游戏(Puyo Puyo Tetris),为此,我尝试使用 SendInput() 来模拟键盘输入来控制游戏,但由于某种原因,输入不起作用任何事物。 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.我知道输入本身可以工作,因为它们可以在记事本和浏览器等外部工作。我怀疑这可能是游戏阻止 SendInput() function 进入游戏内部的一部分。

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?我可以使用 SendInput() 的替代方法吗? Thank you!谢谢!

The sample you used SendInput is incorrect.您使用SendInput的示例不正确。

First, you only sent the keydown input once, and then when you clear the input( ZeroMemory ), you also clear the type and ki.wVk .首先,您只发送了一次 keydown 输入,然后当您清除输入( ZeroMemory )时,您还清除了typeki.wVk And you didn't even call SendInput for KEYEVENTF_KEYUP .而且您甚至没有为KEYEVENTF_KEYUP调用SendInput

You only need to put keydown/up in one SendInput :您只需要将 keydown/up 放在一个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.此外,此 function 受 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 .对于这种情况,您需要在 Application Manifest File 中设置 UIAccess

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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