SendInput - Cursor position wrong.

Hey,

I recently wanted to create a (yet) simple program that simulates a mousemovement.
So far I managed to make the program work. It does move the mouse, click when expected but the problem is the location it does click at.

Here's my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <Windows.h>
#include <stdio.h>

int leftclick (DWORD x, DWORD y);
int main(){

	POINT ptC;
	POINT ptC2;

	Sleep (5000);

	leftclick (1920,1080);
	GetCursorPos (&ptC);

	printf ("First position:\nX-Cord: %d\nY-Cord: %d\n", ptC.x, ptC.y);

	leftclick (100, 100);
	GetCursorPos (&ptC2);

	printf("Second position:\nX-Cord: %d\nY-Cord: %d\n", ptC2.x, ptC2.y);

	system("PAUSE");
	return 0;
}

int leftclick (DWORD dwx, DWORD dwy){

	INPUT input;
	input.type = INPUT_MOUSE;
	input.mi.dwExtraInfo = 0;
	input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
	input.mi.mouseData = 0;
	input.mi.time = 0;
	input.mi.dx = (65535 / GetSystemMetrics (SM_CXSCREEN)) * dwx;
	input.mi.dy = (65535 / GetSystemMetrics (SM_CYSCREEN)) * dwy;
	SendInput (1, &input, sizeof(input));
	return 0;
}


The problem now is:
I want the program (for testing purposes) to click at (1920, 1080) and (100, 100) afterwards.
Now it does click within a specific range. When I use GetCursorPos to retreive the cursors position it differs quite a bit from where I expected the click to be.
Any solutions, ideas, hints or at least SOMETHING I missed?


Oh, a second question I have is:

When I declare the following flag (in the code above) the program does use relative coordinates even though it shouldn't.
 
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;


Where as it works out well when I add a MOUSEEVENTF_MOVE to it.
Why is that? I couldn't find any solution to this in Microsoft MSDN or any other website.
Thank you for your help!
Alright, I partially solved the issue(s) involved in this piece of code.

For the difference in my coordinates when moving the mouse and calling GetCursorPos afterwards it's the following solution (related to my code above and its declarations):
1
2
input.mi.dx = (dwx + 1 ) * (65535 / GetSystemMetrics (SM_CXSCREEN));
input.mi.dy = (dwy + 1 ) * (65535 / GetSystemMetrics (SM_CYSCREEN));


In my original code there was a loss of data as the division came before the multiplication hence a few parts were lost after the CAST thus causing a 'completely' wrong position afterwards.
The +1 was added as there is still a loss of data (but very small) and +1 causes all this to be nearly correct.
If there is someone with a fully working solution that has no chance of faults you're free to post it ;)

As for the second issue I had: SendInput isn't meant to click on certain coordinates hence the MOUSEEVENTF_MOVE is necessary.
I couldn't find no workaround for this.
Anyways, I hope this helps some people out there.
If you multiply first,
dwx * 65535 == (dwx << 16) - 1
It's still exact arithmetic in DWORD.
After that, you have only a single division.
Topic archived. No new replies allowed.