It works best by using SetTimer:
; The following hotkeys work only if MS-WORD is the active window:
#If WinActive("ahk_exe WINWORD.EXE") ; (1)
1::
if 1_presses > 0
{
1_presses += 1
SetTimer Key1, 300
return
}
1_presses = 1
SetTimer Key1, 300
return
Key1:
SetTimer Key1, off
if 1_presses = 2
SendInput, BYE
else
SendInput, HELLOW
1_presses = 0
return
F2:: MsgBox, You pressed F2 in MS-Word
; The following hotkeys work only if NOTEPAD is the active window:
#If WinActive("ahk_exe NOTEPAD.EXE")
1:: Send 2
F2:: MsgBox, You pressed F2 in NOTEPAD
#If ; turn off context sensitivity (= end of context-sensitive hotkeys)
; The following hotkeys work only if MS-WORD or NOTEPAD is NOT the active window (because they are already defined in those programs):
1:: Send 3
F2:: MsgBox, You pressed F2 while MS-WORD or NOTEPAD is NOT the active window
; The following hotkeys work in all windows (incl. MS-WORD and NOTEPAD because they are NOT defined in those programs)
F3:: MsgBox, You pressed F3
Esc:: ExitApp
https://autohotkey.com/docs/commands/SetTimer.htm#Examples (Example #3)
(1) Like the #IfWin directives, #If creates context-sensitive hotkeys and hotstrings and is positional: it affects all hotkeys and hotstrings physically beneath it in the script.
#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).
Best Answer
Try looking at the SendPlay functionality.
Here is an example:
If that doesn't work, then take a look at the link above and try one of the other solutions.