如何在python中获取按下的键而不按回车键?

发布于 2024-10-09 02:06:37 字数 144 浏览 3 评论 0原文

我看到这里一个解决方案,但我不想要等到按键被按下。我想按下最后一个键。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2024-10-16 02:06:37

相关问题可能对您有帮助,正如 @S.Lott 提到的: Detect in python which键已按下

我正在写信,不过是为了给你建议:别担心。
您想制作什么样的节目?
在终端上运行的程序通常没有一个可以让“实时”击键变得有趣的界面。现在不行。对于在终端中运行的程序,您应该担心有用的命令行用户界面,使用 optparse 或其他模块。

对于交互式程序,您应该使用 GUI 库并为您的用户创建一个像样的 UI,而不是重新发明轮子。对于您想要做的事情,哪一个更好?用户单击一个图标,屏幕上会打开一个窗口,上面有几个按钮,“文件”菜单下包含六个左右的菜单选项,就像屏幕上的所有其他窗口一样 - 或者 - 一个黑色终端打开具有 80 年代外观的文本界面,带有一些蓝色突出显示的菜单选项等等?您可以将 Tkinter 用于简单的窗口应用程序,因为它预装了 Python + Windows,因此您的用户不必担心安装其他库。

为了清楚起见,重新表述一下:任何需要用户界面的程序都应该有一个 GUI 库,或者有一个 WEB 界面。尝试创建通过终端操作的 UI 是浪费您和您的用户的时间 - 我们已经不再是 1989 年了。

如果您确实需要文本界面,那么您应该查看 ncurses 库。比尝试重新发明轮子更好。

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.

刘备忘录 2024-10-16 02:06:37

http://code.activestate.com/recipes/134892/

我认为这就是你所需要的

PS 哎呀,我没有看到这与您拒绝的解决方案相同...为什么,顺便说一句?

编辑:

你知道吗:

from msvcrt import getch

它只适用于 Windows,但是......
(并且在上面的链接中进行了概括)
从这里: http://www.daniweb.com/forums/thread115282.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文