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...")
In Python 3.3.2 how do I have the code wait for the user to press the "w" key? - Stack Overflow
Have program wait for keypress to start (while not in focus in command prompt)?
How so I wait for a keystroke?
How to detect keypress in Python? Without wait the user to press any key
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
the easiest way is:
x = input("Press w")
if x == "w":
#Code
or you can use enter and get rid of the if statment:
input("Press Enter to Continue...")
#Code
there are better ways but you ask for something simple
What you want is a key listener. input and raw_input will only return when they see an EOL or EOF character (e.g. you hitting the enter key). I assume you're making some sort of game (because you want to accept particular keys)?
In that case, you want a keylistener, which would make this relevant: Key Listeners in python?
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!