[英]Is it possible to “rumble” a Xbox 360 controller with Python?
是否有可能用Python“隆隆”我的無線Xbox 360控制器用於PC? 我只找到了閱讀輸入的解決方案,但我無法找到有關振動/隆隆聲的信息。
編輯:
按照@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.
請注意,上一個錯誤是從西班牙語翻譯的。
這是可能的,但這並不容易。 在C中,您將使用XInputSetState()
函數來控制隆隆聲。 要從Python訪問它,您必須編譯用C編寫的Python擴展或使用ctypes
庫 。
這樣的事情應該有用,但請記住我沒有測試過這個:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.