Hi there, for my project (a remote controle on my raspberry pi 4 on Debian arm64) i want a button connect to the UP arrow. SO if this button on the remote controle is pressed python has to give a arrow up.
I wanted to use the python-keyboard module for that. Now, installing it is a bit problematic. I have tried to use pip install keyboard. But then i get the “This environment is externally managed” message. Then I tried “sudo apt get install python-keyboard” but that doesn’t exist. Then I installed pipx but pipx is complaining “No apps associated with package keyboard or its dependencies.” I tried sudo pip, no result. Then i did “pip install keyboard --break-system-packages” wich sound terrible to me but i saw no other solution. Now i have no complain after “import keyboard” but at using “keyboard.send()” i get “ImportError: You must be root to use this library on linux.”. I think because of the --break-system-pakkage thing. To solve this use on the cli sudo python script.py. No because “No module named ‘keyboard’”. It is not installed for user root. I don’t know it anymore. Pls help.
if you google this you will find that there are many threads addressing this issue (you might want to add the word Python in front of ImportError, however).
The problem here is that programs are deliberately not allowed to read keypresses, unless keyboard input is being directed at their window or the like. This is to protect against keyloggers and things like that. Since you’re doing a remote control, I assume there’s nobody actively using the Raspberry Pi? In that case you could probably just have your program open a window, set focus to itself, then monitor from there.
what you write is correct, it is a raspberry pi and i am the only one to use it. How can i use keyboard.send(key up) then? Or is there an other way to do this? It is strange i can not decide myself wether i want my system to be unsafe or not. It is always been linux that let you the choice.
But oke then, i have to work around it. Is it possible or do i have to change to C?
You’ll need to install the package as root in order to be able to run it as root. I would recommend becoming root (eg sudo -i), creating a virtual environment (python3 -m venv env) - it will then be owned by root - and then using that to install your package and run your script.