AutoHotkey - application loosing focus when enabling full screen mode

1

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

litu16

Posted 2016-11-28T20:37:28.757

Reputation: 143

Answers

1

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

user3419297

Posted 2016-11-28T20:37:28.757

Reputation: 2 055

I'm currently testing, I don't know sometimes works, sometimes not. Not working I stil have to make click on ArtRage window after performing the first two times, any idea? – litu16 – 2016-11-29T02:42:20.773

Check my edited answer. – user3419297 – 2016-11-29T09:32:08.670

Nop, it doesn't create a new instance of ArtRage, the AHK script always prints "there is only 1 window fo this ahk class. The curious thing is that I have tried that key with two macro recorders, "Key Manager" and "Hot Keyboard Pro" both work fine, so I gues there isn't nothing special on the Artrage window. can I use contrlsend in this case? – litu16 – 2016-11-29T11:16:13.913

See also EDIT2 above. – user3419297 – 2016-11-29T11:32:50.630

thanks it worked using the #If WinExist and the ahk_parent. But now the problem is that if artrage is open (even not focused) Capslock won't fire Capslock in any other application instead it will jump to ArtRage and press ^{Right}, so other apps that use Capslock to enter in full screen mode won't work, until I close either ArtRage or Autohotkey. What can I do? I EDITED my question to show what I did, Thanks Advanced. – litu16 – 2016-11-29T17:52:32.267

There are many ways to solve this problem, and it will depend on your situation. I'd start another question to discuss it. – user3419297 – 2016-11-29T18:08:20.697

Let us continue this discussion in chat.

– user3419297 – 2016-11-29T18:10:39.590

A new question would be very similar to this one, cuz it would need to kinda summarize all what was explained in this questions, things like... I use AHK 1.1 to set the capslock to be a hotkey to toogle ArtRage full screen mode (workbench mode), I do so, cuz 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 far #if winexist is to wide handle, but If I use #if winactive it won't work, how can I solve this? thanks advanced. – litu16 – 2016-11-29T18:55:01.657

See EDIT3 above. – user3419297 – 2016-11-29T20:12:44.020

Hi, user3419297, I don't understand, #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). isn't that what I just did?, I tried to put another #If before the #ifWinExist. Could you please show an example of How to do it? I posted in my edidted code how I thought I could put another #if before the #ifWinExist but then you said it is wrong, how should I do it? thanks advanced. – litu16 – 2016-11-29T22:05:04.980

Hi user3419297I made a new question about this issue, hope you could help me out http://superuser.com/questions/1151188/autohotkey-hotkey-overriding-key-in-all-windows-apps-instead-of-just-if-windo thanks advanced.

– litu16 – 2016-11-29T23:26:09.253

See Example in EDIT3. – user3419297 – 2016-11-29T23:30:03.700

I see that spaces have no meaning in AHK scripting. Thanks for the example, now I think I could do it, but I usually put the #If WinActive then do some code then #if (end of context-sensitive hotkeys) before start another different piece of code, cuz otherwise most of the hotkeys won't work to me. So I tested as your example but I set the precedence, put the code, then closed the context-sensitive hotkeys, and open the new #IfWinActive ArtRage and it worked, but... would it mean that I have to kinda LIST all apps different tahn ArtRage where I want the capslock to work different. Thank – litu16 – 2016-11-30T00:01:16.050

anoother thing that confuse me of AHK scripting is that If and else statements need braces (if the code within them are larger than 1 line), but these in your example doesn't need them, why? – litu16 – 2016-11-30T00:02:49.420

hi user3419297 I solved it, but I have problems refocusing on ArtRage window after the script have run, I posted what I did in this new question with all details, could you please check it out? tahnks advanced. http://superuser.com/questions/1151188/autohotkey-hotkey-overriding-key-in-all-windows-apps-instead-of-just-if-windo

– litu16 – 2016-11-30T03:21:52.550

2

How about this?

*$vk14:: ; Capslock
{
    Send {vk11 Down} ; Ctrl
    Sleep 50
    Send {vk27 Down} ; Right arrow
    Sleep 50
    Send {vk11 Up} ; Ctrl
    Sleep 50
    Send {vk27 Up} ; Right arrow
}
Return

Sometimes adding key codes and adding sleep time between key presses/releases helps.

KulaGGin

Posted 2016-11-28T20:37:28.757

Reputation: 61

I'm testing.. it seems that it works, but sometimes not. Not working I stil have to make click on ArtRage window after performing the first two times, any idea? – litu16 – 2016-11-29T02:42:56.810