是否有可能用Python“隆隆”Xbox 360控制器?

时间:2013-11-03 03:00:33

标签: python output xbox vibration

是否可以使用Python“隆隆”我的无线Xbox 360控制器?我只找到了阅读输入的解决方案,但我无法找到有关振动/隆隆声的信息。

修改

按照@AdamRosenfield提供的代码,我收到以下错误。

Traceback (most recent call last):
  File "C:\Users\Usuario\Desktop\rumble.py", line 8, in <module>
    xinput = ctypes.windll.Xinput  # Load Xinput.dll
  File "C:\Python27\lib\ctypes\__init__.py", line 435, in __getattr__
    dll = self._dlltype(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found. 

请注意,上一个错误是从西班牙语翻译过来的。

1 个答案:

答案 0 :(得分:5)

这是可能的,但这并不容易。在C中,您可以使用XInputSetState() function来控制隆隆声。要从Python访问它,您必须编译用C编写的Python扩展或使用ctypes library

这样的事情应该有用,但请记住我没有测试过这个:

import ctypes

# Define necessary structures
class XINPUT_VIBRATION(ctypes.Structure):
    _fields_ = [("wLeftMotorSpeed", ctypes.c_ushort),
                ("wRightMotorSpeed", ctypes.c_ushort)]

xinput = ctypes.windll.xinput1_1  # Load Xinput.dll

# Set up function argument types and return type
XInputSetState = xinput.XInputSetState
XInputSetState.argtypes = [ctypes.c_uint, ctypes.POINTER(XINPUT_VIBRATION)]
XInputSetState.restype = ctypes.c_uint

# Now we're ready to call it.  Set left motor to 100%, right motor to 50%
# for controller 0
vibration = XINPUT_VIBRATION(65535, 32768)
XInputSetState(0, ctypes.byref(vibration))

# You can also create a helper function like this:
def set_vibration(controller, left_motor, right_motor):
    vibration = XINPUT_VIBRATION(int(left_motor * 65535), int(right_motor * 65535))
    XInputSetState(controller, ctypes.byref(vibration))

# ... and use it like so
set_vibration(0, 1.0, 0.5)