Simulate mouse and keyboard via Raspberry Pi OTG
There are many tutorials how to simulate keyboard input with the Raspberry Pi zero, but no good how to simulate mouse movement. Here is a reliable way.
- Preparation:
echo "dwc2" >> /etc/modules
echo "libcomposite" >> /etc/modules
echo "dtoverlay=dwc2" >> /boot/config.txt
- Copy HID file:
cp usb-hid /usr/bin/
- Add
/usr/bin/usb-hid
torc.local
- Reboot
- Clone https://github.com/aagallag/hid_gadget_test
make
chmod +x hid-gadget-test
- Use hid-gadget-test to use the mouse:
# move mouse (100,100)
echo ' 100 100' | ./hid-gadget-test/dev/hidg0 mouse > /dev/null
# move mouse (100,100) and hold button b1
echo '--hold --b1 100 100' | ./hid-gadget-test /dev/hidg0 mouse > /dev/null
If you get something error like Cannot send after transport endpoint shutdown
, the Raspberry Pi is not properly connected to your device or was not recognized. Reconnect your cables!