Ask coding questions

← Back to all posts
Why can't I use keyboard module?
Aayan11

I wanted to make some hotkeys. so I found out about keyboard and it's useful feature that detects keypress which I really wanted.
Then I tried importing it, first it took A LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG TIME to load!
And then it said an error message

Voters
MemeManHimself
M07
Alex0204
Aayan11
Comments
hotnewtop
JBloves27

Oh, well, because it is python, you might want to use the getch module or maybe as Wumi4 said, use pynput?

Wumi4

Hi! Can you please show us the error?

The problem with the keyboard module is always a never-solved problem with Repl. Seriously, try it on your local machine is a much better choice.

Aayan11

@Wumi4 okay, is there a alternative
that can help me detect a key-press?

Wumi4

@Aayan11 I have found one. It's called pynput. You can search it for more info.

Aayan11

it shows me this error..."
Traceback (most recent call last):
File "main.py", line 1, in <module>
import pynput
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pynput/init.py", line 40, in <module>
from . import keyboard
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pynput/keyboard/init.py", line 31, in <module>
backend = backend(name)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pynput/_util/init.py", line 76, in backend
raise ImportError('this platform is not supported: {}'.format(
ImportError: this platform is not supported: ("failed to acquire X connection: No module named 'Xlib'", ModuleNotFoundError("No module named 'Xlib'"))

Try one of the following resolutions:

  • Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly"
M07

What language? (I'm not very skilled don't judge)

Aayan11

@M07 oh sorry I forgot to mention, it is python

M07

@Aayan11 Ok, I'm kinda good with python, but I'm nowhere near skilled enough to be able to do that kind of thing, if I could help I would love to, but I worry I would just be a hindrance.

Aayan11

@M07 nah at this point anything will help

YPD

You need root to use the keyboard module... and that's a thing you can't do on repl.it. You can't be root.

InvisibleOne

I don't think replit supports it, I would look into getch