Automatically detect if a certain window becomes active?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Patrick_Dayna
Posts: 44
Joined: 01 Jun 2021, 15:48

Automatically detect if a certain window becomes active?

22 Jun 2021, 21:18

I mean detect it without pressing a hotkey that will call the lines of code that will check it if said window is active

Below example works fine but pressing 4 is required.

Code: Select all

4::
if WinActive("*Untitled - Notepad ahk_exe notepad.exe")
  {
      MsgBox, Found Notepad
  }
  else
  {
       MsgBox, Did Not Find Notepad
  }
  Return
I have also tried this, but it just spams me with MsgBoxes.

Code: Select all

Loop, 50
  {
    if WinActive("ahk_class #32770")
      {
          MsgBox, Found Notepad
      }
      else
      {
           MsgBox, Did Not Find Notepad
      }
  }
Thanks.
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Automatically detect if a certain window becomes active?

22 Jun 2021, 21:21

Code: Select all

Loop {
 WinWaitActive, ahk_exe notepad.exe
 MsgBox, 64, Notepad, Found Notepad.
 WinWaitNotActive
 MsgBox, 64, Notepad, Did not find Notepad.
}
Also: https://autohotkey.com/board/topic/32628-tool-shellhook-messages/
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Automatically detect if a certain window becomes active?

22 Jun 2021, 21:25

Have your loop use WinWait to wait for your window to appear and show your MsgBox, then use WinWaitClose after the MsgBox so it doesn’t loop around to the WinWait again until after it has disappeared. That will cause it to notify you once per appearance instead of spamming you.

Edit: As mikeyww showed. Same idea but waiting for it to be active or not.
User avatar
Patrick_Dayna
Posts: 44
Joined: 01 Jun 2021, 15:48

Re: Automatically detect if a certain window becomes active?

22 Jun 2021, 22:03

How to quote individual users on this forum?
When I click reply, am taken to another page, where the mention user icon is not shown. Its very annoying.
Thanks guys.
Anyways, I tried your code mikeyww. It works well but the messages box appears again, I am sure this is what boiler was referring to, I will test his suggestion as well.
A question regarding this Loop command in this context. Since there is no specific number defined for the Loop, (Loop,50) does this mean the the loop is being repeated at its fastest, and for as long as the script is running so is the loop.

This is scrip is always gonna be running as part of my main script. Will this be process intensive? I want to use it but I would appreciate it if I can understand how its working.
User avatar
Patrick_Dayna
Posts: 44
Joined: 01 Jun 2021, 15:48

Re: Automatically detect if a certain window becomes active?

22 Jun 2021, 22:13

I missed this part of your message, I was messing around with this earlier. Your link actually explains what it is.
Excellent. I will make do with a loop for now and will move over to shelhook thing as I get more experience.
Thanks allot
User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Automatically detect if a certain window becomes active?

22 Jun 2021, 22:32

Patrick_Dayna wrote:
22 Jun 2021, 22:03
How to quote individual users on this forum?
When I click reply, am taken to another page, where the mention user icon is not shown. Its very annoying.
Click the button with a quotation mark on it that appears next to the post you want to quote rather than clicking the "Reply" button at the bottom.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, Rohwedder and 390 guests