I'm reasonably familiar with software, and wanted to help my friend with their cosplay project, which was using this RGB matrix and toggling between different images using this mini three-key keyboard. I have a working program, everything works fine. The only problem is that we want the script to automatically run at boot, without a monitor plugged in. I didn't think too much about this during the planning stage because it seemed like it should be something that a raspberry pi is designed for, but we've run into a lot more trouble than I've expected.
Originally, I used the pynput module to poll for user input, but that seems to have a dependency on X, so we were unable to get it to work without a module plugged in.
I also found this question which mentions using the keyboard
module, but I keep getting raise ImportError('You must be root to use this library on linux.')
even after installing the module with sudo pip install
, running the script with sudo
, even doing sudo su
to actually change the user to root
user, so that was a dead end.
Finally, we switched to sshkeyboard which seemed to not have a dependency on anything like that, and the actual code that reads input isn't all that complicated:
import threading
from sshkeyboard import listen_keyboard
def keys(key):
print("key {key} pressed")
# Do additional work.
def poll():
listen_keyboard(
on_press=keys,
debug=True
)
poll_thread = threading.Thread(target=poll)
poll_thread.start()
In terms of automatically running on boot, I've considered I think everything that's possible:
- crontab is a no-go because it doesn't seem to work for interactive scripts.
- We were able to start the program by putting it in
.bashrc
, and even tried explicitly redirecting input with< /dev/tty1
, but in both cases the buttons didn't do anything. - Same with using
rc.local
-- it has to be run in the background for booting to continue, so getting input to there seems to be a dead end.
The most promising has been systemd with the following service:
[Unit]
Description=Interactive script
[email protected]
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/
StandardInput=ttfy-force
ExecStart=/home/pi/script.sh
[Install]
WantedBy=multi-user.target
It's not closer in the sense that the behavior is any different from the other methods, but it feels like it should work. I also tried using openvt as described in this answer and this answer, but neither of those seemed to make a difference -- still no input.
The other option I've considered is manually polling /dev/input
for keypresses (like the keyboard
module is supposed to do), but that seems like overkill given that seems like something that should be a solved problem.
Things are made slightly harder because I'm helping my friend remotely over a discord call, so I can't manually fiddle with things myself. But I'd appreciate any help that people have for how to get a script that runs on boot and can poll for keyboard input.
I've considered is manually polling /dev/input
... if that works, then why is that a problem?