Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

Discuss the future of the AutoHotkey language
william_ahk
Posts: 496
Joined: 03 Dec 2018, 20:02

Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

23 Jun 2023, 22:56

WinTitle and WinText have the same meaning as for WinActive or WinExist, but only strings can be used, and they are evaluated according to the default settings for SetTitleMatchMode and DetectHiddenWindows as set by the auto-execute thread. See WinTitle for details.
HotIf - Syntax & Usage | AutoHotkey v2
Is this intended design or work in progress? If it's intended, why?
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

24 Jun 2023, 20:24

For memory-efficiency and code size, and due to legacy design, the window criteria for hotkeys are stored as strings. I have no intention to change this because it is highly unusual to set a HWND as the criteria, and it is extremely simple to prefix it with "ahk_id". Furthermore, hotkeys and hotkey criteria are "permanent" whereas windows are transient. A window can be destroyed and recreated (or the app can be restarted) and although the script can create a new hotkey variant for the new window, it cannot delete the old hotkey variant. This is not just a case of memory waste, since a HWND value can be reused by the system.

If you are defining hotkeys for your script's own GUI windows, using context-sensitive hotkeys via a system-wide keyboard hook is inefficient and can have unwanted side-effects. I recognize that this is an area where the GUI object can be improved, as currently you need to use OnMessage to detect keyboard messages, or use tricks like a hidden Default Button, or keyboard shortcuts defined in Menus (for which the GUI must have a MenuBar). At least v2.1-alpha.1 implements per-GUI OnMessage.
william_ahk
Posts: 496
Joined: 03 Dec 2018, 20:02

Re: Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

24 Jun 2023, 21:32

lexikos wrote:
24 Jun 2023, 20:24
For memory-efficiency and code size, and due to legacy design, the window criteria for hotkeys are stored as strings. I have no intention to change this because it is highly unusual to set a HWND as the criteria [...]

If you are defining hotkeys for your script's own GUI windows, using context-sensitive hotkeys via a system-wide keyboard hook is inefficient and can have unwanted side-effects. [...]
I don't think it's that unusual, I'm actually defining global hotkeys excluding the script's GUI windows. Is there a better way to do the exclusion? But yeah prefixing ahk_id is fine.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

25 Jun 2023, 03:41

Excluding the script's GUI windows? AutoHotkey has never supported excluding windows by HWND or any criteria other than pure title (no ahk_ keywords) and text.

I suppose you could achieve the same effect by including the script's windows for a hotkey variant that does nothing, and letting the global variant handle everything else. If you want to match all of the script's GUI windows, combining ahk_pid with ahk_class AutoHotkeyGUI would work. If you only ever create or exclude one GUI, then I suppose it doesn't matter whether you use HWND or PID.

Unless I'm forgetting something, the only way to exclude the script's windows from a hotkey altogether would be to use a function with HotIf or an expression with #HotIf, in which case the limitation you quoted is irrelevant.

That you are wanting to do it at this time does not in any way contradict my view that it is unusual.
20170201225639
Posts: 144
Joined: 01 Feb 2017, 22:57

Re: Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

26 Aug 2023, 02:42

I just went through my code and deleted all occurrences of `"ahk_id "`, which was a big source of annoyance. I had no idea you can now just use the hwnd!
iseahound
Posts: 1446
Joined: 13 Aug 2016, 21:04
Contact:

Re: Why do Win functions accept hwnd integers as WinTitle while HotIfWin functions only accept strings?

02 Sep 2023, 09:13

Oh just a brief mention—it's probably safer to use "ahk_id" since it directly specifies the type of input (which would be a window id). With that said, I use whichever one is more convenient.

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: lmstearn and 118 guests