0

This is probably pretty simple, but I can't find the answer anywhere. I basically want some code that detects different key presses (enter, left etc) and then saves the type of key in a variable.

zeldor
  • 101
  • 1
  • 9
  • What OS are you using? Does it need to detect all key presses everywhere, or just ones that occur when your application has keyboard focus? – Kevin Mar 02 '15 at 18:24
  • possible duplicate of [Detect key input in Python](http://stackoverflow.com/questions/17815686/detect-key-input-in-python) – Bhargav Rao Mar 02 '15 at 18:24
  • I am using Windows 8, and just ones that occur when my program asks for a key press (result must differ depending on key). I don't want a new pop-up window, just keep it in the command prompt. – zeldor Mar 02 '15 at 18:25
  • 1
    There are a few libraries that support keyboard input like Tkinter and PyGame. Have you checked them out? Might add a little too much functionality than you probably want though. – Shawnic Hedgehog Mar 02 '15 at 18:33

1 Answers1

1

If you're on Windows and only want to get input when your window has keyboard focus, you can use msvcrt.getch:

Read a keypress and return the resulting character. Nothing is echoed to the console.

Kevin
  • 74,910
  • 12
  • 133
  • 166