Make Python Wait For a Pressed Key

In this article, we will discuss how to pause the execution of Python code until a given key is pressed. This concept can be useful to simply pause Python execution or impose conditions on the code implementation. There are three ways to do this:

  1. Using keyboard package
  2. Using msvcrt / getch package
  3. Using inbuilt input function

Before discussing those methods, let us discuss a concept we need to understand first.

Prerequisite step

Since we will be reading the key pressed, it is vital that the code editor or IDE we are using to implement these concepts accepts input from the user. I will, therefore, suggest you run the code snippets shown in this article using Visual Studio Code, PowerShell/ Terminal, or JuPyter. If you are running on Sublime Text, install the SublimeRepl package on the editor before running the code (see stackoverflow.com).

Once you are sure that your Python can take input, we can now proceed to discuss the three methods.

Method 1: Using the keyboard package

This package allows us to read the key pressed and then impose our condition at that point. It is not an inbuilt function in Python but can be installed using pip, that is, run pip install keyboard on Windows PowerShell. Once installed, we can execute the following code to learn how this method works:

import keyboard #load keyboard package

while True:
    if keyboard.is_pressed("q"): #returns True if "q" is pressed
        print("You pressed q")
        break #break the while loop is "q" is pressed

The above code will keep running until key “q” is pressed, that is when “q” is pressed Python prints “You pressed q” and then the loop is terminated with a “break” keyword.

As an alternative to the above method, we can use the following line of code  (In this case, Python waits until the escape key is pressed).

keyboard.wait("esc")#wait for escape key to be pressed

The package also has a function called read_key() that reads the key pressed. You can read more cool things you can do with the keyboard package in its documentation.

In Linux and macOS, you need to have SUDO (Super User DO) privileges to use the keyboard package for executing python code and installing the package.

Method 2: Use msvcrt / getch package

This inbuilt Python package provides useful functions in Microsoft Visual C/C++ Runtime Library (hence the name msvcrt). This article will leverage its functionality of reading the key pressed to pause Python execution.

import msvcrt

while True:
    char = msvcrt.getch().decode("utf-8")#read pressed key, decode
    print(char)
    if char == chr(27): #char(27) is the escape key
        break

In the above code snippet, msvcrt.getch() fetches the code pressed in byte format, and therefore we need to use UTF-8 decoding to get it in string format. For example, b’\x0c’ becomes ♀, b’\x1b’ becomes ← (this is the escape key, by the way).

You can input different non-standard characters with the Ctrl key and a letter to see how it works.

The code function introduced in this code snippet is chr(). It returns a string character from an integer (the integer represents the Unicode code point of the string character). Character 27 in the Unicode is an escape character, and therefore pressing the escape key matches chr(27) in the code. Therefore, the while loop is terminated only when the escape key is pressed. It is necessary to use the chr() function only when dealing with problematic decoded characters like escape key; otherwise, when dealing with alphabets, for example, we won’t need chr().

The msvcrt package is only available on Windows OS. On Linux and macOS, the getch package can be used to get the same functionalities as msvcrt. You might need to install the getch package by running pip install getch on the terminal. Once installed, you can now use the code below instead

from getch import getch

while True:
    char = getch() #read the pressed key
    print(char)
    if char == chr(27):
        break

Method 3: Using the input function

In Python 3, using the input() function will pause Python execution until the ENTER key is pressed, for example,

input("Press Enter to continue... ")

In Python 2, you can use raw_input() or input().