I use AHK 1.1 to set the CapsLock to be a hotkey to toggle ArtRage full screen mode (workbench mode), I do so because ArtRage doesn't allow me to set CapsLock as a hotkey, but I'm quite used to use that key to do that in many programs, so I thought AHK could help me, my script was simple:
; AR4 Toggle Workbench Mode
Capslock::
Send {Ctrl Down}{Right}{Ctrl Up} ; Ctrl Right is the key I set to toogle the workbench mode
WinActivate ahk_class ArtRage 3
return
#If
The script works only the first time I enable the full screen mode
and the first time I disable it, I mean the first two times I press CapsLock , but then it won't work unless I manually click on the ArtRage window. If I do so I can use the hotkey two more times.
So I guess I'm somehow loosing focus on the window. I have tried also this:
Capslock::
ControlSend,, {Ctrl Down}{Right}{Ctrl Up}, ahk_class ArtRage 3
WinActivate ahk_class ArtRage 3
return
#If
with the same result, I google about it, and I tried:
Capslock::
WinGet, AR4_id, ID, A
Send {Ctrl Down}{Right}{Ctrl Up}
ControlFocus,,%AR4_id%
return
but it doesn't work at all. Hope some superuser could help me on this one.
EDITED >>>>
So Now I have tried to make the script work if WinExist AND if WinActive, is that possible? I made it like this, but it doesn't work, CapsLock still calls ArtRage on every application.
#If WinActive("ahk_class ArtRage 3")
#If WinExist("ahk_class ArtRage 3")
Capslock::
ControlSend, ahk_parent, {SC037}, ahk_class ArtRage 3 ; NumpadMult
return
#If
#If
EDIT2>>>>
I tweaked the code like this:
If WinActive("ahk_class ArtRage 3")
Capslock::
ControlSend, ahk_parent, {SC037}, ahk_class ArtRage 3 ; NumpadMult
return
#If
the code works, but if ArtRage is open (not focused) and I'm in MS Word, if I press CapsLock it will not send CapsLock but it will send the "work in benchmode" in Artrage despite is not focused.
PD: Now NumpadMult is the new hotkey to enter the full screen mode (it's easier).
Best Answer
EDIT:
Could it be that the program creates a new window of this ahk_class in the workbench mode? Use this to find it out:
EDIT2:
Try also this as standalone script (close all other scripts that may interfere):
If it doesn't work, read https://autohotkey.com/docs/FAQ.htm#games and try the solutions mentioned there.
EDIT3:
The answer to the question how to best use the #If- or #IfWin directives depends on your situation.
https://autohotkey.com/docs/commands/_IfWinActive.htm#Basic_Operation
#if WinExist is a wide handle, but only if you give priority to it, that is, if you put it before other #if directives in the script. Try to give priority to #if WinActive directives (put them before #if WinExist in your script).
Example:
BTW: #If WinExist("ahk_class ArtRage 3") after #If WinActive("ahk_class ArtRage 3") doesn't make sense (The #If WinActive directive presupposes that this window exists).