While creating programs that run with graphical user interfaces, we need to detect if the user has pressed a key or not several times. In this article, we will see how we can detect keypress in Python.
Detect Keypress Using the keyboard Module in Python
To detect keypress in Python, we can use the keyboard module. It works on both Windows and Linux operating systems and supports all the hotkeys. You can install the keyboard module in your machine using PIP as follows.
pip install keyboard
If you are working with Python 3, you can install the keyboard module using the following command.
pip3 install keyboard
Detect Keypress Using the is_pressed() function in Python
To detect the keypress in Python, we will use the is_pressed()
function defined in the keyboard module. The is_pressed()
takes a character as input and returns True if the key with the same character is pressed on the keyboard. Therefore, we can use the is_pressed() function with a while loop to detect keypress as shown in the following example.
import keyboard
while True:
if keyboard.is_pressed("a"):
print("You pressed 'a'.")
break
Output:
aYou pressed 'a'.
Here, we have executed the while loop until the user presses the key “a
”. On pressing other keys, the is_pressed()
function returns False
, and the while loop keeps executing. Once the user presses “a”,
the condition inside if block becomes true and the break statement is executed. Hence the while loop terminates.
Keypress using the read_key() Function
Instead of the is_pressed()
function, we can use the read_key()
function to detect the keypress in Python. The read_key()
function returns the key pressed by the user. We can use the read_key()
function with a while loop to check whether the user presses a specific key or not as follows.
import keyboard
while True:
if keyboard.read_key() == "a":
print("You pressed 'a'.")
break
Output:
You pressed 'a'.
In the above example, if the user presses the key “a”, the read_key() method returns the character “a”. Hence, the statement inside the if block becomes True and the execution moves out of the while loop using the break statement. Otherwise, the program keeps waiting for the user to press the correct key.
Detect Keypress Using the wait() Function
We can also detect keypress using the wait()
function defined in the keyboard module. The wait()
function takes a character as input. Upon execution of the wait() method, the execution of the program is paused until the user presses the key passed as an input argument to the function. Once the user presses the correct key, the function stops its execution.
You can observe this in the following example.
import keyboard
keyboard.wait("a")
print("You pressed 'a'.")
Output:
You pressed 'a'.
In this example, we have passed the character “a” to the wait() method. Hence, the wait() method halts the execution of the program until the user presses the key “a”. Once the correct key is pressed, the execution of the program moves ahead and the print statement is executed.
Conclusion
In this article, we have discussed different ways to detect keypress in Python using the keyboard module. To learn more about inputs, you can read this article on getting user input from keyboard in Python. You might also like this article on string concatenation in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.