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

qyzbxkaa  于 2023-03-07  发布在  Python
关注(0)|答案(2)|浏览(350)

我找到了一个通过DirectInput模拟鼠标移动的解决方案。重点是使用Python脚本在3D游戏中导航角色。这意味着必须使用相对鼠标移动。
一切正常,但当我尝试计算x单位(在MouseMoveTo函数中)和Angular 之间的关系时,我发现aritmetics不工作好。
例如:
当我移动鼠标时向左移动2 x 200个单位然后向右移动1 x 400个单位字符没有看向相同的方向(如果在桌面上,光标不在相同的位置)

    • 200个小于1400个**

如果我试图动画运动(例如将运动分为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))
c6ubokkw

c6ubokkw1#

好吧...所以问题是Windows的“增强指针精度”设置,简而言之,这使得小(慢)鼠标移动更小,大(快)更大...
关机后,一切正常。
有关此Windows“功能”的更多信息,请单击https://www.howtogeek.com/321763/what-is-enhance-pointer-precision-in-windows/

o3imoua4

o3imoua42#

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

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

相关问题