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)).
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)).
In Python 3, use input():
input("Press Enter to continue...")
In Python 2, use raw_input():
raw_input("Press Enter to continue...")
How so I wait for a keystroke?
Jupyter Python Script waiting keyboard input without Enter strike
python - raw_input without pressing enter - Stack Overflow
Have program wait for keypress to start (while not in focus in command prompt)?
Videos
I want to make a program that waits for the user to press a key and then acts based on what key was pressed. The only way I found was to make a loop that checks if a key is pressed, but I want the program to halt and wait for the keystroke rather than repeat.
Edit: To clarify, the program doesn't do anything while it waits. It just waits as if I used the input() method.
Edit2: I've been given a solution. Thank you to everyone.
PS.: New here so sorry if I've asked in a strange way or something of the sorts
Actually in the meantime (almost 10 years from the start of this thread) a cross-platform module named pynput appeared. Below a first cut - i.e. that works with lowercase 's' only. I have tested it on Windows but I am almost 100% positive that it should work on Linux.
from pynput import keyboard
print('Press s or n to continue:')
with keyboard.Events() as events:
# Block for as much as possible
event = events.get(1e6)
if event.key == keyboard.KeyCode.from_char('s'):
print("YES")
Under Windows, you need the msvcrt module, specifically, it seems from the way you describe your problem, the function msvcrt.getch:
Read a keypress and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed.
(etc -- see the docs I just pointed to). For Unix, see e.g. this recipe for a simple way to build a similar getch function (see also several alternatives &c in the comment thread of that recipe).
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!
Howdy,
Do you know if there is a way to accept user's input without pressing enter?
I am trying to exit a while loop by pressing the key "q" but with the input() function I have to press enter.
I would like to use standard Python modules (if possible) and not install anything else with pip.
Thank you!
Edit: I'm using Python3.8 on Unix