17
votes

The AutoHotkey Beginner Tutorial includes a sample script to demonstrate window specific hotkeys and hotstrings.

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

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

The second example is fairly self-explanatory: Check for a window named "untitled - Paint". It's the first example's use of ahk_class that has me confused.

I haven't been able to find any explanation of the variable in the AHK documentation. According to an AHK forum post, ahk_class is the name of a window as given by Windows Spy, the post didn't go into any more detail.

Would there be any difference between using ahk_class Notepad and Untitled - Notepad? Would the second example work if replaced with #IfWinActive ahk_class Paint?

What is ahk_class and how can I use it for window matching?

2
Additional note: I'm using the portable version of AutoHotkey, which does not include WindowSpy.Stevoisiak

2 Answers

17
votes

From 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.

In other words you can use it to identify windows of the same type, if you open notepad the title will be Untitled - Notepad if you save it to temp.txt the title will become temp - Notepad. ahk_class on the other hand will always remain Notepad.

The second example will work if you replace it with #IfWinActive ahk_class MSPaintApp because that's the class of mspaint.

Usually you find ahk_class using Window Spy and then use it in your scripts. If you don't have Window Spy you can use the following hotkey:

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

After you found it you can use it in any place you can use window title for example instead of writing WinActivate, Untitled - Notepad you can write WinActivate, ahk_class Notepad.

1
votes

Check this article. Ahk_class is the class that is given to you when you use the WindowSpy tool. This tool should be in the same folder as your AutoHotkey executable.