How to know when any window is activated

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

How to know when any window is activated

14 Oct 2021, 16:06

So basically, this is for a project im making, and the last piece of it, is to detect when any window is activated, I've searched and found people using ShellHook but it's not working how I want it to, it only works if a new window is open, not when you change from one window to another, perhaps this has to do with my windows version (Windows 10), im not entirely sure, thing is, lets say i use shellhook for it, when I open a new window that doesn't exist yet, it works, but when I open a window that does exist but hasn't been activated, it doesn't work, I want this to detect when a window is activated not when a new one is made
mauritius
Posts: 25
Joined: 06 Nov 2020, 14:00
Contact:

Re: How to know when any window is activated

14 Oct 2021, 17:45

Did you try to use WinActivate (activates the window you want) or WinGetActiveTitle (you cac check which window is now active) functions? Also WinWaitActive / WinWaitNotActive may help.
I used all of the above (mosty first and third one) and they all worked fine for me.
evilmanimani
Posts: 29
Joined: 24 Jun 2020, 16:42

Re: How to know when any window is activated

14 Oct 2021, 18:00

This can be done fairly easily with a Settimer, this for example calls the Notify function every time the activeWin variable changes:

Code: Select all

#Persistent
activeWin := ""
settimer, windowWatch, 10
return


windowWatch:
if (activeWin != WinActive("A")) {
    activeWin := WinActive("A")
    Notify(activeWin)
}
return


Notify(hwnd) {
    static title
    WinGetTitle, title, ahk_id %hwnd%
    settimer, tooltipoff, -2000
    settimer, tooltip, 100
    return
    tooltip:
    Tooltip, % "Window """ title """ activated"
    return
    tooltipoff:
    settimer, tooltip, off
    tooltip
    return
}
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: How to know when any window is activated

14 Oct 2021, 18:44

evilmanimani wrote:
14 Oct 2021, 18:00
This can be done fairly easily with a Settimer, this for example calls the Notify function every time the activeWin variable changes:

Code: Select all

#Persistent
activeWin := ""
settimer, windowWatch, 10
return


windowWatch:
if (activeWin != WinActive("A")) {
    activeWin := WinActive("A")
    Notify(activeWin)
}
return


Notify(hwnd) {
    static title
    WinGetTitle, title, ahk_id %hwnd%
    settimer, tooltipoff, -2000
    settimer, tooltip, 100
    return
    tooltip:
    Tooltip, % "Window """ title """ activated"
    return
    tooltipoff:
    settimer, tooltip, off
    tooltip
    return
}
thank you so much, I hadn't thought of that for some reason, I have this because I just want it to be activated when the mouse is clicked

Code: Select all

#Persistent
activeWin := ""
return

~LButton::
Sleep, 200
if (activeWin != WinActive("A")) {
    activeWin := WinActive("A")
    Notify(activeWin)
}
Notify(win) {
	ToolTip, %win%
}
EntropicBlackhole
Posts: 40
Joined: 12 Jun 2021, 15:28

Re: How to know when any window is activated

14 Oct 2021, 18:46

mauritius wrote:
14 Oct 2021, 17:45
Did you try to use WinActivate (activates the window you want) or WinGetActiveTitle (you cac check which window is now active) functions? Also WinWaitActive / WinWaitNotActive may help.
I used all of the above (mosty first and third one) and they all worked fine for me.
this isn't exactly what I wanted to achieve, because if I want it to be with any window, and not just thaI, the window that is active will be compared against a list of windows and if its one of those windows, then it'll hide that window, keep an eye out for "WinLock" dropping today on the Scripts and Functions forum
Lamron750
Posts: 8
Joined: 02 Feb 2019, 19:31

Re: How to know when any window is activated

16 Oct 2021, 12:09

When using ShellHook, watch for wParam to be either 4 or 32772

Code: Select all

If(wParam = 4 OR wParam = 32772)	;WINDOWACTIVATED
{
; Check Window / Do Stuff
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: MSN [Bot], NinjoOnline and 205 guests