11

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;
}

}

user2389323
  • 769
  • 2
  • 10
  • 22
  • 4
    There's no obvious connection between generating mouse input and "screen goes black". There is a flaw in your code, you don't initialize the INPUT structure completely, generating bogus data for MOUSEINPUT.time and .dwExtraInfo. Use `INPUT input = {};" Ensure there's a reasonable delay after you call this code. – Hans Passant Jun 26 '13 at 09:08
  • It worked!!!!!!!!!! ..... Wow u saved my life... I cant thank you enough!!!!!!......... – user2389323 Jun 26 '13 at 09:12
  • Hans' comment saved my bacon too. Should most definitely put that as an answer. – BugHunterUK May 02 '19 at 11:23

2 Answers2

10

Try

ZeroMemory(&input,sizeof(input));

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

mchouhan_google
  • 1,775
  • 1
  • 20
  • 28
2

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.

Philip Beber
  • 1,115
  • 12
  • 18