首页 > 解决方案 > 鼠标随 SendInput 飘走

问题描述

当我收到坐标时,GetCursorPos我需要将它们设置回坐标。

下面是一个代码示例(第一个场景),展示了如何实现这一目标。

for(;;)
{
    // get cursor position
    POINT Point;
    GetCursorPos(&Point);
    // send mouse input
    SetCursorPos(Point.x, Point.y);
    // sleep
    usleep(15000);
}

上述方法使用SetCursorPos,我不打算使用。下面的代码示例(第 2 种情况)应该以相同的方式使用INPUTand SendInput,但事实并非如此(更多下文)。

for(;;)
{
    // get cursor position
    POINT Point;
    GetCursorPos(&Point);
    // configure mouse input
    INPUT Input;
    Input.type = INPUT_MOUSE;               // mouse input type
    Input.mi.dx = (Point.x * 65535) / 2560; // normalized x coordinate
    Input.mi.dy = (Point.y * 65535) / 1440; // normalized y coordinate
    Input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; // move absolute
    Input.mi.time = 0;  // let system decide
    Input.mi.dwExtraInfo = 0;
    // send mouse input
    SendInput(1, &Input, sizeof(INPUT));
    // sleep
    usleep(15000);
}

我期望在这两种情况下会发生什么:

真正发生的事情:

(注)我使用双显示器设置,并排排列。两台显示器的分辨率均为 2560 x 1440。中间顶部是右侧显示器的左上角。

这(在我看来)清楚地表明我在使用SendInput. 我怎样才能消除这种奇怪的行为?

标签: cwindowswinapimouse

解决方案


推荐阅读