wrichards0
wrichards0

Reputation: 187

Check if window has focus with AutoIt

I wonder if it is possible to check to see if a window has focus in AutoIt. I have checked and not found much. I have tried using WinSetOnTop but this didn't seem to do anything so then I considered using WinActivate but this didn't seem to do what I need.

The reason I want to do this is because I have this application I am writing as a prank and I do not want the co-worker on whom I'm playing the prank to just ignore the window when it starts automatically. I am wanting to put a shortcut to it in the startup folder and we have several applications that run on startup and so I want mine to either always be on top or audibly shout rude words at the user if they try and ignore the application.

Is this possible and, if so, can you help me out because I am out of ideas.

Upvotes: 0

Views: 1720

Answers (1)

Bookeater
Bookeater

Reputation: 474

Regardless of your motives, you may try WinWaitActive.

Syntax:

WinWaitActive ( "title" [, "text" [, timeout = 0]] )

Example that may be useful to try it out:

Func Example()
    ; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    WinWaitActive("[CLASS:Notepad]", "", 10)

    ; Wait for 2 seconds to display the Notepad window.
    Sleep(2000)

    ; Close the Notepad window using the classname of Notepad.
    WinClose("[CLASS:Notepad]")
EndFunc   ;==>Example

Reference:
https://www.autoitscript.com/autoit3/docs/functions/WinWaitActive.htm

Upvotes: 2

Related Questions