How do I wait for a pressed key?
Here is an example of how you might wait for a key press in Python:
import msvcrt
def wait_for_key():
while True:
if msvcrt.kbhit():
return msvcrt.getch()
print("Press any key to continue...")
key = wait_for_key()
print("You pressed: ", key)
This uses the msvcrt
module, which provides functionality for interacting with the Windows console. The kbhit()
function is used to check if a key has been pressed, and if one has, the getch()
function is used to retrieve the pressed key.
Watch a video course
Python - The Practical Guide
Alternatively, you can use input()
function to wait for key press and input together like this
key = input("Press any key to continue...")
Keep in mind that the msvcrt
module is only available on Windows and input()
function works on all platforms.