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.
-
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– Kevin2015-03-02 18:24:03 +00:00Commented Mar 2, 2015 at 18:24
-
possible duplicate of Detect key input in PythonBhargav Rao– Bhargav Rao2015-03-02 18:24:41 +00:00Commented Mar 2, 2015 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– zeldor2015-03-02 18:25:21 +00:00Commented Mar 2, 2015 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– Shawnic Hedgehog2015-03-02 18:33:44 +00:00Commented Mar 2, 2015 at 18:33
Add a comment
|
1 Answer
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.
5 Comments
zeldor
How would I make the console wait for a key press (input doesn't work)?
Kevin
getch waits for a keypress.zeldor
When I run the code it just prints
b'\xff' without waiting for me to press something.Kevin
Strange, works on my machine... Maybe that's behavior specific to Windows 8. I'm still on 7.
zeldor
Found the reason, I was using IDLE instead of the command prompt. Thanks anyway