non-blocking keyboard input from windows not working on PyCharm ?

Answered

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 ?

5 comments
Comment actions Permalink

A quick workaround was to use https://github.com/boppreh/keyboard
But i'm still interested in understanding why the initial does not works ;-) 

0
Comment actions Permalink

Hi Jesper,

You need to enable Emulate terminal in output console option in the run configuration used to execute your code (Run | Edit Configurations).

1
Comment actions Permalink

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).

0
Comment actions Permalink

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.

 

0
Comment actions Permalink

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.

0

Please sign in to leave a comment.