I saw here a solution, but i don't want wait until the key is pressed. I want to get the last key pressed.
-
You can always search, since it's been asked before. All of these are related. http://stackoverflow.com/search?q=[python]+keypress – S.Lott Dec 21 '10 at 16:55
-
possible duplicate of [Detect in python which keys are pressed](http://stackoverflow.com/questions/694296/detect-in-python-which-keys-are-pressed) – S.Lott Dec 21 '10 at 16:56
-
I searched, but I didn't found anything. – pythonFoo Dec 21 '10 at 16:59
-
I looked at your link: pyKeylogger is too complex for me, and pyHook is only for windows (I use linux). – pythonFoo Dec 21 '10 at 17:05
2 Answers
The related question may help you, as @S.Lott mentioned: Detect in python which keys are pressed
I am writting in, though to give yu advice: don't worry about that. What kind of program are you trying to produce? Programas running on a terminal usually don't have an interface in which getting "live" keystrokes is interesting. Not nowadays. For programs running in the terminal, you should worry about a usefull command line User Interfase, using the optparse or other modules.
For interative programs, you should use a GUI library and create a decent UI for your users, instead of reinventing the wheel.Which wouldb eb etter for what you ar trying to do? Theuser click on an icon,a window opens on the screen, witha couple of buttons on it, and half a dozen or so menu options packed under a "File" menu as all the otehr windws on the screen - or - a black terminal opens up, with an 80's looking text interface with some blue-highlighted menu options and so on?. You can use Tkinter for simple windowed applications, as it comes pre-installed with Python + Windows, so that yoru users don't have to worry about installign aditional libraries.
Rephrasing it just to be clear: Any program that requires a user interface should either se a GUI library, or have a WEB interface. It is a waste of your time, and that of your users, to try and create a UI operating over the terminal - we are not in 1989 any more.
If you absolutely need a text interface, you should look at the ncurses library then. Better than trying to reinvent the wheel.
-
-
It can be to launch a complex task (which last for instance 1h) from the terminal. I want to be able to interrupt and save the progress it when the user press Q. Ctrl+C won't work because the progress won't be saved and the interruption eventually corrupt the progress made. – Conchylicultor Aug 20 '16 at 09:41
http://code.activestate.com/recipes/134892/
i think it's what you need
ps ooops, i didn't see it's the same solution you rejected...why, btw?
edit:
do you know:
from msvcrt import getch
it works only in windows, however... (and it is generalised in the above link) from here: http://www.daniweb.com/forums/thread115282.html

- 5,151
- 2
- 26
- 43