Python Forum
Can't get keyboard.library to work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't get keyboard.library to work
#1
This example:


import keyboard  # using module keyboard
import time
while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.is_pressed('q'):  # if key 'q' is pressed 
            print('You Pressed A Key!')
            break  # finishing the loop
    except:
        time.sleep(2)
        print('xxxxxx!')
won't capture any keyboard input. Eg. console output:

Output:
xxxxxx! xxxxxx! xxxxxx! xxxxxx! xxxxxx! qqqxxxxxx! qqqqxxxxxx! qqqqqxxxxxx! qqqqqqqqqxxxxxx! qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxxxxxx! qqqqqqqqqqxxxxxx! xxxxxx! xxxxxx!
I tried using opencv waitKey and had the same behavior. I tried it on a different computer with same result.

Using python 3.11.2 on Debian 12 in a venv.

What am I doing wrong?

Also, where are the code tags?

Thanks,
JP.
Reply
#2
What are you trying to accomplish? The combination of checking if a key is pressed and a lengthy wait is odd.
Reply
#3
Code work when i test in Windows.
keyboard library on Linux needs access to low-level input devices,
which is typically restricted unless you run as root / have special permissions.
To see the real error do this
import keyboard, time, traceback

while True:
    try:
        if keyboard.is_pressed('q'):
            print("You pressed q")
            break
        time.sleep(0.01)
    except Exception:
        traceback.print_exc()
        break
If you’re in a venv, run the venv Python with sudo:
sudo -E /path/to/venv/bin/python yourscript.py
Reply
#4
Snipsat, I'll have to look into the traceback lib, I have never done that before. This is the result:

(venv_1) jogl@aspire23:~/venv_1$ sudo -E /home/jogl/venv_1/bin/python 1.py
qqqq twgtebdchqqq^[^CTraceback (most recent call last):
  File "/home/jogl/venv_1/1.py", line 8, in <module>
    time.sleep(2)
KeyboardInterrupt

(venv_1) jogl@aspire23:~/venv_1$ 
It hangs on the sleep until I interrupt regardless of the sleep time value.

Quote:What are you trying to accomplish?
I am using opencv to determine locations for a robot to pick up things. I just wanted to use keyboard inputs to simulate the handshaking between vision computer and robot.
Reply
#5
I guess I'll have to find another way to simulate the robot. I just thought that using a few keys would be quick and easy... Wrong!
Reply
#6
Silly Frenchman? I'm a grumpy old Canadian of Scottish and English ancestry.
Reply
#7
(Dec-14-2025, 10:24 AM)jogl Wrote: I guess I'll have to find another way to simulate the robot. I just thought that using a few keys would be quick and easy... Wrong!
OpenCV’s has it's own key handling inside the display loop:
import cv2

while True:
    # ... your vision work ...
    cv2.imshow("view", frame)

    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        print("Handshake: q")
        break
    elif key == ord('g'):
        print("Handshake: go")
(Dec-14-2025, 10:24 AM)jogl Wrote: Silly Frenchman? I'm a grumpy old Canadian of Scottish and English ancestry.
User Titles
Reply
#8
(Dec-14-2025, 10:29 AM)jogl Wrote: Silly Frenchman? I'm a grumpy old Canadian of Scottish and English ancestry.
See this for example. The name of the Python language comes from tho Monty Python show.
« We can solve any problem by introducing an extra level of indirection »
Reply
#9
(Dec-14-2025, 11:09 AM)snippsat Wrote: OpenCV’s has it's own key handling inside the display loop:

Yes, I started out this whole ordeal trying to use waitKey. Similar problem, did not seem to acknowlege the key stroke. Then I read that the keyboard lib was less problematic...

OK, Monte Python, I see, the beatings will continue until a solution is found.
Reply
#10
keyboard module info

Quote:Known limitations:
Events generated under Windows don't report device id (event.device == None). #21
Media keys on Linux may appear nameless (scan-code only) or not at all. #20
Key suppression/blocking only available on Windows. #22
To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requires root.
Other applications, such as some games, may register hooks that swallow all key events. In this case keyboard will be unable to report events.
This program makes no attempt to hide itself, so don't use it for keyloggers or online gaming bots. Be responsible.

You could try pynput
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  keyboard module doesn't work in the microsoft version terminal of python. username 1 4,788 Feb-25-2021, 05:19 PM
Last Post: Larz60+
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 6,669 May-06-2017, 11:29 AM
Last Post: harun2525
  How does CMU Sphinx Java library work in Python j.crater 2 15,509 Oct-03-2016, 10:16 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020