“mouse_event”功能 - 将光标发送到(略)错误的坐标 [英] "mouse_event" function - sending cursor to (slightly) wrong coordinates

查看:186
本文介绍了“mouse_event”功能 - 将光标发送到(略)错误的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mouse_event函数将光标移动到稍微错误的坐标(关闭1-20个像素)。它多少的程度是基于一个模式,我不能完全弄清楚。

The mouse_event function sends the cursor to slightly wrong coordinates (1-20 pixels off). The degree of how much it is "off" is based on a pattern I can't quite figure out.

这是我的代码

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创建的鼠标移动越不准确。

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.

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

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

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

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.

任何想法为什么会发生这种情况?它发生在更复杂的实现,以及我无法量化,这不同于这一个,所以我认为它必须在mouse_event函数本身或一些功能我不明白的方式。

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

先感谢任何帮助

推荐答案

我会说这是由于使用整数运算像素,请尝试:

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

这篇关于“mouse_event”功能 - 将光标发送到(略)错误的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆