简体   繁体   English

使用 SendInput 或 mouse_event 不适用于 clock()?

[英]Using SendInput or mouse_event doesn't work with clock()?

void leftClick(){
    INPUT input[2];

    input[0].type = INPUT_MOUSE;
    input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    input[1].type = INPUT_MOUSE;
    input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;

    SendInput(2, input, sizeof(INPUT));
    cout<<"click down\n";
    cout<<"click up\n\n";

/*
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
*/
}

ReadProcessMemory(process, (void*)address, &value, sizeof(value), 0);
if (value == 1){
    if (clock() - click_time >= 500){
        cout<<"click time = "<< click_time <<endl;
        cout<<clock() - click_time <<endl;

        leftClick();

        click_time = clock();
    }
}

This gives an output of这给出了 output

click time = 0
57209
click down
click up

click time = 57222
501
click down
click up

click time = 57738
500
click down
click up

As you can see, the clicks should be happening, but this doesn't send any clicks.如您所见,点击应该会发生,但这不会发送任何点击。 It only actually clicks if I take out all the clock stuff.只有当我拿出所有时钟的东西时,它才会真正发出咔嗒声。 Same thing happens if I use mouse_event instead of SendInput.如果我使用 mouse_event 而不是 SendInput,也会发生同样的事情。

edit: I changed SendInput up there, didn't change any behavior.编辑:我在那里改变了 SendInput,没有改变任何行为。 The commented mouse_events gives the same behavior as well注释的 mouse_events 也给出了相同的行为

edit: It seems I can't make a minimal reproducible example.编辑:似乎我无法制作一个最小的可重现示例。 Without ReadProcessMemory, it works fine with a delay, it only doesn't send clicks with a delay while reading the process.如果没有 ReadProcessMemory,它会延迟正常工作,它只是在读取进程时不会延迟发送点击。 Looking in to issues with combining ReadProcessMemory with any sort of delayed input.研究将 ReadProcessMemory 与任何类型的延迟输入相结合的问题。

The target process/window can detect SendInput and block it using the LLMHF_INJECTED flag.目标进程/窗口可以检测到 SendInput 并使用 LLMHF_INJECTED 标志阻止它。

If it's not blocking the input, you should SendInput() once with MOUSEEVENTF_LEFTDOWN, then after a delay, you should call it again with MOUSEEVENTF_LEFTUP.如果它没有阻塞输入,您应该使用 MOUSEEVENTF_LEFTDOWN 发送一次输入(),然后在延迟之后,您应该使用 MOUSEEVENTF_LEFTUP 再次调用它。 You don't know how the target process is processing input, adding a delay has been helpful for me in several situations.您不知道目标进程如何处理输入,在几种情况下添加延迟对我很有帮助。

Here's an example usint std::chrono for 1 delay timer, I used a sleep between DOWN & UP, in an effort to not over complicate the solution more than it needs to be for a simple example.这是一个用于 1 个延迟计时器的 usint std::chrono 示例,我在 DOWN 和 UP 之间使用了睡眠,以尽量不使解决方案过于复杂,而不是简单的示例。

#include <chrono>
#include <thread>

int main()
{
    INPUT input{ 0 };
    input.type = INPUT_MOUSE;

    bool bClick = false;

    using Clock = std::chrono::steady_clock;
    std::chrono::time_point<std::chrono::steady_clock> start, now;
    std::chrono::milliseconds duration;

    start = Clock::now();

    while (true)
    {
        //toggles it on and off with one key
        if (GetAsyncKeyState('U') & 1)
            bClick = !bClick;

        if (bClick)
        {
            now = Clock::now();
            duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);

            if (duration.count() >= 100)
            {
                input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
                SendInput(1, &input, sizeof(INPUT));

                std::this_thread::sleep_for(std::chrono::milliseconds(30));

                input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
                SendInput(1, &input, sizeof(INPUT));
                start = std::chrono::steady_clock::now();
            }
        }
    }
    return 0;
}

You can toggle it on and off with the U key.您可以使用 U 键打开和关闭它。

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

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