pydirectinput icon indicating copy to clipboard operation
pydirectinput copied to clipboard

Slow press even with _pause = False

Open Jorgvt opened this issue 5 years ago • 2 comments

Hi! First of all, thanks for working to make this library happen!

I'm currently implementing a bot that uses a NN to predict which key should be pressed and then I input it with the press() function. My problem is that when I'm not doing any input I can get about 60-70 fps (measured withing the mss loop for screen recording) but when using press() it drops down to about 3 fps and 4 fps if I use _pause=False.

I tryed using only the functions:

def PressKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

And I recover the 60 fps but it does miss some inputs randomly. Are there any other ways of making pydirectinput faster?

Thanks a lot in advance!

Jorgvt avatar Feb 22 '21 20:02 Jorgvt

Just FYI in case you didn't figure this out, you have to set pydirectinput.PAUSE to False before you call typewrite. As far as I can tell the _pause argument doesn't do anything. I can't see it being used anywhere in the functions it exists in; this is usually denoted by the leading underscore.

DDoop avatar Apr 06 '21 20:04 DDoop

doing pydirectinput.PAUSE = 0.0 fixed the issue for me.

terukaaz avatar May 06 '23 14:05 terukaaz