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.
Asked
Active
Viewed 631 times
0
-
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
-
1There 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 Answers
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
-
How would I make the console wait for a key press (input doesn't work)? – zeldor Mar 02 '15 at 18:52
-
-
When I run the code it just prints `b'\xff'` without waiting for me to press something. – zeldor Mar 02 '15 at 18:57
-
Strange, works on my machine... Maybe that's behavior specific to Windows 8. I'm still on 7. – Kevin Mar 02 '15 at 19:03
-
Found the reason, I was using IDLE instead of the command prompt. Thanks anyway – zeldor Mar 02 '15 at 20:04