snippetpythonCriticalCanonical
How do I wait for a pressed key?
Viewed 0 times
keyhowforpressedwait
Problem
How do I make my python script wait until the user presses any key?
Solution
In Python 3, use
In Python 2, use
This only waits for the user to press enter though.
On Windows/DOS, one might want to use
This should wait for a key press.
Notes:
In Python 3,
In Python 2,
input():input("Press Enter to continue...")In Python 2, use
raw_input():raw_input("Press Enter to continue...")This only waits for the user to press enter though.
On Windows/DOS, one might want to use
msvcrt. The msvcrt module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime Library (MSVCRT):import msvcrt as m
def wait():
m.getch()This should wait for a key press.
Notes:
In Python 3,
raw_input() does not exist.In Python 2,
input(prompt) is equivalent to eval(raw_input(prompt)).Code Snippets
input("Press Enter to continue...")raw_input("Press Enter to continue...")import msvcrt as m
def wait():
m.getch()Context
Stack Overflow Q#983354, score: 865
Revisions (0)
No revisions yet.