raw_input 无需按 Enter 键
我在 Python 中使用 raw_input
与 shell 中的用户交互。
c = raw_input('Press s or n to continue:')
if c.upper() == 'S':
print 'YES'
它按预期工作,但用户必须在按“s”后在 shell 中按 Enter。有没有一种方法可以通过用户输入完成我需要的操作,而无需在 shell 中按 Enter 键?我正在使用 *nixes 机器。
I'm using raw_input
in Python to interact with user in shell.
c = raw_input('Press s or n to continue:')
if c.upper() == 'S':
print 'YES'
It works as intended, but the user has to press enter in the shell after pressing 's'. Is there a way to accomplish what I need from an user input without needing to press enter in the shell? I'm using *nixes machines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
实际上,与此同时(距本线程开始近 10 年)有一个名为 pynput 的跨平台模块出现了。
在第一个剪切下方 - 即仅适用于小写的 's'。
我已经在 Windows 上测试过它,但我几乎 100% 肯定它应该可以在 Linux 上运行。
Actually in the meantime (almost 10 years from the start of this thread) a cross-platform module named pynput appeared.
Below a first cut - i.e. that works with lowercase 's' only.
I have tested it on Windows but I am almost 100% positive that it should work on Linux.
在 Windows 下,您需要
msvcrt
模块,具体来说,从您描述问题的方式来看,函数 msvcrt.getch:(等等——请参阅我刚刚指出的文档)。对于 Unix,请参阅此食谱 用于构建类似的
getch
函数的简单方法(另请参阅该配方的评论线程中的几个替代方案 &c)。Under Windows, you need the
msvcrt
module, specifically, it seems from the way you describe your problem, the function msvcrt.getch:(etc -- see the docs I just pointed to). For Unix, see e.g. this recipe for a simple way to build a similar
getch
function (see also several alternatives &c in the comment thread of that recipe).Python 不提供开箱即用的多平台解决方案。
如果您使用的是 Windows,您可以尝试 msvcrt :
Python does not provide a multiplatform solution out of the box.
If you are on Windows you could try msvcrt with:
curses 也可以做到这一点:
curses can do that as well :
适用于类 Unix 操作系统(包括 Linux)的标准库解决方案:
其工作原理是在从终端读取数据之前将终端置于非规范输入模式。
不回显用户输入的替代解决方案(例如,如果用户按 z,
z
将不会出现在屏幕上):使用示例:
Standard library solution for Unix-like operating systems (including Linux):
This works by putting the terminal into non-canonical input mode before reading from the terminal.
Alternative solution that does not echo the user's input (e.g. if the user presses z,
z
will not appear on screen):Usage example:
为了获得单个字符,我使用了 getch,但我不知道它是否适用视窗。
To get a single character, I have used getch, but I don't know if it works on Windows.
使用 readchar:
https://pypi.org/project/readchar/ 到网页
use readchar:
https://pypi.org/project/readchar/ to webpage
除了
msvcrt
模块,您还可以使用 WConio:Instead of the
msvcrt
module you could also use WConio:附带说明一下,msvcrt.kbhit() 返回一个布尔值,确定当前是否按下了键盘上的任何键。
因此,如果您正在制作游戏或其他内容,并且希望按键执行某些操作但又不想完全停止游戏,则可以在 if 语句中使用 kbhit() 以确保仅在用户确实想要执行某些操作时才检索密钥。
Python 3 中的示例:
On a side note, msvcrt.kbhit() returns a boolean value determining if any key on the keyboard is currently being pressed.
So if you're making a game or something and want keypresses to do things but not halt the game entirely, you can use kbhit() inside an if statement to make sure that the key is only retrieved if the user actually wants to do something.
An example in Python 3:
我知道这已经很旧了,但解决方案对我来说还不够好。
我需要支持跨平台并且无需安装任何外部Python包的解决方案。
我的解决方案,以防其他人遇到这篇文章
参考: https://github.com/unfor19/mg-tools/blob/master/mgtools/get_key_pressed.py
I know this is old, but the solution wasn't good enough for me.
I need the solution to support cross-platform and without installing any external Python packages.
My solution for this, in case anyone else comes across this post
Reference: https://github.com/unfor19/mg-tools/blob/master/mgtools/get_key_pressed.py
如果您可以使用外部库,
blessed
(跨平台)可以做到做到这一点很容易:请注意,在
with
块内时,终端的行编辑功能将被禁用。cbreak
的文档,inkey< /code>
,以及示例 与
inkey
。If you can use an external library,
blessed
(cross platform) can do do this quite easily:Note that while inside the
with
block, line editing capabilities of the terminal will be disabled.Documentation for
cbreak
,inkey
, and an example withinkey
.