I'm trying to write a code that displays a text when a particular key is pressed. If anyone can help me figure out how to install keyboard in repl.it, please let me know. This is a code for a maze, which moves the characters through it using arrow keys. Useless piece of information, but perhaps background detail would be important.
I wanted the text to be displayed when I pressed an arrow key, so I could develop that into my maze. I tried installing keyboard, but import keyboard doesn't work. Doing pip install keyboard inside the shell doesn't work, nor does sudo pip3 install keyboard, or pip3 install keyboard. I tried updating my pip install, but now it's giving me this error:
error: externally-managed-environment
This environment is externally managed This command has been disabled as it tries to modify the immutable
`/nix/store` filesystem.
To use Python with Nix and nixpkgs, have a look at the online documentation:
<https://nixos.org/manual/nixpkgs/stable/#python>.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
I've looked it up in the repl.it community, but there's nothing there, so I'm stuck with my assignment. Importing keyboard gives me the result:
pygame 2.5.2 (SDL 2.28.2, Python 3.10.11)
Hello from the pygame community. https://www.pygame.org/contribute.html
Working
Traceback (most recent call last):
File "/home/runner/Maze-Rough-Work/main.py", line 32, in <module>
if keyboard.is_pressed("a"):
File "/home/runner/Maze-Rough-Work/.pythonlibs/lib/python3.10/site-packages/keyboard/__init__.py", line 410, in is_pressed
_listener.start_if_necessary()
File "/home/runner/Maze-Rough-Work/.pythonlibs/lib/python3.10/site-packages/keyboard/_generic.py", line 35, in start_if_necessary
self.init()
File "/home/runner/Maze-Rough-Work/.pythonlibs/lib/python3.10/site-packages/keyboard/__init__.py", line 196, in init
_os_keyboard.init()
File "/home/runner/Maze-Rough-Work/.pythonlibs/lib/python3.10/site-packages/keyboard/_nixkeyboard.py", line 113, in init
build_device()
File "/home/runner/Maze-Rough-Work/.pythonlibs/lib/python3.10/site-packages/keyboard/_nixkeyboard.py", line 109, in build_device
ensure_root()
File "/home/runner/Maze-Rough-Work/.pythonlibs/lib/python3.10/site-packages/keyboard/_nixcommon.py", line 174, in ensure_root
raise ImportError('You must be root to use this library on linux.')
ImportError: You must be root to use this library on linux.
Using pip install pynput is giving me the same error:
error: externally-managed-environment
× This environment is externally managed
╰─> This command has been disabled as it tries to modify the immutable
`/nix/store` filesystem.
To use Python with Nix and nixpkgs, have a look at the online documentation:
<https://nixos.org/manual/nixpkgs/stable/#python>.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
You can try using
pynput, instead ofkeyboard.Install it like this:
pip install pynput, and hopefully that should work.Here's a snippet of code to get you started:
When you run this program, whatever key you press, you should see it pop up in the shell like this:
Key.right.Key.rightin this case because I pressed the right arrow key.Pynputis a great rounded choice, as it also supports both controlling and listening to the mouse and keyboard. For more information, I suggest you check out the documentation.It would help if you also explained what your error was with installing
keyboard, you showed the error with upgrading pip, but you didn't show the error when trying to installkeyboard, so it would help if you showed that. Ifpynputcannot install as well, then I recommend figuring out what's wrong with your installation and pip before anything else.