How to detect which key is pressed in Python – Keypress detection in Python
This tutorial is on key press detection in Python. Today we are going to learn how to detect key press in Python. I am not talking about only the detection of a key press, we will also learn how to detect which key is pressed in Python.
Detect which key is pressed in Python
Here we are going to provide a Python program to detect which key is pressed. The program will work as below:
- After running the program, you can press any key.
- In the terminal, the program will tell you which key is pressed using the keyboard.
Python Program to detect key press:
import msvcrt while True: if msvcrt.kbhit(): key_stroke = msvcrt.getch() print(key_stroke) # will print which key is pressed
Here is the Python Code.
The sample output is:
$ python CodeSpeedy.py b'p' b'8' b'6' b'1' b'/' b']
Screenshot of the output:

Output: detect which key is pressed in Python
After the small b, in between the single quotes, the pressed key is shown.
Here you can see that we are using msvcrt module which is a module of windows. Though I am not sure if it will work on Linux or not. It has been tested on Windows and it works fine for me.
There are other ways too to detect keypress in Python. But I personally like this one.
Feel free to let us know if you find a better way to do this in the below comment section.
Learn,
- How to detect strings that contain only whitespaces in Python
- How to create multiplication table in Python
doesnt work, is this not python 3.0?
I have tested it with Python 3.6
Can you let us know which kind of error you are getting so that we can help you out
When i run it all i can do is type letters and stuff
is that how its supposed to be ? u type the letter r on ur keyboard and it types the letter r ?
how do u actually detect a keypress anf if that keypress gets pressed it runs a function ?
You need to run it in py.exe not idle. If you save the code and open it from file explorer it ought to automatically open in py.exe (black command prompt window)
You need to use .decode to convert bytes to string, and then analize the key pressed:
Example:
import msvcrt
def showX():
print (“x Key pressed”)
while True:
if msvcrt.kbhit():
key_stroke = msvcrt.getch()
print(key_stroke) # will print which key is pressed
if key_stroke.decode(“utf-8”) == “x”:
showX()
Hi, same problem here with python 3.7.4… copy/paste your code, i can type one or more characters in the same line, even pressing “enter” with only one character but nothing happens, the only feedback i can provide is after killing it:
“Traceback (most recent call last):
File “C:/borrame2.py”, line 4, in
if msvcrt.kbhit():
KeyboardInterrupt”
Seems msvcrt.kbhit() cant read my keyboard…
is it possible by using this if I want to start the timer when it detects the first keypress ?
The error could be as simple a print syntax issue. Try the code below.
import msvcrt
def showX(x):
print (“{} Key pressed”.format(x))
while True:
if msvcrt.kbhit():
key_stroke = msvcrt.getch()
print(key_stroke)
if key_stroke.decode(“utf-8”) == “x”:
showX()