• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / How to Detect Keypress in Python

How to Detect Keypress in Python

Author: Aditya Raj
Last Updated: June 30, 2023

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.

Table of Contents
  1. Detect Keypress Using the keyboard Module in Python
    1. Detect Keypress Using the is_pressed() function in Python
    2. Keypress using the read_key() Function
    3. Detect Keypress Using the wait() Function
  2. Conclusion

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!

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us