user3198516
user3198516

Reputation: 21

What are the other "codes" for the left and right keys in python?

while True:
    key = ord(getch())  

    if key == 72:
        print "up"
        time.sleep(1)

    elif key == 80:
        print "down."
        time.sleep(1)

I am searching for same kind of codes for arrow keys.

What are the codes for LEFT and RIGHT?

Upvotes: 2

Views: 3040

Answers (2)

svk
svk

Reputation: 5919

It seems like you are calling the _getch function which is provided in the msvcrt module on Windows platforms.

Note that the arrow keys are delivered as two values, that is, your values for UP and DOWN are wrong (alone, 72 is 'H' and 80 is 'P').

By complementing the documentation above with experimentation it seems like the answer may be:

  • UP is either 0 or 224, followed by 72.
  • DOWN is either 0 or 224, followed by 80.
  • LEFT is either 0 or 224, followed by 75.
  • RIGHT is either 0 or 224, followed by 77.

First call _getch once, and if it's either 0 or 224, call it again to get the actual key codes for the arrow keys.

Upvotes: 2

RickyA
RickyA

Reputation: 16029

while True:
    key = ord(getch()) 
    print(key)

and then press the keys you want to know.

Upvotes: 1

Related Questions