pythonconsole

How to prevent a user freezing python by interacting with the console window?


I am making a retro-style GUI in (semi)pure python using ascii characters. The script works by printing and clearing to the console while using the users mouse and keyboard data to create a fully interactive GUI.

However, upon creating the click detection system, I have noticed that python freezes running when the user interacts with the actual console window (e.g drags console, clicks on console). This freeze then ends when the user presses any key or right clicks.

I have attempted to overcome this issue by running a separate script to press right click immediately after the user left clicks, to uninteract and unfreeze the code, however this piece of code never runs as the program is frozen before it can be run.

I have also tried putting this code in a completely separate file and running it separately, but this is also frozen when any python console window is (even if the separate file is running as a pyw).

This is the click detector:

def c_c():
   global mouse_pos
   last_cycle = False
   cd = os.getcwd()
   nw = "pyw " + cd + "\\misc_scripts\\click_activator.py"
   os.system(nw)
   while True:
        
        state = ctypes.windll.user32.GetAsyncKeyState(0x01) # left click
        pressed = (state & 0x8000 != 0)
        if pressed:
           onclick(mouse_pos)

The other script contained a similar program, which was fully functional in using the right_click() function to immediately right click after the user left clicks, which was fully operational in outside testing, however was not while using the main program leading me to believe that this script also gets frozen when the user interacts with any console.

Im looking for a way to get around this issue, or making the user unable to interact with the main console while still being able to click on it.


Solution

  • Disable quick edit console mode by running:

    import ctypes
    
    kernel32 = ctypes.windll.kernel32
    kernel32.SetConsoleMode(kernel32.GetStdHandle(-10), 128)