gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:34:29 28 4
gpt4 key购买 nike

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

关于c++ - "mouse_event"函数 - 将光标发送到(稍微)错误的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16849523/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com