Check if User Pressed Enter in Python

There are several ways to check if ENTER key is pressed in Python. We will discuss the following methods in this article:

Method 1: Using the input() function

In Python, the input() function allows input from the user. Once the input prompt is started, it can be exited by pressing the ENTER key. The input() function will return the value typed by the user, and program execution will resume upon pressing ENTER. Let’s see an example.

Output (first execution):

Press ENTER:
You pressed ENTER.

Output (second execution):

Press ENTER: Tomasz Decker
You typed 'Tomasz Decker' before pressing ENTER.

In the first execution, ENTER was pressed on the prompt. This captures what we want. The second execution, on the other hand, shows the contrary. Other characters are typed before ENTER is pressed.

Method 2: Using the getkey package

The getkey module is used to read single keystrokes. In the following example, we use the package to read pressed keys and exit the while loop when ENTER key is pressed. The package can be installed using pip with the following command:

python3 -m pip3 install getkey

Method 3a: Using the getch package (for Linux and macOS users only)

Python getch module also fetches keystrokes from the keyboard. The following code shows how getch reads the pressed key and the loop breaks once ENTER key is pressed. As shown by the output of repr(<char>), the package captures ENTER with a new line character (“\n”).

If getch is not installed, you can install it using pip by running the following command on the terminal.

python3 -m pip install getch

Method 3b: Using the mvcrt package in Windows

Unlike getch used in Linux, mvcrt.getch() reads ENTER key as a carriage return character (“\r”). The mvcrt.getch() fetches the key pressed in byte format; therefore, we need to use UTF-8 decoding to get it in string format. The execution of the while-loop in the code snippet below will break when ENTER key is pressed.

Method 4: Using the pynput package

The pynput Python package will be used in this method to detect any key press. Classes for managing and monitoring the keyboard can be found in “pynput.keyboard.” It uses the pynput.keyboard.Listener method. Stopping the Listener can be done anywhere or by returning False from a callback (as shown in the following example). The callback (show) returns False when the ENTER key is pressed, stopping the Listener.

You can install pynput by running the following command.

python3 -m pip install pynput

Method 5: Using the keyboard package

The keyboard package is not inbuilt into Python but can be installed using pip. Additionally, you need to be a root user to install and use the keyboard in Linux and macOS. For that reason, we need to invoke sudo when installing the package.

sudo pip3 install keyboard

For Windows, you can install and use the keyboard package normally. Use the following command on Command Prompt or Windows Powershell to install the package: “python3 -m pip uninstall keyboard”

File: press_enter3.py

The code should also be executed with sudo privileges. For example, to run the press_enter3.py script containing the above code, run the following command on the terminal.

sudo python3 press_enter3.py

Alternatively, the while-loop in the code above can be reduced to the following one-liner.