简体   繁体   中英

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

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.

edit: I changed SendInput up there, didn't change any behavior. The commented mouse_events gives the same behavior as well

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. Looking in to issues with combining ReadProcessMemory with any sort of delayed input.

The target process/window can detect SendInput and block it using the LLMHF_INJECTED flag.

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. 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.

#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.

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