【问题标题】:Mouse emulation on Raspberry Pi 4 does not workRaspberry Pi 4 上的鼠标仿真不起作用
【发布时间】:2021-07-06 16:45:26
【问题描述】:

对于一个个人项目,我目前正在尝试让我的 Raspberry Pi 4 模拟键盘和鼠标。键盘模拟效果非常好并且令人信服,但是在模拟鼠标时我一直遇到问题。
我已将我的 Pi 配置为 HID 设备,并在启动时执行以下代码:
#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p pihid
cd pihid
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "test" > strings/0x409/manufacturer
echo "PiHID" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower

# Add functions here
mkdir -p functions/hid.usb0
echo 1 > functions/hid.usb0/protocol
echo 1 > functions/hid.usb0/subclass
echo 8 > functions/hid.usb0/report_length
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.usb0/report_desc
ln -s functions/hid.usb0 configs/c.1/

mkdir -p functions/hid.usb1
echo 2 > functions/hid.usb1/protocol
echo 2 > functions/hid.usb1/subclass
echo 8 > functions/hid.usb1/report_length
echo -ne \\x05\\x01\\x09\\x02\\xA1\\x01\\x09\\x01\\xA1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x03\\x05\\x01\\x09\\x30\\x09\\x31\\x15\\x81\\x25\\x7F\\x75\\x08\\x95\\x02\\x81\\x06\\xC0\\xC0 > functions/hid.usb1/report_desc
ln -s functions/hid.usb1 configs/c.1/
# End functions

ls /sys/class/udc > UDC

启动后/dev 中有两个文件,名为“hidg0”和“hidg1”,我应该可以写入它们。使用此 python 脚本将键盘命令写入“hidg0”完全可以正常工作:

NULL_CHAR = chr(0)

def write_report(report):
    with open('/dev/hidg0', 'rb+') as fd:
        fd.write(report.encode())

write_report(NULL_CHAR*2+chr(6)+chr(7)+chr(8)+chr(9)+chr(10)+chr(11))
write_report(NULL_CHAR*8)

当我在编辑器中时,会出现“cdefgh”。

这是我用来写“hidg1”的脚本:

NULL_CHAR = chr(0)

def ms_write(report):
    with open("/dev/hidg1", "rb+") as fd:
        fd.write(report.encode())

ms_write(NULL_CHAR+chr(100)+NULL_CHAR*6)
ms_write(NULL_CHAR*8)

但是在执行之后,不是将鼠标移动 100 个单位,而是什么也没有发生,程序结束了。 关于鼠标仿真或与 Windows HID 驱动程序的通信,我做错了什么?

【问题讨论】:

    标签: python linux windows emulation hid


    【解决方案1】:

    我认为鼠标需要 3 个字节而不是 8 个字节。另外,请尝试使用 wb+ 而不是 rb+

    NULL_CHAR = chr(0)
    
    def ms_write(report):
        with open("/dev/hidg1", "wb+") as fd:
            fd.write(report.encode())
    
    ms_write(NULL_CHAR+chr(100)+NULL_CHAR)
    
    ms_write(NULL_CHAR*3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-18
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 2022-09-07
      相关资源
      最近更新 更多