Note: Since you request Python 3.x I will use input()
. For Python 2.x use raw_input()
.
In case you want to check a single time you can do
# Request a key to be pressed by the user
res = input("Waiting for 'w' to be pressed...")
if res == "w":
# Do something if w was pressed
else:
# Do something if other then w was pressed
# Code after check
The downside of this is that your program execution will continue after the check is completed regardless of whether you are satisfied with retrieved key value or not.
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 by another iteration of the loop
# Code after check
By doing that the execution of the program will be stuck inside the endless loop and will keep asking for your desired key to be pressed until the user kills the application or fulfills the requirement.