Simulate XBox Controller Input with Python

只谈情不闲聊 提交于 2019-11-29 03:07:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/43483121/simulate-xbox-controller-input-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!