AutoHotkey – Fix Application Losing Focus in Full Screen Mode

autohotkeyfocuswindow

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

#If WinExist("ahk_class ArtRage 3")

    Capslock::
    WinActivate, ahk_class ArtRage 3
    WinWaitActive, ahk_class ArtRage 3
    Send {Ctrl Down}{Right}{Ctrl Up}    ; Ctrl Right is the key I set to toogle the workbench mode
    return

#If

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:

F1::
WinGet, instances, count, ahk_class ArtRage
MsgBox, There exist %instances% windows of this ahk_class.
return

EDIT2:

Try also this as standalone script (close all other scripts that may interfere):

#If WinExist("ahk_class ArtRage 3")

    Capslock::
    ControlSend, ahk_parent, ^{Right}, ahk_class ArtRage 3
    ; or:
    ; ControlSend,, ^{Right}, ahk_class ArtRage 3
    return

#If

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.

The #IfWin directives are positional: 
they affect all hotkeys and hotstrings physically beneath them in the script. 
They are also mutually exclusive; that is, only the most recent one will be in effect.

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:

#If WinActive("ahk_class ArtRage 3")

    Capslock:: MsgBox, You pressed Capslock while ArtRage  was active

    1:: MsgBox, You pressed 1 while ArtRage  was active 

#If WinActive("ahk_class notepad")

    Capslock:: MsgBox, You pressed Capslock while Notepad was active 

    1:: Send, 2

#If WinActive("ahk_class CabinetWClass")

    Capslock:: MsgBox, You pressed Capslock while Explorer was active 

    1:: Run %A_MyDocuments%

#If WinExist("ahk_class ArtRage 3")

    Capslock:: MsgBox, You pressed Capslock while ArtRage was inactive `n(Notepad and Explorer are not active or do not exist)

    1:: MsgBox, You pressed 1 while ArtRage was inactive`nNotepad and Explorer are not active or do not exist

#If WinExist("ahk_class IEFrame")

    Capslock:: MsgBox, You pressed Capslock while IE was inactive `nArtRage does not exist,`nNotepad and Explorer are not active or do not exist

#If                 ; end of context-sensitive hotkeys


Capslock:: MsgBox, You pressed Capslock while ArtRage and IE do not exist`nNotepad and Explorer are not active or do not exist

1::  MsgBox, You pressed 1 while ArtRage and IE do not exist`nNotepad and Explorer are not active or do not exist

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

Related Question