1

How do I use ord() to catch an arrow key press in Mac and Linux? I know you can use the msvcrt in Windows. They don't have a character associated with them.

jww
  • 97,681
  • 90
  • 411
  • 885
Yello Four
  • 227
  • 1
  • 4
  • 17
  • http://stackoverflow.com/questions/22397289/finding-the-values-of-the-arrow-keys-in-python-why-are-they-triples – Ayy Aug 07 '15 at 19:15
  • I specifically need to use ord() ... – Yello Four Aug 07 '15 at 20:31
  • It's not possible using ord. Ord returns an unicode value of a character, the arrow keys don't have any character bound to them. – Ayy Aug 07 '15 at 20:38
  • But they do ... as the answer below indicates... it just happens to be more than one character. And it seems to be a part of a special type of character list, not part of ASCII – Yello Four Aug 07 '15 at 20:41
  • Use 8456 or wasd instead of the arrow keys. And the code below isn't working for me. – Ayy Aug 07 '15 at 20:50
  • Well I guess you could use wasd ... I was asked to get arrow keys working so... – Yello Four Aug 07 '15 at 20:52
  • 2
    Possible duplicate of [Finding the Values of the Arrow Keys in Python: Why are they triples?](https://stackoverflow.com/q/22397289/608639) – jww Jun 02 '18 at 17:47

1 Answers1

0

Are you talking about in the console?

I have a hacky solution if you want to avoid msvcrt.

    value = raw_input("Hit left arrow key then ENTER: ")
    print ord(value[0]), ord(value[1]), ord(value[2])
    if ord(value[0]) == 27 and ord(value[1]) == 91 and ord(value[2]) == 68:
        print "You hit the left key!"
    elif ord(value[0]) == 27 and ord(value[1]) == 91 and ord(value[2]) == 67:
        print "You hit the right key!"

This is written to illustrate how it's done. You can easily condense this into a more consice system depending on what you need. You can use ord -- just need to remember that it's a multi-character string that you need to analyze character by character.

shark3y
  • 166
  • 6
  • Is there a way that I can avoid hitting the enter key? That would like solve all of the issues I'm having right now. I am actually using this for a GUI that I am making with wxPython, but the thing they use to catch a keypress doesn't work on a Mac....soooo this is me trying to do it manually. That's why I need to avoid hitting the enter key since I can't really be using the terminal or console :((( But you deserve upvote for this nice workaround anyway :) – Yello Four Aug 07 '15 at 19:38
  • Do you get a keyCode at all for pressing those keys in your program? Or does it just do nothing when you press those keys? – shark3y Aug 07 '15 at 20:28
  • Did you post the question for wxPython? Cause the idea that NO event is triggered for a EVT_KEY_DOWN on the arrow keys is problematic. You should definitely consider testing it on other systems to eliminate some variables from the situation. – shark3y Aug 07 '15 at 21:57
  • I didn't post it yet no ... I guess I will – Yello Four Aug 07 '15 at 23:29