Python DirectInput 鼠标相对移动行为不符合预期

Pyhon DirectInput Mouse Relative Moving act not as expected

我找到了通过 DirectInput 模拟鼠标移动的解决方案。要点是使用 python 脚本在 3D 游戏中导航角色。这意味着必须使用相对鼠标移动。

一切正常,但是当我尝试计算 x 单位之间的关系(在 MouseMoveTo 函数中)和 Angle 角色在游戏中转弯时,我发现 算术不能正常工作

例如:

当我移动鼠标 2 x 200 单位向左 然后 1 x 400 单位向右 字符不看同一个方向(光标不是如果在桌面上,则在同一位置)

2x200 < 1x400

如果我尝试为运动设置动画(例如将运动分成 50 步),情况会变得更糟。

我是做错了什么还是正常行为?如果这是正常行为,我有什么方法可以计算以更正传递给 MouseMove To() 的单位数?

import ctypes
import time

# C struct redefinitions 
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time",ctypes.c_ulong),
                ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

# Actuals Functions
def MouseMoveTo(x, y):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.mi = MouseInput(x, y, 0, 0x0001, 0, ctypes.pointer(extra))

    command = Input(ctypes.c_ulong(0), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(command), ctypes.sizeof(command))

好的...所以问题是 Windows "Enhance Pointer Precision" 设置,简而言之,它使小(慢)鼠标移动更小,大(快)更大...

关闭后一切正常。

有关此内容的更多信息 windows "feature" 此处 https://www.howtogeek.com/321763/what-is-enhance-pointer-precision-in-windows/

只需 运行 下面的代码 ...

def MouseMoveTo(x, y):
        x = 1 + int(x * 65536./1920.)#1920 width of your desktop
        y = 1 + int(y * 65536./1080.)#1080 height of your desktop
        extra = ctypes.c_ulong(0)
        ii_ = Input_I()
        ii_.mi =  MouseInput(x,y,0, (0x0001 | 0x8000), 0, ctypes.pointer(extra) )
        x = Input( ctypes.c_ulong(0), ii_ )
        SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))