Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

how to detect a window is opened?


  • Please log in to reply
15 replies to this topic
A117
  • Members
  • 14 posts
  • Last active: Jan 20 2015 01:26 AM
  • Joined: 14 Jun 2012
I would like to do sth. whenever a window is opened. How to write this code please? IfWinActive or WinWait did not work at all.

Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
IfWinExist / IfWinNotExist Checks if the specified window exists.

hope it helps

Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

Sjc1000
  • Members
  • 572 posts
  • Last active: Mar 11 2017 11:41 AM
  • Joined: 06 Feb 2012
Loop
{
	sleep 500
	WinGetActiveTitle, Title
	If Title = [color=#FF0000]Window Name[/color]
		break
}
[color=#00BF00]**Code you want when window is active**[/color]
If your right with having a loop

Sjc1000 - Insert inspirational quote here!

PLEASE find me on the IRC if you have questions. I'm never on the forum anymore.

 


  • Guests
  • Last active:
  • Joined: --
first, when I said "a window", I meant "any window". So when I used IfWinActive, I would like it to have no parameters.
second, it seems I have to use the Loop, which proved there is no way to simply Detect when a window is opened. All actions so far I know are triggered by Keys. So I still do not know how to trigger the Loop function in ahk file. If I write IfWinExist, ... {... } or Loop {...}, nothing happens. :?:

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
you can combine settimer with winget, list and then check the list for new items

something like:
;this section runs automatically at startup
settimer, checkForNewWindow, 100   ;every 100ms, run the label
return

checkForNewWindow:    ;the label
WinGet, id, list,,, Program Manager   ;list of windows (exclude the desktop)
if id > lastid
{  
   Tooltip, %id% windows!
   SetTimer, ClearTooltip, -2000
}
lastid := id    ;always keep up to date
Return

ClearTooltip:
Tooltip
Return

i alsmot made it have a msgbox, but that would add a new window and act recursively!

  • Guests
  • Last active:
  • Joined: --

you can combine settimer with winget, list and then check the list for new items

something like:

;this section runs automatically at startup
settimer, checkForNewWindow, 100   ;every 100ms, run the label
return

checkForNewWindow:    ;the label
WinGet, id, list,,, Program Manager   ;list of windows (exclude the desktop)
if id > lastid
{  
  WinMaximize
}
lastid := id    ;always keep up to date
Return

i alsmot made it have a msgbox, but that would add a new window and act recursively!

I changed it as above. nothing happened.
My workaround below does not work either.
settimer, checkForNewWindow, 1000   ;every 1s, run the label
;return

checkForNewWindow:    ;the label
WinWait,,,,Program Manager
;WinGet, id, list,,, Program Manager   ;list of windows (exclude the desktop)
;if id > lastid
;{
WinMaximize
;}
Return


guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
it seems engunneer's script needs #persistent

but as he said, you have to combine with the method above. heres the example that will trigger whenever a new notepad window is opened:

#persistent

   ;this section runs automatically at startup

   settimer, checkForNewWindow, 100   ;every 100ms, run the label

return

checkForNewWindow:    ;the label
   WinGet, id, list,,, Program Manager   ;list of windows (exclude the desktop)
   if (id > lastid)
   {  
      WinGetActiveTitle, Title
      If InStr(Title, "Notepad") {
         msgbox, new notepad window opened
      }
   }
   lastid := id    ;always keep up to date
Return



  • Guests
  • Last active:
  • Joined: --

it seems engunneer's script needs #persistent

but as he said, you have to combine with the method above. heres the example that will trigger whenever a new notepad window is opened:

#persistent

   ;this section runs automatically at startup

   settimer, checkForNewWindow, 100   ;every 100ms, run the label

return

checkForNewWindow:    ;the label
   WinGet, id, list,,, Program Manager   ;list of windows (exclude the desktop)
   if (id > lastid)
   {  
      WinGetActiveTitle, Title
      If InStr(Title, "Notepad") {
         msgbox, new notepad window opened
      }
   }
   lastid := id    ;always keep up to date
Return

settimer just seems not working at all for me. Windows 8

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
here is simple settimer test

SetTimer, popup, 3000
msgbox, timer started!
return

popup:
Msgbox Pop!
Return

Esc::ExitApp

does this do anything? is there icon for running script?

A117
  • Members
  • 14 posts
  • Last active: Jan 20 2015 01:26 AM
  • Joined: 14 Jun 2012
actually I'm using the current AutoHotkey_L on win 8. the simple test does not work for me
begging part of my ahk
#s::Run "c:\WINDOWS\system32\sndvol32.exe"

;#persistent
SetTimer,checkForNewWindow,1000
msgbox, timer started!
return

checkForNewWindow:
         msgbox,new notepad window opened
Return
#PgUp::WinMaximize,A


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
Move your #s hot key to the end?

A117
  • Members
  • 14 posts
  • Last active: Jan 20 2015 01:26 AM
  • Joined: 14 Jun 2012

Move your #s hot key to the end?

yes. why? there should be some place telling me settimer must be the first in the script. Or did I just miss it?
Thank you all very much.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
May be useful: [How to] Hook on to Shell to receive its messages?

guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

actually I'm using the current AutoHotkey_L on win 8. the simple test does not work for me
begging part of my ahk

#s::Run "c:\WINDOWS\system32\sndvol32.exe"

;#persistent
SetTimer,checkForNewWindow,1000
msgbox, timer started!
return

checkForNewWindow:
         msgbox,new notepad window opened
Return
#PgUp::WinMaximize,A


put the 'simple test' in its OWN script and run it to test this first and let us know

do not put it on top at the beginning of your script because its likely that your script is buggy.

  • Guests
  • Last active:
  • Joined: --
#Persistent

DllCall("RegisterShellHookWindow", "UInt", A_ScriptHWND)

OnMessage(DllCall("RegisterWindowMessage", "Str","SHELLHOOK"), "ShellMessage" )

Return



ShellMessage(wParam, lParam) {

    global createdWindowTitle

    If wParam = 1

        WinGetTitle, createdWindowTitle, ahk_id %lParam%

    SetTimer, ToolTip, -10

}



ToolTip:

    ToolTip, %createdWindowTitle%

    return