1

The question pretty much says everything. I've seen answers to similar questions, but not this question precisely, and it seems I need a more precise answer. Keep in mind that I am relatively new to programming and won't really understand any high walls of code you put up for me. Please keep it as utterly simple as possible. :(

All I know is that this was very simple (basic?) to do in BASIC, the one language wherein I have some experience.

Remember, I need a specific key pressed, not just any key.

I'm on a Mac, BTW.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

4 Answers4

4

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

Serial
  • 7,925
  • 13
  • 52
  • 71
  • Thanks! But if that works, what makes a more complicated way better? – Robert Aronson May 21 '13 at 00:34
  • well i thought you wanted it to be just a key press on w intead of w then enter or just enter but if that works awesome!!! come to think of it the only other options you have to import a module so yeah that should work fine and if it worked you can except my answer – Serial May 21 '13 at 00:35
  • Actually, just tested it: it doesn't work. It just types a "w" on the screen and then nothing happens. I get: `Press ww Traceback (most recent call last): File "/Users/robertaronson/Desktop/helloworld.py", line 1, in x = input("Press w") File "", line 1, in NameError: name 'w' is not defined` – Robert Aronson May 21 '13 at 00:41
  • there is no reason it shouldnt you should just type w and then hit enter are you making a game of some sort or what – Serial May 21 '13 at 03:02
  • so like a basic like text based or are you using pygame because pygame makes it easy to use key commands – Serial May 21 '13 at 14:20
  • I'm not using pygame. I just looked at Python for the first time two days ago. – Robert Aronson May 22 '13 at 23:41
  • oh well then yeah the only way to do it without any libraries is using keylisteners – Serial May 22 '13 at 23:47
3

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?

Community
  • 1
  • 1
Ford
  • 2,559
  • 1
  • 22
  • 27
0

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.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
0

I like to use Pynput, I think you can have simpler and more elegant solutions.

Many options in the official documentation: https://pypi.org/project/pynput/

Command:

pip install pynput

Code:

from pynput import keyboard

def on_activate_a():
    print('A pressed')

def on_activate_b():
    print('B pressed')

def on_activate_c():
    print('C pressed')

def quit():
    print('QUIT')
    h.stop()

with keyboard.GlobalHotKeys({
        'a': on_activate_a,
        'b': on_activate_b,
        'c': on_activate_c,
        '<ctrl>+c': quit}) as h:
    h.join()