How to get in python the key pressed without press enter?

放肆的年华 提交于 2019-12-09 13:39:59

问题


I saw here a solution, but i don't want wait until the key is pressed. I want to get the last key pressed.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4501700/how-to-get-in-python-the-key-pressed-without-press-enter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!