Skip to content

Instantly share code, notes, and snippets.

@davesteele
Last active Apr 19, 2021
Embed
What would you like to do?
Implement a Pi Keyboard Jiggler using pizero-usb-hid-keyboard
#!/usr/bin/env bash
/home/pi/pizero-usb-hid-keyboard/rpi-hid.sh
chmod 777 /dev/hidg0
/home/pi/tickle.py &
exit 0
#!/usr/bin/python3
import os
import select
import time
"""Suppress screen savers by periodically toggling Caps Lock
See https://davesteele.github.io/raspberrypi/2021/04/18/keyboard-jiggler/
Intended to be run on a Gadget Tools-generated device, which configurres a USB
port to run as a USB client.
This periodically sends two Caps Lock key presses, in quick succession, to the
remote host.
This requires an installed pizero-usb-hid-keyboard environment [1], which
creates a virtual keyboard USB client.
[1]: https://github.com/raspberrypisig/pizero-usb-hid-keyboard
"""
# keycodes -
# https://github.com/raspberrypisig/pizero-usb-hid-keyboard/blob/master/hid-gadget-test.c#L32
KEYCODE = 0x39
GDEV = "/dev/hidg0"
DELAYSECS = 55
def capstoggle():
"""Send two Caps Lock key presses in quick succession."""
press = bytearray([0, 0, KEYCODE, 0, 0, 0, 0, 0])
release = bytearray([0, 0, 0, 0, 0, 0, 0, 0])
fd = None
try:
fd = os.open(GDEV, os.O_RDWR)
for _ in range(2):
for msg in (press, release):
os.write(fd, msg)
except Exception:
print("Error writing device")
finally:
if fd is not None:
os.close(fd)
def main():
while True:
capstoggle()
time.sleep(DELAYSECS)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment