我想在没有输入"的情况下捕获单个用户按键,然后查看它是r"还是b"等,但特别是在带有 PyDev 的 Eclipse 中(Windows 7:64 位,Python 3.6.1).Python 从用户读取单个字符 当然.mrvcrt 似乎在 cmd.exe 中有效,但在 PyDev 中无效:
I want to capture a single user keypress without "enter", and see afterward whether it was 'r' or 'b' etc, but specifically in Eclipse with PyDev (Windows 7: 64bit, Python 3.6.1). Many nice alternatives are mentioned in Python read a single character from the user of course. The mrvcrt seems to work in cmd.exe but not in PyDev:
import msvcrt mych = msvcrt.getwch() print('You pressed: ' + mych)为什么不呢?我看到@MatthieuRiegler 已经在 在 Eclipse/PyDev 中使用 msvcrt.getch() 提出了这个问题 ...但我对任何有用的东西都持开放态度,不一定是mrvcrt.谢谢!
Why not? I see @MatthieuRiegler already asked this at Using msvcrt.getch() in Eclipse / PyDev ... but I am open to anything that works, not necessarily mrvcrt. Thanks!
推荐答案问题在于 PyDev/Eclipse 没有给你一个真正的终端(你的程序在没有真正"控制台的情况下启动它只是重定向输出).
The problem is that PyDev/Eclipse doesn't give you a real terminal (your program is launched without a 'real' console and it just redirects the outputs).
因此,另一种方法是检查您是否处于这种情况:
So, the alternative is checking whether you're in this scenario with:
import sys is_in_terminal = sys.stdin.isatty() if not is_in_terminal: entered = input() # input() on Py3, on Py2 it'd be raw_input() else: import msvcrt entered = msvcrt.getwch()唯一的问题是,如果它不在终端中,则内容只能在新行上供程序使用(因此,如果不按回车键,就无法获得该输出).
The only thing is that if it's not in a terminal, the contents are only available to the program on a new line (so, it's not really possible to get that output without him pressing enter).
请注意,虽然它需要在 Eclipse 中进行一些终端模拟,但可能有一个真正的"终端——例如 marketplace.eclipse/content/tcf-terminals -- 然后 PyDev 可以在这样的终端中启动程序而不是使用控制台视图......(但是这只是在想法世界中,没有截止日期,因此,不幸的是,目前无法在 PyDev/Eclipse 中不输入的情况下获取单个字符).
Note that having a 'real' terminal could be possible, although it'd require some terminal emulation inside Eclipse -- such as marketplace.eclipse/content/tcf-terminals -- and then PyDev could launch a program in such a terminal instead of using the console view... (but this is just in the ideas world, there's not due date for that, so, unfortunately, it's not currently possible to grab a single char without an enter inside PyDev/Eclipse).
更多推荐
Pydev/Eclipse 中的单字符输入
发布评论