Monday, 29 July 2019

Simulating gamepad hardware signals

I'm trying to write a script to simulate a gamepad, for testing purposes.

I've used the inputs library to capture the signals being sent by the gamepad but I'm at a loss when it comes to sending those signals to the computer to interpret (i.e. make the computer press A for me).

Code:

while 1:
    events = get_gamepad()
    for event in events:
        print(event.ev_type, event.code, event.state)

Result:

Absolute ABS_RY -210
Sync SYN_REPORT 0
Absolute ABS_HAT0X -1
Sync SYN_REPORT 0
Absolute ABS_HAT0X 0
Sync SYN_REPORT 0
Key BTN_SOUTH 1
Sync SYN_REPORT 0
Key BTN_SOUTH 0
Sync SYN_REPORT 0
Absolute ABS_RY -274
Sync SYN_REPORT 0

This resulted from me pressing left on the d-pad and the A button.

I was able to make a script using pynput which lets me type as follows:

from pynput.keyboard import Key, Controller
import time

kb = Controller()

#time before typing
time.sleep(4)

kb.press('h')
kb.release('h')
kb.press('e')
kb.release('e')
kb.press('l')
kb.release('l')
kb.press('l')
kb.release('l')
kb.press('o')
kb.release('o')

So my question is:

How do I send (for example) a press of the A button using a similar method?



from Simulating gamepad hardware signals

No comments:

Post a Comment