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.

input1 = input("Press ENTER: ")  # use raw_input in python2
if input1 == "":
    # Captures situations where the input is just ENTER key
    print("You pressed ENTER.")
else:
    # If other characters are pressed before ENTER, it is captured here.
    print(f"You typed '{input1}' before pressing ENTER.")

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 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

# "pip install getkey" if getkey is not installed
from getkey import getkey, key
while True:
    # Get the pressed key
    var = getkey()
    # Print the pressed key
    print(var)
    if var == key.ENTER:
        # If the ENTER key is pressed, break the While loop.
        print("You pressed enter")
        break

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

from getch import getch
while True:
    char = getch()
    # print char that was pressed
    # for ENTER character nothing is displayed so we need to use repr() function
    print(char)
    # repr(char) returns a printable representation of "char"
    # ENTER has a printable value "\n" (newline character)
    print(repr(char))
    if char == "\n":
        # This captures "ENTER"
        break

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.

import msvcrt
while True:
    char = msvcrt.getch().decode("utf-8")
    print(char)
    # show printable representation of "char"
    # ENTER key is represented as "\r", the carriage return.
    print(repr(char))
    if char == "\r":
        break

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

# "python3 -m pip install pynput" if pynput is not installed.
from pynput.keyboard import Key, Listener
def show(key):
    # Print pressed key
    print("\nYou Entered {}".format(key))
    if key == Key.enter:
        # Release/ Stop listener if ENTER key is pressed
        return False
# Collect all events until released
with Listener(on_press=show) as listener:
    listener.join()

Method 5: Using the keyboard package

The keyboard package is not inbuilt in 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

import keyboard
while True:
    # Print pressed keys
    print(keyboard.read_key())
    if keyboard.is_pressed("enter"):
        # Break if ENTER key is pressed
        print("You pressed Enter")
        break

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.

# import keyboard
# # wait until ENTER is pressed
# keyboard.wait("enter")