Window Active or Focus Event?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Window Active or Focus Event?

Post by Coiler » 25 Feb 2021, 18:07

I'm currently using a complex hot-if callback function for all of my hotkeys, and one of the first condition tests in that callback is the focused application. I was concerned that as I extend this key map, I may eventually have 10+ actions binded to any given key, where each app has its own actions binded to it. This means there may potentially be 10+ hot-if callback tests each time the key is pressed.

I was wondering if there would be a more efficient method to do this. Essentially, to activate entire app key maps, or enable all of an app's hotkeys, whenever that target app becomes focused. But I haven't been able to find any methods to support this in the documentation. It seems like most of the window-focus methods are aimed at either checking which window is active, or waiting for one. Is it possible to set up a callback event that would execute when the active window changes?

Or in case I am overlooking something else, if anyone is familiar with a more efficient way to handle this, please speak up. Thanks!

Note: I'm aware of #HotIf WinActive, but I'm assuming it would have the same type of overhead as what I'm doing now. If that is not the case, please let me know.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Window Active or Focus Event?

Post by swagfag » 26 Feb 2021, 19:22

this sounds like an optimization u'd be wasting ur time implementing. even if u had a thousand callback per hotkey to evaluate, were still talking sub millisecond ranges here
u can monitor events by installing either a Shell Hook or a WinEvent Hook: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook
User avatar
Coiler
Posts: 114
Joined: 29 Nov 2020, 09:06

Re: Window Active or Focus Event?

Post by Coiler » 10 Mar 2021, 13:28

Are you sure about your math? From the testing I've done, AutoHotKey is decent for a non-compiled language, but not to the degree of thousands of hot-key callback tests executing in sub-milliseconds. I know there is a shroud of darkness that hovers over micro optimizers and millisecond collectors, but I'm asking because the delay is detectable. I'm writing a script for artists that use graphics tablets, and the left mouse button is sacred. Any type of delay between events and reactions is easily noticeable. It ends up feeling like a pen that won't spit its ink out fast enough.

I'm confused as to why there wouldn't be some type of focus-switch built into AHK. Basically, hotkeys that take a nap until their app is focused. I'm sure it would be possible to rig something up using windows callbacks, but I'd rather just wait until AHK devs realize it needs this feature.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Window Active or Focus Event?

Post by swagfag » 10 Mar 2021, 14:58

i am. and mind u, im not saying there cant be delays in ur script, but what i am saying is its not #HotIfs having to execute a bunch of WinActive()s thats responsible for them.
but dont take my word for it, try it out on ur own

Code: Select all

#Requires AutoHotkey v2.0-a128-f63b0eb4

g_t0 := A_TickCount
dummyTitle := 'asdfasfasf'
hotkeyTrigger := 'q'

HotIf(f0.Bind(dummyTitle))
Hotkey(hotkeyTrigger, dummyHotkeyFunc)
HotIf()

dummyHotkeyFunc(ThisHotkey) => MsgBox(A_ThisFunc)
f(winTitle, ThisHotkey) => WinActive(winTitle)

f0(winTitle, ThisHotkey) {
	hwnd := WinActive(winTitle)
	global g_t0 := A_TickCount
	return hwnd
}

Loop n := 5000
{
	HotIf(f.Bind(dummyTitle A_Index))
	Hotkey(hotkeyTrigger, dummyHotkeyFunc)
	HotIf()
}

Hotkey(hotkeyTrigger, f1)

MsgBox n ' #HotIf Variants have been defined. Press "' hotkeyTrigger '" now.'

f1(ThisHotkey) {
	global g_t0, n
	MsgBox n ' #HotIfs took ' A_TickCount - g_t0 ' ms.'
}

Esc::ExitApp()
so i get 16ms per 5k callbacks which given A_TickCount's granularity of...16ms, can be anywhere from 0-32ms(u can test with QPC instead if u like) and i dont even have that good of a computer anymore(so the numbers that graphic-tablet-wielding-artists with their render machines are expected to see will be even lower than mine). i can comfortably crank it up to 20k before i start getting 200ms delays and keyboardhook timeouts. i think thats more than reasonable enough.
why there wouldn't be some type of focus-switch built into AHK
  • nobodys thought of it
  • its hard to implement
  • it may not be reliable
  • there no one to implement it
  • not enough people clamor for it
  • low priority
  • the list goes on and on...

I'd rather just wait until AHK devs realize it needs this feature
have we established that it does need it though? also, devs is a tad optimistic.
if i were u, id get on with banging out those event hooks and windows callbacks lol
Post Reply

Return to “Ask for Help (v2)”