You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pywinusb捕获鼠标原始数据失败的问题求助

鼠标原始数据捕获问题:pywinusb无数据、libusb/pyusb后端不可用

我尝试用pywinusb获取Gaming Mouse G402的原始数据,代码能正确识别设备,但捕获不到任何数据;另外用libusb和pyusb时,还碰到后端不可用的错误。

代码示例

import pywinusb.hid as hid

def sample_handler(data):
    print("Received raw data")
    print(f"Data Type: {type(data)}, Data Length: {len(data)}")
    print("Raw data: {0}".format(data))

def find_mouse_device():
    all_hids = hid.HidDeviceFilter().get_devices()
    for device in all_hids:
        print(f"Device found: {device.product_name} (Vendor ID: {device.vendor_id}, Product ID: {device.product_id})")
        if 'mouse' in device.product_name.lower():
            return device
    return None

def main():
    mouse = find_mouse_device()

    if mouse:
        print(f"Mouse found: {mouse.product_name} (Vendor ID: {mouse.vendor_id}, Product ID: {mouse.product_id})")

        try:
            mouse.open()
            print("Device opened successfully.")
            mouse.set_raw_data_handler(sample_handler)
            print("Raw data handler set.")

            print("Listening for raw data... (Press Ctrl+C to stop)")

            try:
                while True:
                    pass
            except KeyboardInterrupt:
                print("Stopping...")
            finally:
                mouse.close()
                print("Device closed.")
        except Exception as e:
            print(f"Error: {e}")
    else:
        print("No mouse found.")

if __name__ == "__main__":
    main()

运行输出

Device found: Gaming Mouse G402 (Vendor ID: 1133, Product ID: 49278)
Mouse found: Gaming Mouse G402 (Vendor ID: 1133, Product ID: 49278)
Device opened successfully.
Raw data handler set.
Listening for raw data... (Press Ctrl+C to stop)
Stopping...
Device closed.

问题解决办法

针对pywinusb无法捕获数据的处理

  • 精准过滤HID接口:G402这类游戏鼠标可能包含多个HID接口,原代码仅通过名称匹配可能选中了非输入接口。修改过滤逻辑,指定鼠标标准的Usage Page(0x01)和Usage ID(0x02):
    def find_mouse_device():
        # 过滤符合鼠标HID规范的设备
        mouse_filter = hid.HidDeviceFilter(usage_page=0x01, usage=0x02)
        all_mice = mouse_filter.get_devices()
        for device in all_mice:
            print(f"Device found: {device.product_name} (Vendor ID: {device.vendor_id}, Product ID: {device.product_id})")
            return device
        return None
    
  • 释放设备独占占用:Windows默认鼠标驱动会独占设备,导致pywinusb无法获取数据。可以通过USBDeview工具卸载默认HID驱动,改用通用HID驱动;或者用Zadig工具将设备绑定到兼容驱动。
  • 修复事件循环逻辑:原代码的while True: pass无法正确处理HID事件,替换为带休眠或poll的循环:
    import time
    # 替换原while循环
    while True:
        time.sleep(0.1)
        hid.util.hid_poll()  # 触发HID事件处理
    

针对libusb/pyusb后端不可用的处理

  • 安装后端依赖:Windows下需下载libusb-1.0.dll,放入系统目录(如C:\Windows\System32)或Python的site-packages目录。
  • 绑定设备到libusb驱动:使用Zadig工具,将G402的HID接口绑定到libusb-win32或libusbK驱动,确保pyusb能识别设备。
  • 以管理员权限运行脚本:Windows访问USB设备需要管理员权限,右键终端选择“以管理员身份运行”后执行脚本。

内容的提问来源于stack exchange,提问作者Vid Kalisnik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 03:26:02