Using the IfWinActive keyword in AutoHotKey

This question about the use of AutoHotKey is specific to a Windows LaTeX editor called TeXnicCenter.

So, I was trying to write my first AutoHotKey (AHK) script, and wanted the shortcuts to be available only when the TeXniceCenter window was active. AHK provides the IfWinActive keyword to deal with these scenarios, however, I ran into some difficulties in using this keyword.

Here is a draft file I wrote:

SetTitleMatchMode, 2
SetTitleMatchMode, Slow

#IfWinActive, .* TeXnicCenter *.
!t::
Send \texttt{{}{}}
return

The #IfWinActive, .* TeXnicCenter *. line, so that the pattern "TeXnicCenter" is found somewhere in the window name does not work.

There were some other options that I discarded

  • I use TeXnicCenter mainly with projects, so that the window name shows up as "projectname - TeXnicCenter", so it is not feasible to use this as the argument to IfWinActive. Note that the window name is TeXnicCenter if working on standalone documents.

  • Another option provided by AHK is that you use something called the ahk_class of the process, which is typically intuitive (and can be had from the handy bundled AHK tool, called Window Spy) -- for example, in the case of Chrome, it is Chrome_WidgetWin_1.

However, for TeXnicCenter, it shows the bizarre signature -- for example, for one of my projects, it is Afx:000000013F370000:8:0000000000010005:0000000000000000:0000000012B80087, and not only that, it is not constant across TeXnicCenter windows, as it usually is for other processes.

I am at a loss -- does anyone have experience setting up AHK with TeXnicCenter, and using the IfWinActive keyword? I have a feeling that this might be better directed at the developers of TeXnicCenter, but here's hoping.


Solution 1:

You used SetTitleMatchMode to set the title-matching mode to 2 which means A window's title can contain WinTitle anywhere inside it to be a match. So, it is trying to find .* TeXnicCenter *. in the title-bar. You should remove the .* and *. (unless the title bar actually contains those—which as far as I know, it does not). You can set the title-matching mode to RegEx if you would rather use the regex syntax, (and even then, the *. is incorrect, it should be .*).

As for the class, I had the same issue with GraphEdit which for the main window has a window class like Afx:1000000:b:10011:6:1070780 with the same pattern, but different numbers for each instance. I solved it by using regex mode (SetTitleMatchMode, RegEx) and a pattern like ^Afx:.+:.:.+:.:.+$—you can specify the exact number of digits between the colons, but it’s unlikely you’ll need to.

(I eventually ended up simplifying the whole process by using groups.)

So, in your case, you would use one of the following:

SetTitleMatchMode, 2
SetTitleMatchMode, Slow

#IfWinActive, TeXnicCenter
!t::
Send \texttt{{}{}}
return



SetTitleMatchMode, regex
SetTitleMatchMode, Slow

#IfWinActive, .* TeXnicCenter *.
!t::
Send \texttt{{}{}}
return

Here is my recommendation:

SetTitleMatchMode, regex
SetTitleMatchMode, Slow
GroupAdd, TXC, ^.*TeXnicCenter.*$ ahk_class ^Afx:.+:.:.+:.+:.*$

#IfWinExist, ahk_group TXC
  !t::
    Send \texttt{{}{}}
  return
#IfWinExist