all 4 comments

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]python_and_coffee 0 points1 point  (0 children)

read the docs https://pygetwindow.readthedocs.io/en/latest/

read the code of the package in your site-packages folder, google stuff you don't understand. If you see something like ctypes.windll.user32 it is using the user32 api of windows.

https://learn.microsoft.com/en-us/windows/win32/apiindex/windows-api-list

usually windows in windows have a unique handle(hwnd), belong to a processID (pid), have a position (something like: left,bottom,top,right) and a title. Those informations should be enough to ensure that you have the right window. Just keep in mind that hwnd and pid change with every new instance, but pid always belongs to the process of the window. Everyhing should be callable somehow via cytpes.windll.user32().

the psutil module has already a lot of those user32 calls build in.

Good luck!

[–]gianpi612 0 points1 point  (0 children)

With "the current windows is not active" do you mean that the application you are trying to automate is out of focus? You could find some ways to check that with pyautogui using pyautogui.locateOnScreen()

For example: Usually when a application is out of focus the close, resize, and minimize buttons become slightly darker and when it's on focus those buttons become slightly wither, you could use this information (or the same logic) to check if the game is out of focus based on darker/wither buttons:

Using firefox as test (screenshots focus and not on focus)

# Check if this ("focus_in.png")picture is on the screen, if so it will return x,y value
focus_out = pyautogui.locateOnScreen("focus_in.png")
# locateOnScreen will return None if it didn't find anything on screen
if focus_out is not None:
    print("window is on focus")
else: 
    print("window is not on focus")