Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate XBox Controller Input with Python

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?

like image 636
Herka Avatar asked Apr 18 '17 22:04

Herka


1 Answers

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.

like image 193
Herka Avatar answered Oct 18 '22 14:10

Herka