使用Python模拟XBox控制器输入 [英] Simulate XBox Controller Input with Python

查看:1199
本文介绍了使用Python模拟XBox控制器输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的python程序模拟XBox控制器输入. 如有可能,请同时使用模拟拇指摇杆和开/关"按钮.

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

我找到了有关在python中用ctypes模拟键盘输入的主题,例如: Python模拟keydown

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

是否可以像普通键盘或鼠标上的"keydown"一样模拟它?

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

推荐答案

如果遇到相同问题的人会发现此线程:

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

我用vJoy,pyVJoy和x360ce解决了这个问题.

I solved the issue with vJoy, pyVJoy and x360ce.

vJoy提供了一个SDK和驱动程序来模拟输入设备.您可以使用操纵杆,游戏手柄等. PyvJoy允许您访问这些驱动程序并模拟python内部的输入. https://github.com/tidzo/pyvjoy pyvJoy对模拟"棒使用0到32767之间的值. 例如,要使xbox控制器的左指杆处于中间位置,请将XAxis和YAxis置于32767的1/2中.

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.

因此,对于我来说,输入值介于0和1之间,我会将它们乘以该值. 在我的代码中,它看起来像这样:

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()

您还可以通过这种方式更新二进制"/数字"按钮,pyvjoy github页面上还有更多示例. 尝试使用此摇杆应用程序进行校准: http://www.planetpointy.co.uk/joystick-test-application/

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/

最后一部分是使用X360CE,其作用是将vJoy"DigitalInput" Devide转换为XInput设备.因此,PC/游戏认为它是实际的Xbox 360或Xbox One控制器.只有某些仅允许使用官方Xbox控制器的游戏(例如GTA 5)才需要最后一部分. 您可以从此处获取X360CE: http://www.x360ce.com/

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/

所有这些结合在一起,使我可以通过python玩那些游戏.我了解到,使用WASD训练神经网络并不能很好地工作,因为它总是发挥极端作用,因为它只允许1或0来按下按钮.使用这些控件,您可以获得更流畅的游戏控件.

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.

这篇关于使用Python模拟XBox控制器输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆