non-blocking keyboard input from windows not working on PyCharm ?
已回答
I'm trying to run/debug a very simple non-blocking keyboard input from PyCharm (2020.3), but for some reason it is not working.
Env: Windows 10 Pro/10.0.19042, CPython 3.9
import msvcrt
while True:
if msvcrt.kbhit():
print("Key pressed: %s" % msvcrt.getch())
It works fine in the PyCharm-Terminal and everywhere else including VS and VSC !!
Anything I missed to make it work ?
请先登录再写评论。
A quick workaround was to use https://github.com/boppreh/keyboard
But i'm still interested in understanding why the initial does not works ;-)
Hi Jesper,
You need to enable Emulate terminal in output console option in the run configuration used to execute your code (Run | Edit Configurations).
Thanks .. for the tips with "Emulate ..."
Regarding the quick workaround .. It is skipped because of the Global keyboard hook, that generates keyboard event even with the running script (terminal/command Prompt) out of scope (focus).
Why is it working with this 'Emulate terminal in output console' ? and not without .. What is going on behind the 'scene' ?
In VSC it works perfectly without such setting.
VSC basically opens a command line and runs a given script in it. PyCharm instead starts a Python process without touching a terminal by default. The option tells IDE to start the process with a pseudo terminal.