In Python 3, use 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)).

Answer from riza on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ make-python-wait-for-a-pressed-key
Make Python Wait For a Pressed Key - GeeksforGeeks
July 23, 2025 - The input function presenting the standard Python distribution could serve the purpose. But the function is only limited to the Enter key, i.e., It requires the Enter key to be pressed for it to return. The following example demonstrates the working: Now the code, upon execution, halts the execution of the program and waits for the Enter keypress.
Discussions

In Python 3.3.2 how do I have the code wait for the user to press the "w" key? - Stack Overflow
In case you want to wait - as in do not continue until condition fulfilled - for the specific key you can do ยท while True: res = input("Waiting for 'w' to be pressed...") if res == "w": # Do something if w was pressed and exit loop break else: # Do something if other then w was pressed # followed ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Have program wait for keypress to start (while not in focus in command prompt)?
Time.sleep() might be useful here. If you pend on input, and place it within a while loop, along with time.sleep(), then you can check if any input is provided or sleep the foreground process for a second. More on reddit.com
๐ŸŒ r/learnpython
12
1
November 20, 2021
How so I wait for a keystroke?
Contrary to what has been answered so far it is actually super simple to do what you describe. If I have not misunderstood you, you basically want to create some sort of hotkey functionality. Basically execute some code whenever button x is pressed on the keyboard. You can utilize the module keyboard or alternatively pynput for that. Keep in mind that you may need to run the script with admin privileges. Below is a quick example that triggers a function when the spacebar is hit. import keyboard def hotkey_callback(): print("Space was pressed!") keyboard.add_hotkey('space', hotkey_callback) # Stops the program when you hit esc keyboard.wait('esc') More on reddit.com
๐ŸŒ r/learnpython
16
2
September 14, 2023
How to detect keypress in Python? Without wait the user to press any key
Use the kbhit function. from msvcrt import getch, kbhit while True: if kbhit(): # returns True if the user has pressed a key key = ord(getch()) if key == 13: # etc More on reddit.com
๐ŸŒ r/learnpython
2
1
May 26, 2020
๐ŸŒ
Pierian Training
pieriantraining.com โ€บ home โ€บ how to wait for a keypress in python
How to Wait for a Keypress in Python - Pierian Training
April 28, 2023 - One way to wait for a keypress in Python is by using the `getch()` function from the `keyboard` module. The `getch()` function waits for a single keypress and returns the character representation of the key that was pressed.
๐ŸŒ
W3docs
w3docs.com โ€บ python
How do I wait for a pressed key? - 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)
Find elsewhere
๐ŸŒ
Linux Hint
linuxhint.com โ€บ python_pause_user_input
Python Pause For User Input โ€“ Linux Hint
A bye message is printed after pressing any key. sleep() method can be used to pause the user input for a certain period of time. In the following script, a simple addition task is given for the user. sleep() method is used here to wait for the user for 5 seconds before typing the answer.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-make-a-command-to-wait-for-the-user-to-press-ENTER-in-Python
How to make a command to wait for the user to press 'ENTER' in Python - Quora
Answer (1 of 6): Is not input command 1.10. Input and Output already available. input(โ€œWhat ever text as prompt you wantโ€) and the console will wait for you to press enter.
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ programming โ€บ python
Detecting Key Press No Window or Wait - Raspberry Pi Forums
I sat thinking for 5 minutes on what to put here. Finally I put something like this. Check out my github page @ https://github.com/ankith26 ... from pynput import keyboard import time a = "" def on_press(key): global a try: a=key.char except AttributeError: pass listener = keyboard.Listener( on_press=on_press) listener.start() while True: try: PUT YOUR CODE RIGHT HERE except KeyboardInterrupt: break listener.stop() print("stop")
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ wait for key press in python
Wait for Key Press in Python - CodeSpeedy
January 18, 2024 - Exiting...".format(timeout_duration)) return False if not wait_for_key_press(10): exit(0) # Exit the program if no key was pressed within the timeout ยท Run this program to see the output. It will wait for the specific mentioned time to take input from the user.
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ how_do_i_wait_for_a_pressed_key
How do I wait for a pressed key?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
PythonForBeginners.com
pythonforbeginners.com โ€บ home โ€บ how to detect keypress in python
How to Detect Keypress in Python - PythonForBeginners.com
June 30, 2023 - Hence, the wait() method halts the execution of the program until the user presses the key โ€œaโ€. Once the correct key is pressed, the execution of the program moves ahead and the print statement is executed.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ have program wait for keypress to start (while not in focus in command prompt)?
r/learnpython on Reddit: Have program wait for keypress to start (while not in focus in command prompt)?
November 20, 2021 -

Hi!

I have a python script that I don't want to start until a key is pressed (ideally a specified key, but if not, any key). I cannot use the Keyboard module because the program will not support multithreading, and the keyboard.wait function requires multithreading to work unfortunately.

None of the solutions I googled have worked so far.

I've tried:

    import os
    os.system("pause")

Also this:

import msvcrt as m
def wait():
    m.getch()

And of course this:

input()

I think part of the issue is I'm running the program in Pycharm, so maybe it doesn't register these? Although I do plan on compiling into an exe so maybe it won't be an issue when the app is distributed, I'd like for some kind of wait key that works in Pycharm so that I can run it during debugging etc. without having to worry about compiling it first.

Can anyone help? I'm at wits end trying to solve what I think should be an easy problem to solve!

๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ top-10-ways-to-wait-for-key-press-in-python
Top 10 Ways to Wait for a Key Press in Python - sqlpey
December 5, 2024 - For those utilizing Python 2, the equivalent to input() is raw_input(). This function behaves similarly but is specifically meant for strings. ... For a more sophisticated approach on Linux, you can define a function that captures a single keypress without spinning in a loop. Hereโ€™s how you can implement it: import termios import fcntl import sys import os def wait_for_keypress(): fd = sys.stdin.fileno() old_flags = fcntl.fcntl(fd, fcntl.F_GETFL) old_attrs = termios.tcgetattr(fd) new_attrs = old_attrs.copy() new_attrs[3] &= ~(termios.ICANON | termios.ECHO) termios.tcsetattr(fd, termios.TCSANOW, new_attrs) try: c = sys.stdin.read(1) return c finally: termios.tcsetattr(fd, termios.TCSANOW, old_attrs) fcntl.fcntl(fd, fcntl.F_SETFL, old_flags)
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ detect keypress in python
Detect keypress in Python - Java2Blog
June 12, 2021 - The kbhit() function waits for a key to be pressed and returns True when it is pressed. The getch() function returns the key pressed in a byte string. Notice the b in the output. The break statement will come out of this code block.
๐ŸŒ
Iditect
iditect.com โ€บ programming โ€บ python-example โ€บ make-python-wait-for-a-pressed-key.html
Make Python Wait For a Pressed Key
import keyboard print("Press any key to continue...") keyboard.wait('any') # Waits for any key press
๐ŸŒ
iCert Global
icertglobal.com โ€บ home โ€บ community โ€บ what is the most reliable way to make a python script wait for a specific key press to continue?
What is the most reliable way to make a Python script wait for a specific key press to continue? | iCert Global
For Windows, you can use the built-in msvcrt module, specifically msvcrt.getch(), which reads a single key press without echoing it to the screen. On Linux or macOS, you typically need to interface with the termios and tty modules to put the terminal in raw mode. However, for a cross-platform solution, the keyboard library is excellent; you can simply use keyboard.wait('space').
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [deleted by user]
[deleted by user] : r/learnpython
November 8, 2017 - See here for some examples: https://stackoverflow.com/questions/1394956/how-to-do-hit-any-key-in-python ... That works on Windows but not *nix. I'm not sure if there is a cross platform method. ... Install this package. ... But with no argument I believe it just waits for any key.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-17568.html
Keypress when running a python app in a console on windows
April 16, 2019 - Hi all, I have a small python program running for the windows command window (console app) I want to detect a keypress when the console app has not got focus. I tried several ways to do this but it never seems to work. Steve