gpt4 book ai didi

c++ - 输入/vector 数组不适用于 SendInput

转载 作者:行者123 更新时间:2023-11-30 02:21:02 29 4
gpt4 key购买 nike

所以我尝试使用 SendInput 来同时发送多个按键按下/按键释放。数组和 vector 具有正确的信息,但由于某种原因,它只是没有按下任何东西。为什么?

我已经尝试从数组和 vector 中选取一个元素并使用 SendInput 按下/释放键,它工作正常。

#include <iostream>
#include <Windows.h>
#include <vector>

class movement
{
public:
std::vector<INPUT> inputs;

void Key(std::vector<INPUT> INPUTkeys)
{
INPUT * inputs = new INPUT[INPUTkeys.size()];

for (int i = 0; i < INPUTkeys.size(); i++)
inputs[i] = INPUTkeys.at(i);

SendInput(INPUTkeys.size(), inputs, sizeof(inputs));

delete[] inputs;
}

void prepareInput(INPUT &input, int key, bool down)
{
input.type = INPUT_KEYBOARD;
input.ki.dwExtraInfo = NULL;
input.ki.time = NULL;
input.ki.wScan = NULL;
input.ki.wVk = key;

if (down)
input.ki.dwFlags = 0;
else
input.ki.dwFlags = KEYEVENTF_KEYUP;
}
};

movement oMovement;

void main()
{
while (!GetAsyncKeyState(VK_SPACE))
Sleep(1);

std::vector<INPUT> inputs;

INPUT input;

oMovement.prepareInput(input, 'U', true);
inputs.push_back(input);
oMovement.prepareInput(input, 'S', true);
inputs.push_back(input);
oMovement.prepareInput(input, 'X', true);
inputs.push_back(input);

oMovement.Key(inputs);
inputs.clear();

oMovement.prepareInput(input, 'U', false);
inputs.push_back(input);
oMovement.prepareInput(input, 'S', false);
inputs.push_back(input);
oMovement.prepareInput(input, 'X', false);
inputs.push_back(input);

oMovement.Key(inputs);
inputs.clear();

system("pause");
}

最佳答案

您将指针的大小作为第三个参数传递给 SendInput,而它应该是 INPUT 结构的大小。您还应该检查结果(至少在 Debug模式下):

void Key(std::vector<INPUT> & INPUTkeys) // pass by reference to prevent copying
{
::UINT const sent_events_count
{
SendInput
(
static_cast<::UINT>(INPUTkeys.size())
, INPUTkeys.data()
, sizeof ::INTPUT
)
};
assert(INPUTkeys.size() == sent_events_count);
}

关于c++ - 输入/vector 数组不适用于 SendInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49066645/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com