将光标移动到另一个应用程序中的指定位置

时间:2016-07-20 10:31:53

标签: c# winapi events cursor mouse

我有一款使用FPC和我的应用的游戏。游戏将光标锁定在屏幕中央,只需在鼠标移动时移动游戏中的相机。我必须将光标移动到屏幕上的指定点(在游戏中)。我已经尝试了SetCursorPosSendInputmouseeventCursor.Position,但其中没有人做过这些事情。只有这个代码(我发现它在某个地方,但我找不到它;))移动了鼠标,但移动到了错误的位置(如果游戏在顶部,其他功能什么也没做):

private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
private const int MOUSEEVENTF_MOVE = 0x0001;

public static void MoveTo(float x, float y)
{
    float min = 0;
    float max = UInt16.MaxValue;

    int mappedX = (int)Remap(x, 0.0f, Screen.PrimaryScreen.Bounds.Width, min, max);
    int mappedY = (int)Remap(y, 0.0f, Screen.PrimaryScreen.Bounds.Height, min, max);

    mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, mappedX, mappedY, 0, 0);
}


public static float Remap(float value, float from1, float to1, float from2, float to2)
{
    return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}

我认为这是因为光标仍处于锁定状态,因此功能仍会尝试移动鼠标。我没有更多的想法如何做到这一点。

[编辑]:问题是游戏使用原始输入。即使原始输入选项打开,如何发送按预期工作的鼠标输入?

1 个答案:

答案 0 :(得分:0)

您需要使用像WinIO这样的内核模式库来模拟原始输入。 (好的,你自己的程序可以在用户模式下工作)