Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendInput putting the system to sleep

Tags:

c++

I'm trying to figure out the proper use of the SendInput function so I can directly manipulate the cursor on the screen, so for a basic test to see how things work, I made this short snippet that should move the cursor 10 pixels to the right. In theory.

#include <windows.h>
#include <winable.h>

int main()
{
    INPUT joyInput;
    joyInput.type = INPUT_MOUSE;
    joyInput.mi.dx = 10;
    joyInput.mi.dwFlags = MOUSEEVENTF_MOVE;
    SendInput(1, &joyInput, sizeof(INPUT));
    return 0;
}

However, in practice, the SendInput function is either putting my computer to sleep, or at least shutting off my monitors, which is certainly an unwanted effect! Commenting out that line prevents the issue from happening, but obviously I need it to perform the task. What am I doing wrong?

like image 324
Cs ツ Avatar asked Dec 05 '12 13:12

Cs ツ


1 Answers

The MOUSEINPUT structure has three members that you aren't initializing - dy, mouseData, and time. Since the documentation doesn't mention default values, I assume the program is free to initially fill those members with whatever junk it wants. You should explicitly set the values to avoid this.

#include <windows.h>
#include <winable.h>

int main()
{
    INPUT joyInput;
    joyInput.type = INPUT_MOUSE;
    joyInput.mi.dx = 10;
    joyInput.mi.dwFlags = MOUSEEVENTF_MOVE;

    joyInput.mi.dy = 0;
    joyInput.mi.mouseData = 0;
    joyInput.mi.time = 0;

    SendInput(1, &joyInput, sizeof(INPUT));
    return 0;
}
like image 175
Kevin Avatar answered Sep 23 '22 19:09

Kevin