"mouse_event"函数 - 将光标发送到(稍微)错误的坐标

"mouse_event" function - sending cursor to (slightly) wrong coordinates

本文关键字:稍微 坐标 错误 mouse event 函数 光标      更新时间:2023-10-16

mouse_event函数将光标发送到稍微错误的坐标(偏离1-20像素)。它"偏离"的程度是基于一种我不太清楚的模式。

我的代码

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

如果运行此命令,将导致鼠标从屏幕左上角向下和向右移动,然后再向上移动。但越往下,"mouse_event"创建的鼠标移动就越不正确。

我移动它,然后记录当前坐标,然后计算差值。这种差异(移动误差)随着我在屏幕上的移动而增加。我甚至尝试添加一个额外的检查,测试如果坐标是关闭的,然后尝试将鼠标移动到正确的位置,但它不工作

知道为什么会这样吗?

为方便起见,这里是该程序输出的日志。

Output_Log.txt

它清楚地表明,在第一个循环(向下和向右移动鼠标)中,错误增加,然后在第二个循环(再次向上和向左移动鼠标)中,错误以同样的方式减少。

知道为什么会这样吗?它发生在更复杂的实现以及我无法量化的方式,不像这个,所以我认为它必须在mouse_event函数本身或一些我不理解的功能

提前感谢您的帮助

我想说这是由于使用整数算法来计算像素,试试这个:

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