pythonpython-3.xmousedirectinput

Python DirectInput Mouse Relative Moving act not as expected


I found solution to emulate mouse movement by DirectInput. Point is to use python script to navigate character in 3D Game. That means that have to use relative mouse movement.

Everything was working but when i try calculate relationship between x Units (in MouseMoveTo function) and Angle character turn in game, I found out that aritmetics did not work well.

For example:

When i move mouse 2 x 200 Units Left then 1 x 400 Units Right character not looking at same direction (cursor is not at same place if in desktop)

2x200 < 1x400

If i try to animate movement (for example divide movement to 50 steps), it gets even worse.

Am i doing something wrong or it is normal behavior? if it is normal behavior, is there any way i can calculate to correct number of units passing to 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))

Solution

  • Ok ... so problem was Windows "Enhance Pointer Precision" setting, which, in short, makes small (slow) mouse moves even smaller and big (fast) even bigger...

    After turning it off, everithing works fine.

    More about this windows "feature" here https://www.howtogeek.com/321763/what-is-enhance-pointer-precision-in-windows/