asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.24k forks source link

Click on console window freezes script #797

Closed MaxGorr closed 1 year ago

MaxGorr commented 1 year ago

Environment

Description

Let's consider a simple clicker:

import pyautogui, time

while True:
    print('Before')
    pyautogui.click(100, 100)
    print('After')
    time.sleep(10)

Run cmd.exe, a point (100, 100) should be inside a cmd.exe window. Run this script (e.g. python clicker.py).

Actual behavior:

Expected behavior:

Details

harsha-bharadwaj100 commented 1 year ago

Very Interesting! I tried your sample code on my windows machine and what you are saying is indeed true. But the thing is... it's related to the working of command prompt and powershell. This has absolutely nothing to do with pyautogui. This is happening because when you click on command prompt it will enter selection state, when it is in the selection state it will not run your code. I'm not exactly sure why this happens, but I'm sure about one thing, that is... this has something do with command prompt itself not with python or pyautogui. when you click on some other key, it will return from selection state and then it will continue. Try running any code and this will happen. while the below example code is running click inside the command prompt window manually.

import time
time.sleep(10)
while True:
    print('Before')
    time.sleep(5) # Here Manually click on the command prompt 
    print('After')
    time.sleep(10)

Hope it helps!

MaxGorr commented 1 year ago

Thanks a lot! It is indeed a shell's "feature", and not about PyAutoGUI

harsha-bharadwaj100 commented 1 year ago

Thanks a lot! It is indeed a shell's "feature", and not about PyAutoGUI

You're welcome! :) 👍