简体   繁体   English

“mouse_event”函数 - 将光标发送到(略微)错误的坐标

[英]“mouse_event” function - sending cursor to (slightly) wrong coordinates

The mouse_event function sends the cursor to slightly wrong coordinates (1-20 pixels off). mouse_event函数将光标发送到稍微错误的坐标(1-20像素关闭)。 The degree of how much it is "off" is based on a pattern I can't quite figure out. 它“关闭”的程度取决于我无法弄清楚的模式。

Here is my code 这是我的代码

int x, y;
int repeats = 1000;
int start = 0;
POINT pt;

for(int i=0; i<repeats; i+=10) //first loop, down right
{
    x = (65536 / 1920) * i - 1; //convert to absolute coordinates
    y = (65536 / 1080) * i - 1;
    mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0); //move
    GetCursorPos(&pt); //get cursor position
    if(pt.x != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);} //check if the position is wrong, and if so fix it.
    if(pt.y != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);}
    cout << "Try: " << i << ", " << i << "\tReal: " << pt.x << ", " << pt.y << "\tDiff: " << pt.x - i << ", " << pt.y - i << '\n';
}

    for(int i=repeats; i>0; i-=10) //second loop, up left
{
    x = (65536 / 1920) * i - 1;
    y = (65536 / 1080) * i - 1;
    mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);
    GetCursorPos(&pt);
    if(pt.x != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);}
    if(pt.y != i){mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x, y, 0, 0);}
    cout << "Try: " << i << ", " << i << "\tReal: " << pt.x << ", " << pt.y << "\tDiff: " << pt.x - i << ", " << pt.y - i << '\n';
}

If this is run it results in the mouse moving down and right from the top left of the screen, then back up again. 如果运行此操作,则会导致鼠标从屏幕左上方向下移动,然后再次向上移动。 But the further down it goes, the more incorrect the mouse movements created by "mouse_event" end up being. 但是越往下走,“mouse_event”最终产生的鼠标移动越不正确。

I move it, then record the current coordinates, then calculate the difference. 我移动它,然后记录当前坐标,然后计算差异。 The difference (the error in movements) increases the further down the screen I go. 差异(运动中的误差)增加了我走的屏幕。 I've even tried to add an additional check which tests if the coordinates are off, then tries to move the mouse to the right spot again but it isn't working 我甚至试图添加一个额外的检查,测试坐标是否关闭,然后尝试再次将鼠标移动到正确的位置,但它不起作用

Any idea why this might be? 知道为什么会这样吗?

Here is a log of the output for this program for convenience. 为方便起见,这是该程序的输出日志。

Output_Log.txt Output_Log.txt

It clearly shows that in the first loop (which moves the mouse down and right) the error increases, then on the second loop (which moves it back up and left again) the error decreases in the same way. 它清楚地表明,在第一个循环(向下和向右移动鼠标)中,错误增加,然后在第二个循环(将其向上移动并再次向左移动)上,错误以相同的方式减小。

Any idea why this might be happening? 知道为什么会这样吗? It happens on more complex implementations as well in ways I can't quantify and which are unlike this one, so I think it must be within the mouse_event function itself or some feature I don't understand 它发生在更复杂的实现上,以及我无法量化的方式以及与此不同的方式,所以我认为它必须在mouse_event函数本身或某些我不明白的功能中

Thanks in advance for any help 在此先感谢您的帮助

I'd say it's due to using integer arithmetic to work out the pixels, try this: 我会说这是由于使用整数运算来计算像素,试试这个:

x = (int)(65536.0 / 1920 * i - 1); //convert to absolute coordinates
y = (int)(65536.0 / 1080 * i - 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM