Use

input('Enter your input:')

if you use Python 3.

And if you want to have a numeric value, just convert it:

try:
    mode = int(input('Input:'))
except ValueError:
    print("Not a number")

If you use Python 2, you need to use raw_input instead of input.

Answer from sharpner on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 74423541 › problem-with-keyboard-read-key-in-python
Problem with keyboard.read_key() in Python - Stack Overflow
I want to capture the pressed key ... key = keyboard.read_key() if key == '1': print('something') elif key == '2': print('something') else: print('something') str = input('Type something: ')...
🌐
PyPI
pypi.org › project › keyboard
keyboard · PyPI
Python 2 and 3. Complex hotkey support (e.g. ctrl+shift+m, ctrl+space) with controllable timeout. Includes high level API (e.g. record and play, add_abbreviation). Maps keys as they actually are in your layout, with full internationalization support (e.g. Ctrl+ç). Events automatically captured in separate thread, doesn't block main program. Tested and documented. Doesn't break accented dead keys (I'm looking at you, pyHook).
      » pip install keyboard
    
Published   Mar 23, 2020
Version   0.13.5
Discussions

python - How to read keyboard input? - Stack Overflow
I would like to read data from the keyboard in Python. I tried this code: nb = input('Choose a number') print('Number%s \n' % (nb)) But it doesn't work, either with eclipse nor in the terminal, it's More on stackoverflow.com
🌐 stackoverflow.com
python - How to detect key presses? - Stack Overflow
This is a poor design pattern that I would never use in Python code. I would expect one of the following instead: a blocking IO call similar to input() or an event based callback system that notifies me when a key is pressed for asynchronous responses. 2025-12-24T06:58:15.337Z+00:00 ... 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... More on stackoverflow.com
🌐 stackoverflow.com
Where to Place the `readkey()` Function in the Python Standard Library?
Topic: Consoles/Terminals Where to Place the readkey() Function in the Python Standard Library? While using Python, I noticed the absence of a built-in method to read keyboard key presses directly. Although I discovered a method for reading keyboard presses on Windows, I believe a standard ... More on discuss.python.org
🌐 discuss.python.org
0
0
June 7, 2024
python - keyboard.read_key() records 2 events - Stack Overflow
I'm trying something: When I press a key, I want a counter to increase and add the current timestamp problem is: I get 2 events for whenever I press a key, any idea? import keyboard from datetime ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.

🌐
Read the Docs
pynput.readthedocs.io › en › latest › keyboard.html
Handling the keyboard — pynput 1.7.6 documentation
This class supports reading single events in a non-blocking fashion, as well as iterating over all events. ... from pynput import keyboard # The event listener will be running in this block with keyboard.Events() as events: # Block at most one second event = events.get(1.0) if event is None: print('You did not press a key within one second') else: print('Received event {}'.format(event))
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()
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-detect-if-a-specific-key-pressed-using-python
How to detect if a specific key pressed using Python? - GeeksforGeeks
July 23, 2025 - The whole Module is divided into 3 segments, The 1st segment deal with simple integers, 2nd Alphanumerical characters and In 3rd we will use a python module to detect a key. Here we are importing the keyboard Module and with the help of read_key() method checking what key is pressed.
🌐
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 raw device files (/dev/input/input*) but this requires root.
Author   boppreh
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Where to Place the `readkey()` Function in the Python Standard Library? - Python Help - Discussions on Python.org
June 7, 2024 - Topic: Consoles/Terminals Where to Place the readkey() Function in the Python Standard Library? While using Python, I noticed the absence of a built-in method to read keyboard key presses directly. Although I discovered a method for reading keyboard presses on Windows, I believe a standard ...
🌐
Python Basics
pythonbasics.org › keyboard-input
How to read keyboard-input? - Python Tutorial
Related course: Complete Python Programming Course & Exercises · The input function prompts text if a parameter is given. The functions reads input from the keyboard, converts it to a string and removes the newline (Enter). Type and experiment with the script below (save as key.py)
🌐
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 455 users
Languages   Python 99.7% | Makefile 0.3%
🌐
Java2Blog
java2blog.com › home › python › detect keypress in python
Detect keypress in Python - Java2Blog
June 12, 2021 - In this article, we will discuss how to detect keypress in Python. ... The keyboard module is well equipped with different functions to perform operations related to keyboard input, detecting-simulating key presses, and more. This module works normally on Windows but requires the device to be rooted on Linux devices. To detect keypress, we can use a few functions from this module. The read_key() function from this module is used to read the key pressed by the user.
🌐
TutorialsPoint
tutorialspoint.com › reading-keyboard-input-in-python
Reading Keyboard Input in Python
September 1, 2025 - # Read the keyboard input input_data = raw_input("Enter your input: ") # Display the output print"Received input is : ", input_data · While executing the above program ,it prompts you to enter any input and it would display same as string on the screen. When I typed "Hello Python!", its output is like this ? Enter your input: Hello Python Received input is : Hello Python
🌐
Shallow Thoughts
shallowsky.com › blog › programming › python-read-characters.html
Reading keypresses in Python (Shallow Thoughts)
A less minimal example: keyreader.py, a class to read characters, with blocking and echo optional. It also cleans up after itself on exit, though most of the time that seems to happen automatically when I exit the Python script.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Reading key presses in Python (without pausing execution) - Raspberry Pi Forums
November 17, 2021 - Use The Keyboard Library For Python Terminal Replace "X.XX" with your python version or remove it if installed only one version ... import keyboard while True: if keyboard.is_pressed('a'): print('a key has ben pressed') print(1) print(1+2)
🌐
PyPI
pypi.org › project › getkey
getkey · PyPI
Please consult tools/keys.txt for a full list of key names available on different platforms, or tools/controls.txt for the abridged version just containing control (normally non-printing) characters. ... Reads the next key-stroke from stdin, returning it as an string.
      » pip install getkey
    
Published   Sep 01, 2016
Version   0.6.5
🌐
Real Python
realpython.com › python-keyboard-input
How to Read User Input From the Keyboard in Python – Real Python
February 20, 2024 - Examples would be inputs such as passwords, API keys, and even email addresses. In these conditions, you can use the standard library package getpass to achieve the intended effect.
🌐
Delft Stack
delftstack.com › home › howto › python › python detect keypress
How to Detect Keypress in Python | Delft Stack
February 12, 2024 - This tutorial provides a demonstration of how to detect keypress on a keyboard in Python.