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)
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!
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).
Note that having a 'real' terminal could be possible, although it'd require some terminal emulation inside Eclipse -- such as https://marketplace.eclipse.org/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).