2

I have a python program that uses evdev to simulate gamepad button presses. The device used is "cloned" from an xbox usb gamepad, by using ui = evdev.UInput.from_device(...)

But for some reason it seems that the presses is sometimes inconsistent.

Below is the function i use to simulate an A button tap:

def press_A_button():
    ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.BTN_A, 1)
    ui.write(evdev.ecodes.EV_SYN, 0, 0)
    sleep(0.01)
    ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.BTN_A, 0)
    ui.write(evdev.ecodes.EV_SYN, 0, 0)
    sleep(0.01)

If i remove the sleep's altogether, it seems no presses will register at all. Albeit 0.01 seems to be too rapid as well, causing it to appear to not register/skip a tap every now and then. (i simply use jstest-gtk to watch how it behaves)

Here's how i test the function:

while True:
    try:
        press_A_button()
        # putting a sleep here, e.g a sleep(2), seemingly still doesn't
        # keep it from sometimes missing the tap
    except KeyboardInterrupt:
        ui.close()
        break

So is there some way to figure out the optimal timing that should be used for it to work consistently?

Or could the problem perhaps not be with my code, but the polling rate of the other software (in this case jstest-gtk) ?

A possibly related issue:

Simulating controller dpad button being held down with Python evdev

DhP
  • 306
  • 1
  • 11
  • 1
    I recommend you to study the kernel input module sources here: https://github.com/torvalds/linux/tree/master/drivers/input . –  Aug 24 '20 at 14:39
  • 1
    I _think_ jstest-gtk does not rely on polling; if I understand correctly it's performing async I/O [here](https://gitlab.com/jstest-gtk/jstest-gtk/-/blob/master/src/joystick.cpp#L81). – Thomas Aug 24 '20 at 14:52
  • @Roadowl unfortunately my C knowledge is a bit too weak to properly figure out what i should be looking for – DhP Aug 24 '20 at 15:11

0 Answers0