Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen goes black when i use SendInput to send mouse cursor positions

I am using SendInput() to send relative positions of the mouse. First ill tel you what am doing.

i use my finger to move the mouse. So first i track my finger in a 640x480 image and get the absolute position in pixels with in the image.

then i send that position to the following method to generate relative mouse position commands using send input.

When the finger goes to the left boundary (xlim1) or the right boundary (xlim2) cursor keep scrolling horizontally to either left or right depending on which limit. The issue is when i run the code and just when the cursor starts to move, screen goes to black.

when i comment the part else if(cx >= prevX && cx > xlim2){ .... } section, then it works.. (So the when finger point goes to right limit of the image it cursor keeps scrolling horizontally to the right. commented part enables the left horizontal scrolling).

bool first variable will be true if this is the first time, we capture the finger. Otherwise it is false.

void movMouse(int cx, int cy, bool first){
static int prevX = 0;
static int prevY = 0;

static int leftPrevX;
static int rightPrevX;

int mx,my;

if(first == true){
    prevX = cx;
    prevY = cy;
}
else{
    mx = (cx - prevX);
    my = (cy - prevY);

    if(cx <= prevX && cx < xlim1){
        mx = -20;

        INPUT input;
        input.type          = INPUT_MOUSE;
        input.mi.mouseData  = 0;
        input.mi.dx         = -(mx);
        input.mi.dy         =  (my);

        input.mi.dwFlags    =  MOUSEEVENTF_MOVE;

        SendInput(1, &input, sizeof(input));
    }
    else if(cx >= prevX && cx > xlim2){
        mx = 20;

        INPUT input;
        input.type          = INPUT_MOUSE;
        input.mi.mouseData  = 0;
        input.mi.dx         = -(mx);
        input.mi.dy         =  (my);

        input.mi.dwFlags    =  MOUSEEVENTF_MOVE;

        SendInput(1, &input, sizeof(input));
    }
    else {
        INPUT input;
        input.type          = INPUT_MOUSE;
        input.mi.mouseData  = 0;
        input.mi.dx         = -(mx);
        input.mi.dy         =  (my);

        input.mi.dwFlags    =  MOUSEEVENTF_MOVE;

        SendInput(1, &input, sizeof(input));
    }

    prevX = cx;
    prevY = cy;
}

}

like image 555
user2389323 Avatar asked Jun 26 '13 08:06

user2389323


2 Answers

Try

ZeroMemory(&input,sizeof(input));

also intialize all the variables including input.time it worked for me :)

like image 178
mchouhan_google Avatar answered Nov 07 '22 10:11

mchouhan_google


I ran into this same problem, even though I was calling ZeroMemory and doing everything else correctly. I was using input.mi.time to inform Windows of the spacing between the clicks, e.g. so double-click would work correctly. However I was getting the 'time' values from a remote computer. Because they differed from the local computer's time it caused Windows to invoke the screen saver! To workaround the problem I added some logic to detect the skew between the computers and bring the values somewhat in line with each other.

In summary: Make sure that input.mi.time is either zero or a value somewhat close to GetTickCount(). Using ZeroMemory to initialize the variable is an excellent suggestion.

like image 42
Philip Beber Avatar answered Nov 07 '22 09:11

Philip Beber