Simulate XBox Controller Input with Python
Asked Answered
P

2

18

I want my python programm to simulate an XBox controller input. Both analog thumb sticks and the on/off buttons if possible.

I found topics about simulating Keyboard input with ctypes in python, for example here: Python simulate keydown

Is it possible to simulate it similar to an "keydown" on a normal keyboard or mouse?

Parisian answered 18/4, 2017 at 22:17 Comment(0)
P
19

In case someone with the same problems will find this thread:

I solved the issue with vJoy, pyVJoy and x360ce.

vJoy provides an SDK and driver to simulate input devices. You can have joysticks, gamepads etc.. PyvJoy allows you to access these drivers and simulate the input inside python. https://github.com/tidzo/pyvjoy pyvJoy uses values between 0 and 32767 for the "analog" sticks. For example, to get the left thumb-stick of an xbox controller in a neutral position you put the XAxis and YAxis in 1/2 of 32767.

So for me having input values between 0 and 1 I multiply them with this values. In my code it looks kinda like this:

MAX_VJOY = 32767
self.j = pyvjoy.VJoyDevice(1)

def play_function(self,X,Y,Z,XRot):
    self.j.data.wAxisX = int(X * self.MAX_VJOY)
    self.j.data.wAxisY = int(Y * self.MAX_VJOY)
    self.j.data.wAxisZ = int(Z * self.MAX_VJOY)
    self.j.data.wAxisXRot = int(XRot * self.MAX_VJOY)
    j.update()

You can also update "binary"/"digital" buttons this way, the pyvjoy github page has a few more examples. Try using this Joystick app for calibration: http://www.planetpointy.co.uk/joystick-test-application/

The final part is using X360CE, what is does is turn the vJoy "DigitalInput" Devide into a XInput device. So the PC/game thinks its an actual Xbox 360 or Xbox One Controller. This last part is only needed for some games that only allow official Xbox controllers, like GTA 5. You can get X360CE from here: http://www.x360ce.com/

All this combined allows me to play those games through python. I learned that using WASD to train a neural network doesnt work too well because it always acts to extreme because it only allows 1 or 0 for the button presses. With these controls you can get smoother game controls.

Parisian answered 7/5, 2017 at 18:17 Comment(3)
It sounds like you were working on a project similar to what I'm doing. Can you share how you managed to record the xbox controller inputs? Do you have a github page or code you can show? I'm trying to use the inputs package, but I'm getting some issues with synchronization. I posted my question here but haven't gotten a response yet. #53804229Hearsh
Hi @Hearsh I got the record Xbox controllert part from the TensorKart Project utils.file: github.com/kevinhughes27/TensorKart/blob/master/utils.pyParisian
Can please show how to use multiple buttons with the j.data method? @ParisianCasto
C
1

vgamepad makes this quite a bit easier than the accepted answer. It does not require any setup from my testing

PyPi: https://pypi.org/project/vgamepad/

Example:

import vgamepad as vg
import time

gamepad = vg.VX360Gamepad()

while True:
    print("press")
    gamepad.press_button(vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
    gamepad.update()
    time.sleep(0.5)
    print("release")
    gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)
    gamepad.update()
    time.sleep(0.5)
Coom answered 28/11, 2023 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.