🌐
GitHub
github.com › boppreh › keyboard › blob › master › README.md
keyboard/README.md at master · boppreh/keyboard
# Replay events python -m keyboard < events.txt · 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 ...
Author   boppreh
🌐
GitHub
github.com › boppreh › keyboard
GitHub - boppreh/keyboard: Hook and simulate global keyboard events on Windows and Linux. · GitHub
February 13, 2026 - # Replay events python -m keyboard < events.txt · 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.
Starred by 4K users
Forked by 454 users
Languages   Python 99.7% | Makefile 0.3%
Discussions

Difference between read_hotkey and add_hotkey
I'm using this package to control a piece of hardware: · Now I would like to use hotkeys, e.g. something like More on github.com
🌐 github.com
14
June 29, 2021
python - How to detect key presses? - Stack Overflow
I am making a stopwatch type program in Python and I would like to know how to detect if a key is pressed (such as p for pause and s for stop), and I would not like it to be something like raw_input, More on stackoverflow.com
🌐 stackoverflow.com
Problem with keyboard.read_key() in Python - Stack Overflow
I want to capture the pressed key and do different tasks if it for example is '1' or '2', etc. This is my sample code: import keyboard key = keyboard.read_key() if key == '1': print('something') More on stackoverflow.com
🌐 stackoverflow.com
Question about the read_key() method from the keyboard module .
FYI you can use code blocks to make your code more readable and preserve formatting. This will help you get more responses: import keyboard recording = True keys = [] while recording: keys.append(keyboard.read_key()) if keyboard.read_key() == "esc": recording = False print(keys) From the documentation it seems you can use keys = keyboard.record(until='esc')so maybe use that instead? It's often better to use the library's own functions because the author will have thought of issues like repeated key presses so you don't have to, though of course sometimes you have to hack things together yourself. More on reddit.com
🌐 r/learnpython
3
3
January 18, 2022
🌐
GitHub
github.com › wingkeet › getkey
GitHub - wingkeet/getkey: This Python module allows the user to read key presses immediately, as the keys are typed, without having to press the Enter key. · GitHub
getkey.py is a Python 3 module that allows the user to read key presses immediately, as the keys are typed, without having to press the Enter key. It provides just one function, getkey(), which blocks until the user presses a key.
Author   wingkeet
🌐
PyPI
pypi.org › project › keyboard
keyboard · PyPI
Global event hook on all keyboards (captures keys regardless of focus). Listen and send keyboard events. Works with Windows and Linux (requires sudo), with experimental OS X support (thanks @glitchassassin!). Pure Python, no C modules to be compiled.
      » pip install keyboard
    
Published   Mar 23, 2020
Version   0.13.5
🌐
GitHub
github.com › andohuman › pyKey
GitHub - andohuman/pyKey: A python library to simulate keyboard presses
August 18, 2019 - Will display the available keys and their corresponding scan codes (on windows) or xdotool name (on linux). The motivation for this project comes from sentdex's python plays gta series in youtube.
Author   andohuman
🌐
GitHub
github.com › topics › keyboard-input
keyboard-input · GitHub Topics · GitHub
It supports wrapping, key shortcuts ... textbox-input ... This Python module allows the user to read key presses immediately, as the keys are typed, without having to press the Enter key....
🌐
GitHub
github.com › boppreh › keyboard › blob › master › keyboard › __init__.py
keyboard/keyboard/__init__.py at master · boppreh/keyboard
- Key suppression/blocking only available on Windows. [#22](https://github.com/boppreh/keyboard/issues/22) - To avoid depending on X, the Linux parts reads raw device files (`/dev/input/input*`) but this requires root.
Author   boppreh
🌐
GitHub
github.com › magmax › python-readchar
GitHub - magmax/python-readchar: Python library to read characters and key strokes · GitHub
Reads the next keystroke from stdin, returning it as a string. Waits until a keystroke is available. ... Note CTRL+C will not be returned by readkey(), but instead raise a KeyboardInterupt.
Starred by 176 users
Forked by 52 users
Languages   Python 99.1% | Makefile 0.9%
Find elsewhere
🌐
GitHub
github.com › gauthsvenkat › pyKey
GitHub - gauthsvenkat/pyKey: A python library to simulate keyboard presses · GitHub
December 27, 2023 - Will display the available keys and their corresponding scan codes (on windows) or xdotool name (on linux). The motivation for this project comes from sentdex's python plays gta series in youtube.
Starred by 56 users
Forked by 6 users
Languages   Python
🌐
GitHub
github.com › boppreh › keyboard › issues › 457
Difference between read_hotkey and add_hotkey · Issue #457 · boppreh/keyboard
June 29, 2021 - adjust = True while adjust: if keyboard.read_key() == 'esc': adjust = False elif keyboard.read_key() == 'left': move_left() elif keyboard.read_key() == 'ctrl+left': # Use read_hotkey, add_hotkey, or some other function? move_left_large_step() From the documentation, it's not clear which function to use and how.
Author   danielbaumer
🌐
GitHub
gist.github.com › jasonrdsouza › 1901709
Python function to get keypresses from the terminal · GitHub
def getchar(): import tty, termios, sys fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) # Exit on ctrl-c, ctrl-d, ctrl-z, or ESC if ord(ch) in [3, 4, 26, 27]: sys.exit() return ch
🌐
GitHub
github.com › makerdiary › python-keyboard
GitHub - makerdiary/python-keyboard: A hand-wired USB & Bluetooth keyboard powered by Python and more · GitHub
A hand-wired USB & Bluetooth keyboard powered by Python and more - makerdiary/python-keyboard
Starred by 500 users
Forked by 59 users
Languages   Python
🌐
GitHub
github.com › AnesBenmerzoug › Bluetooth_HID › blob › master › keyboard › keyboard_client.py
Bluetooth_HID/keyboard/keyboard_client.py at master · AnesBenmerzoug/Bluetooth_HID
#!/usr/bin/python · # # Reads local key events and forwards them to the btk_server DBUS service · # # Adapted from https://www.gadgetdaily.xyz/create-a-cool-sliding-and-scrollable-mobile-menu/ # and http://yetanotherpointlesstechblog.blogspot.de/2016/04/emulating-bluetooth-keyboard-with.html ·
Author   AnesBenmerzoug
🌐
GitHub
github.com › learncodebygaming › pydirectinput
GitHub - learncodebygaming/pydirectinput: Python mouse and keyboard input automation for Windows using Direct Input. · GitHub
This library aims to replicate the functionality of the PyAutoGUI mouse and keyboard inputs, but by utilizing DirectInput scan codes and the more modern SendInput() win32 function.
Starred by 582 users
Forked by 94 users
Languages   Python
Top answer
1 of 16
144

Python has a keyboard module with many features. Install it, perhaps with this command:

pip3 install keyboard

Then use it in code like:

import keyboard  # using module keyboard
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:
        break  # if user pressed a key other than the given key the loop will break
2 of 16
114

For those who are on windows and were struggling to find an working answer here's mine: pynput.

Here is the pynput official "Monitoring the keyboard" source code example:

from pynput.keyboard import Key, Listener

def on_press(key):
    print('{0} pressed'.format(
        key))

def on_release(key):
    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        return False

# Collect events until released
with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

The function above will print whichever key you are pressing plus start an action as you release the 'esc' key. The keyboard documentation is here for a more variated usage.

Markus von Broady highlighted a potential issue that is: This answer doesn't require you being in the current window to this script be activated, a solution to windows would be:

from win32gui import GetWindowText, GetForegroundWindow
current_window = (GetWindowText(GetForegroundWindow()))
desired_window_name = "Stopwatch" #Whatever the name of your window should be

#Infinite loops are dangerous.
while True: #Don't rely on this line of code too much and make sure to adapt this to your project.
    if current_window == desired_window_name:

        with Listener(
            on_press=on_press,
            on_release=on_release) as listener:
            listener.join()
🌐
GitHub
github.com › makerdiary › python-keyboard › blob › main › README.md
python-keyboard/README.md at main · makerdiary/python-keyboard
A hand-wired USB & Bluetooth keyboard powered by Python and more - python-keyboard/README.md at main · makerdiary/python-keyboard
Author   makerdiary
🌐
GitHub
github.com › topics › virtual-keyboard-python
virtual-keyboard-python · GitHub Topics · GitHub
python3 artificial-intelligence hci human-computer-interaction gesture-recognition virtual-keyboard-python gesture-typing shorthand-gesture-typing decode-shorthand-gesture getsure-recognition-algorithm ... The AI Keyboard Project is a virtual keyboard that uses computer vision and machine learning to recognize hand gestures and movements, allowing users to type on a virtual keyboard without the need for physical hardware. The project uses OpenCV, MediaPipe, and Pynput libraries to track hand movements and detect keystrokes.
🌐
Stack Overflow
stackoverflow.com › questions › 74423541 › problem-with-keyboard-read-key-in-python
Problem with keyboard.read_key() in Python - Stack Overflow
It works fine but for example when I press key '1', when the last line (input) runs, the output is like this (Terminal): ... Appreciate any help! ... No module named 'keyboard'. Which library do you use ? ... Sorry. Too difficult to reproduce. Need to be root on Linux. ... keyboard is intended to capture and send keyboard events going to other programs. It doesn't actually consume the event, as that would make it useless for its intended purpose. ... You could try swapping keyboard.read_key() for the keyboard.is_pressed() function to process the input, depending on the keys pressed
🌐
Reddit
reddit.com › r/learnpython › question about the read_key() method from the keyboard module .
r/learnpython on Reddit: Question about the read_key() method from the keyboard module .
January 18, 2022 -

Im trying to read my keyboard input with the read_key() method. If i type slowly it will detect every key press. However, if i type fast then i will get multiples of the same key and completely miss other keys. Is this because the read_key() function cant keep up?

This program is still in its early stages but here is the code.

Import keyboard

recording = True keys = []

while recording: keys.append(keyboard.read_key()) if keyboard.read_key() == “esc”: recording = False

print(keys)

Edit: formatted with indents for the while loop and if statements. Just does not show indents when posted.