peterbrittain / asciimatics

A cross platform package to do curses-like operations, plus higher level APIs and widgets to create text UIs and ASCII art animations
Apache License 2.0
3.64k stars 238 forks source link

getting a KEY_ESC event when resizing pycharm terminal #332

Closed eyaler closed 3 years ago

eyaler commented 3 years ago

widnows 10, asciimatics 1.13.0, python 3.9.5

I am running Screen.wrapper in pycharm (either by using Emulate terminal option OR by using the terminal tab and running python.exe). After resizeing the terminal window a few times, i get a KEY_ESC keyboard event. that is a KeyboardEvent event with key_code=-1. I am not sure if this is a pycharm or asciimatics issue, however i was not able to get such an escape key event on resize when not using asciimatics.

debugging further i added in screen.py:

event = self._stdin.ReadConsoleInput(1)[0]
                if event.EventType == win32console.KEY_EVENT:
                    print(ord(event.Char)) ### added for dubug

add i am seeing 13 followed by 27 on these resizes. however if i just run input() in the terminal i do not seem to get such key events.

peterbrittain commented 3 years ago

Hmmm... Kinda struggling with this one... All asciimatics does is read the input queue and convert it. The fact that it is actually returned by ReadConsoleInput means something is injecting that input - either pycharm or the user (e.g. partial keypress that pycharm routes to its terminal).

Can you reproduce in a normal console window? If not, I think this is a pycharm bug...

eyaler commented 3 years ago

i could not reproduce on a normal console. maybe its a pycharm windows api issue. also if we are on the subject of pycharm terminal... automatic scroll to start on screen.refresh does not work when the terminal is docked and also the height is sometimes wrong when the terminal is docked (so line y=0 may not be in view). undocking the terminal fixes this. i am not sure if you want to deal with these things... if so i can open new issues

peterbrittain commented 3 years ago

Sorry, but the pycharm terminal is not a pure Windows command window/terminal, and shouldn't be used for production, so I don't have any plans to work around their emulation bugs.

peterbrittain commented 3 years ago

Was intrigued to know why this happened, so dug a little more... PyCharm uses pty4j, which uses winpty tocreate the console window (i.e. terminal in PyCharm). That last package does various things to manipulate the console.

In particular, it has this code to freeze/unfreeze the console output (https://github.com/rprichard/winpty/blob/7e59fe2d09adf0fa2aa606492e7ca98efbc5184e/src/agent/Win32Console.cc#L91). Call me suspicious, but that is injecting an escape key into the Window... which asciimatics then detects.

Definitely an interop issue with that package, so I'm going to close this now.

eyaler commented 3 years ago

thanks! injecting ENTER + ESC it seems... maybe this should be mentioned in the pycharm terminal documentation. i was hitting this when i mapped ESC to quit program, so it was quite an issue having my program exit on window resize...

peterbrittain commented 3 years ago

Good point. That belongs in the troubleshooting docs.