How to Make Python Wait for Keypress

3 Best Ways On How to Make Python Wait for Keypress

This article will guide you on how to make Python wait for keypress using specific methods.

If you’ve used Python from time to time, you have surely noticed that using it feels like playing the “input game.”

If you are not aware of this yet, it is worth knowing about.

The input game is used to test a user interface (UI). It involves asking the user to enter a series of characters.

Every time the user fails to enter the correct character after three consecutive failures, an error message appears, and the program waits for a keypress.

In most programming languages, you can specify that code should wait for a key to be pressed before continuing.

This is known as key scheduling. In Python, the standard library provides no built-in facilities for doing so.

How to Make Python Wait for Keypress

To make python wait for keypress use the input() method in Python. Example: userInput = input(“Press Enter to continue: “)

However, it’s relatively simple to add support for your own program if you need it – anywhere from easy to moderately complex depending on the details of your program and what features you require.

In this blog post, you’ll explore how you can make Python wait for specific input before proceeding with execution.

1. Using the input() Function to Make Python Wait for Keypress

The most naive and straightforward way to make Python wait for a keypress is using the input() function.

The input() function takes input from users and works further. This function can help you to keep your Python program waiting until a keypress.

With the help of this function, you can hold your Python script’s execution until the user press a particular key.

Please note: Before the Python 3.0 version, the input() function was named as raw_input() function.

#Regular print function
print("Print on the first priority!")
userInput = input("Press Enter to continue: ")
# It pauses the script here until the user press any key.
print("Printing after having the input!")


Once you hit the run button, the above script prints immediately-

Print on the first priority!
Press Enter to continue:

And the program keeps waiting here until it gets input from the user. Once it receives the input, it prints immediately:

Printing after having the input!

2. Using the getch() Function to Make Python Wait for Keypress

For Linux and macOS, Python has an outstanding option to keep Python waiting for a keypress.

The getch() function lets you take input from the user and keep the input hidden while typing.

The getch() function reads a single char from stdin and returns it once you press any button from the keyboard.

from getch import getch
while True:
character = getch() #read the pressed key
if character == chr(27): # Value of escape key

Note: Windows OS has the same function named msvcrt, which works similarly to the getch() function.

3. Using the keyboard Module to Make Python Wait for Keypress

The keyboard module doesn’t come with the standard Python library. However, you can install it using the pip or anaconda package command.

This keyboard module enables you to create a function that can halt your Python program’s execution until you press a specific key.

For instance, you want to keep your Python program waiting until the user presses the enter key.

Please note: The keyboard module identifies your keys and works as per your command.

#importing the keyboard module.

import keyboard
def myFunction():
    while True:
  if keyboard.read_key() == "enter":
  # If you press the Enter key, the program will resume
print("Printed before myFunction's execution!")
print("Printed after myFunction's execution!")


Printed before myFunction’s execution!
Printed after myFunction’s execution! (Once you hit the enter key)

Using the os Module to Make Python Wait for Keypress

The os module is not platform-independent. Therefore, it is not a widely used method.

However, if you are a Windows OS user, you can have the taste explicitly. The os module has an in-built function system(“pause”) that keeps Python waiting.

import os
print("This line was printed immediately!")
print("This line was printed after the function!")


This line was printed immediately!
This line was printed after the function! (Once you press any key)