什么是 ahk_class?如何将其用于 window 匹配?

What is ahk_class? How can I use it for window matching?

AutoHotkey Beginner Tutorial includes a sample script 演示 window 特定的热键和热字串。

#IfWinActive ahk_class Notepad
::msg::You typed msg in Notepad
#IfWinActive

#IfWinActive untitled - Paint
::msg::You typed msg in MSPaint!
#IfWinActive

第二个示例是不言自明的:检查名为 "untitled - Paint" 的 window。这是让我感到困惑的 ahk_class 的第一个示例。

我没能在 AHK documentation. According to an AHK forum post 中找到变量的任何解释,ahk_class 是 Windows 间谍给出的 window 的名称, post 没有详细说明。

使用ahk_class NotepadUntitled - Notepad有什么区别吗?如果替换为 #IfWinActive ahk_class Paint,第二个示例是否有效?

什么是 ahk_class 以及如何将其用于 window 匹配?

检查 this 篇文章。 Ahk_class 是使用 WindowSpy 工具时给你的 class。此工具应与您的 AutoHotkey 可执行文件位于同一文件夹中。

来自https://autohotkey.com/docs/misc/WinTitle.htm#ahk_class

A window class is a set of attributes that the system uses as a template to create a window. In other words, the class name of the window identifies what type of window it is.

也就是说你可以用它来识别同类型的windows,如果你打开记事本标题会是Untitled - Notepad如果你保存到temp.txt标题会变成 temp - Notepad。另一方面,ahk_class 将始终保持 Notepad

如果将第二个示例替换为 #IfWinActive ahk_class MSPaintApp,第二个示例将起作用,因为那是 mspaint 的 class。

通常您会使用 Window Spy 找到 ahk_class,然后在您的脚本中使用它。如果您没有 Window Spy,您可以使用以下热键:

#1::WinGetClass, Clipboard, A ; Will copy the ahk_class of the Active Window to clipboard

找到它后你可以在任何地方使用它你可以使用 window 标题而不是写 WinActivate, Untitled - Notepad 你可以写 WinActivate, ahk_class Notepad.