Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Question about AutoHotkey's WinActive Function

  • Please log in to reply
2 replies to this topic
  • Members
  • 3 posts
  • Last active: Jun 29 2017 11:12 AM
  • Joined: 13 Nov 2014

Yes this really is a programming question.


Can any of the developers of AutoHotkey tell me how the WinActive function works internally?

I'm very interested to know if it's polling the system using GetForegroundWindow() or if it is hooking to some event like SetWinEventHook().

Also what optimizations are in place to maximize performance of such polling, if any.


I have long been and continue to be a big AutoHotkey user, however I've since moved onto C++/C#/Python/Shell development.

One of my current projects needs to monitor a set of windows and I'm deliberating which approach to take.

Looking back I've always been very happy with the performance of AutoHotkey's IfWinActive function,

so I'm looking to use the same approach internally in my current project.


Any detailed and technical insights would be greatly appreciated.

  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

WinActive just calls GetForegroundWindow exactly once, then checks that window against whatever criteria was given (title, window class, text, etc.).


Hooking an event would mean running code every time a window activates, even if the script is inactive, instead of querying the foreground window when needed (which is probably a trivial call).


WinWaitActive effectively calls WinActive() approximately every 10ms (yielding its CPU time slice after each check, allowing other processes to get CPU time).  Each check takes a very small fraction of that time, and even that only after you've triggered a script to wait for something (e.g. by pressing a hotkey).  WinActive("SciTE4AutoHotkey ahk_class SciTEWindow") benchmarks at 1.3┬Ás on my system, and that's including script/loop overhead.

  • Members
  • 3 posts
  • Last active: Jun 29 2017 11:12 AM
  • Joined: 13 Nov 2014

Thanks Lexikos for the quick and descriptive response, that was a very informative read.

Based on what you said I decided to go ahead and poll GetForegroundWindow,

it does seem like the best approach. and I've already got it setup and working in Visual Studio!

I was originally a little worried about the overhead of polling so frequently.


Thanks again for your insight, it was very helpful! :)