How to make a python script wait for user input?

Sometimes it is very useful to alert the user to press any key before proceeding ahead in the program. In this Python tutorial, we will discuss how to make a python script wait for a pressed key?

Table Of Contents

Method 1 : Using the input() prompt

First and the easiest method that we can use to make a python script wait for user to press a key is the input() method. It is a built-in python method, that ca be used to take user input from the command line. Here, we will not take any value nor store it in any variable. See the example code below:

CODE :

print("Program Started")

# this will wait for any user input
input("Press any key to continue>>>")

print(' Program completed')

OUTPUT :

Advertisements
Program Started
Press any key to continue>>>
Program completed

In the code above, we printed the first line then asked for a input. Once an input is provided by the user, then we completed the program.

Method 2 : Using the Keyboard package

Another method we can use to make a python script wait for a user input is by using the keyboard module. This module doesn’t comes bundled with python, so you need to install it using pip. Type: pip install keyboard in the command line. This module allows user to take fill control of keyboard. It listens and send keyboard events, and many other useful methods and functions. See the example code and output below,

CODE :

import keyboard

print('Program initiated')

print('Press enter to Continue...')

# waits until you press enter
keyboard.wait('enter')

print('Program Completed')

OUTPUT :

Program initiated
Press enter to Continue...
Program Completed

In the example, the keyboard module has been used to make a python script wait for a pressed key. Here we have used keyboard.wait() function, and this function blocks the execution of the code until a key is pressed.

Method 3 : Using os.system()

Another method that we can use to make a python script wait for a pressed key is the system() function of the os module. This module comes bundled with the Python Programming Language, and is used to interact with system or give system commands to our Operating System. Here we will os.system() which is used to execute system commands in a subshell. Here we will use pause command to pause the programs. See the example code below:

CODE :

import os
print('Program initiated')

# giving the pause command to terminal using os.system()
os.system('pause')

print('Program Completed')

OUTPUT :

Program initiated
Press any key to continue . . .
Program Completed

In this example, we have used the os.system('pause') to make a python script wait for a pressed key. When ‘pause’ is executed in the command prompt, it prompts Press any key to continue. When any key is pressed the program continues.

Method 4 : Using msvcrt / getch package

Another method that we can use to make a python script wait for a pressed key is the getch() function of msvcrt module. This module of Python programming language, has special access to the number of advanced function in the Microsoft Visual C/C++ runtime library. Here we will be using mscvrt.getch() which reads key press and returns the character as the byte string. Thats why we need to use decode() function to covert byte string objects. See the example code below,

CODE :

import msvcrt

print('Program initiated')

while True:
    print('Press Esc to continue>>>>')

    # char stores the key pressed that has been decoded by the decode() function and captured by getch().

    char = msvcrt.getch().decode("utf-8")

    # 27 is the value of esc
    if char == chr(27): 
        print('Program Completed')
        break

OUTPUT :

Program initiated
Press Esc to continue>>>>
Program Completed

In the above example, we have used the getch() function of mscvrt module along with decode function to make a python script wait for a key pressed. The var char stores the decoded value of key pressed which has been captured by the getch() function and decoded by the decode() function. Then if statement is used to check against the ASCII value of Escape key against the char variable. If both are same then the program breaks.

Summary

Today we learned, how to make a python script wait for a key pressed. We also learned about four different methods using which you can make a python script wait for a key pressed. Most easy to understand and convenient to use is method 1 which uses the input method, in which without importing any module or capturing and decoding of key binding you can simply press any key to continue in the program. If working with OS module, then os.system() can also be used. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top