The screen goes black when I use SendInput to send mouse positions

I use SendInput () to send relative mouse positions. First, your sick what you do.

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

then I send this position to the next method to generate relative mouse position commands using the send input.

When the finger moves to the left border (xlim1) or the right edge (xlim2), the cursor holds horizontally left or right, depending on which limit. The problem is that when I run the code and only when the cursor starts to move, the screen goes black.

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

The first bool variable will be true, if this is the first time, we fix the finger. Otherwise, this is not true.

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

}

+7
source share
2 answers

Try

 ZeroMemory(&input,sizeof(input)); 

also initialize all variables including input.time this worked for me :)

+6
source

I ran into the same problem, although I called ZeroMemory and did the rest correctly. I used input.mi.time to inform Windows about the intervals between clicks, for example. therefore double-clicking will work correctly. However, I was getting time values โ€‹โ€‹from a remote computer. Because they were different from the time on the local computer, this caused Windows to invoke a screen saver! To get around the problem, I added some logic to detect skew between computers and bring the values โ€‹โ€‹somewhat in line with each other.

In short: make sure input.mi.time is zero or a value close to GetTickCount (). Using ZeroMemory to initialize a variable is a great suggestion.

+1
source

All Articles