【问题标题】:Is it possible to "rumble" a Xbox 360 controller with Python?是否可以用 Python “轰隆隆” Xbox 360 控制器?
【发布时间】:2013-11-03 03:00:32
【问题描述】:

是否可以使用 Python “轰隆”我的 PC 无线 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. 

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

【问题讨论】:

    标签: python output xbox vibration


    【解决方案1】:

    这是可能的,但并不容易。在 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)
    

    【讨论】:

    • 感谢您的回答,“ctypes.struct”出现错误:它不是有效属性。我也不知道“ctypes.struct”是从哪里来的!
    • @Belohlavek:哎呀,应该是Structure,而不是struct
    • 周末好项目! +1
    • @Belohlavek:你安装了 DirectX SDK 吗?根据文档,XInput 库仅默认安装在 Windows Vista 和 Windows 8 上。如果您确实安装了 DirectX SDK,但它仍然找不到 DLL,那么您需要使用类似 xinput = ctypes.windll.LoadLibrary(r'C:\path\to\Xinput.dll') 的内容手动加载库。
    • @Belohlavek:显然实际的 DLL 名称中包含一个版本号。尝试使用ctypes.windll.xinput1_1 代替(或xinput1_3XInput9_1_0,或者实际上您的C:\Windows\System32\xinput&lt;version&gt;.dll 的名称是什么)。
    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多