Last active
September 7, 2023 18:42
-
-
Save davesteele/276a17315cff728bb5932c59329da850 to your computer and use it in GitHub Desktop.
Implement a Pi Keyboard Jiggler using pizero-usb-hid-keyboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
/home/pi/pizero-usb-hid-keyboard/rpi-hid.sh | |
chmod 777 /dev/hidg0 | |
/home/pi/tickle.py & | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Maybe you could modify the code to send a space at startup.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @davesteele thanks for this and the helpful blog here . All the instructions were very clear and easy to follow.
I have a M1 Mac. The h key was definitely sent to the host computer, but I couldn't get your double caps lock press to be recognised. When connecting the Pi Zero to the Mac for the first time, the Mac offered me the keyboard set up interface, asking for a key press to confirm that the accessory is a keyboard. Just wondered if you or anyone have found a way around that?