1

I am trying to get my pi zero to act as a HID USB keyboard when plugged in to a computer, following this guide.

I finished the configuration, and I get to the "Keyboard / Mouse / Joystick (HID)" section. I added these commands to the bottom of /etc/rc.local:

sudo echo -ne "\0\0\x4\0\0\0\0\0" > /dev/hidg0
sudo echo -ne "\0\0\0\0\0\0\0\0" > /dev/hidg0

When I plug the pi zero into the target computer, it recognizes the USB device, as shown by dmesg:

[26919.807536] usb 1-1: new high-speed USB device number 48 using xhci_hcd
[26919.828689] usb 1-1: New USB device found, idVendor=1d6b, idProduct=0104
[26919.828698] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[26919.828704] usb 1-1: Product: iSticktoit.net USB Device
[26919.828709] usb 1-1: Manufacturer: Tobias Girstmair
[26919.828713] usb 1-1: SerialNumber: fedcba9876543210
[26919.831132] input: Tobias Girstmair iSticktoit.net USB Device as /devices/pci0000:00/0000:00:15.0/usb1/1-1/1-1:1.0/0003:1D6B:0104.0007/input/input21
[26919.888595] hid-generic 0003:1D6B:0104.0007: input,hidraw1: USB HID v1.01 Keyboard [Tobias Girstmair iSticktoit.net USB Device] on usb-0000:00:15.0-1/input0

But the computer does not receive any keystrokes at all. I am really struggling to figure this out, and I would apreciate any help. Thanks!

  • 1
    Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. Following the recommendation of the developers from systemd you should avoid using it. Maybe writing to /dev/hidg0 comes to early? – Ingo Dec 15 '18 at 19:09
  • @Ingo Thanks for the suggestion. How would you recommend I do it instead? I was thinking maybe cron, but that might also come too early. – eeze Dec 15 '18 at 19:23
  • I have made an answer. – Ingo Dec 15 '18 at 19:45
2

I don't know if this helps but it reduces one possible source of error. Doing what the developers from systemd suggest is using a Unit file. Here is a very generic one for your problem. Create a new unit file with:

rpi ~$ sudo systemctl edit --force --full hid-keyboard.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=HID keyboard emulator
After=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c 'echo -ne "\0\0\x4\0\0\0\0\0" > /dev/hidg0'
ExecStart=/bin/bash -c 'echo -ne "\0\0\0\0\0\0\0\0" > /dev/hidg0'

[Install]
WantedBy=multi-user.target

Enable the new service with:

rpi ~$ sudo systemctl enable hid-keyboard.service

On boot/startup/poweron it is executed one time. You can manual restart it and show its status with

rpi ~$ sudo systemctl restart hid-keyboard.service
rpi ~$ sudo systemctl status hid-keyboard.service
  • "On reboot it should be executed one time." Does this mean it will only work when I reboot with reboot, or will it also run on startup, if I plug it in to power? – eeze Dec 15 '18 at 19:53
  • I tried the steps in the answer, and still nothing happens – eeze Dec 15 '18 at 19:55
  • @eeze I have updated my answer about reboot. I suspected that nothing happens. I had just a quick view at the tutorial you have linked. Just on Step 0 I stopped reading. The tutorial is made for Raspbian Jessie so it seems a bit outdated. Do you use Jessie? I wouldn't like to investigate it anymore. – Ingo Dec 15 '18 at 20:35
  • Alright, thanks for the help. I will look more into it. Thanks again for the help! – eeze Dec 15 '18 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.