make macro work only when window is active? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ravena1
Posts: 62
Joined: 06 Sep 2017, 15:13

make macro work only when window is active?

06 Mar 2022, 08:17

hi how to make my macro works only if a specified pid window is active?
thank you!

Code: Select all

#NoEnv  
SendMode Input 
SetWorkingDir %A_ScriptDir%
SetKeyDelay, 0, 15 
SetMouseDelay, 30
#SingleInstance ,force


home::
WinGet, AhkPID, PID, A
WinGetClass, Chobobo, ahk_pid %AhkPID%
SplashTextOn,,, PID:%AhkPID% ACTIVATED
Sleep 2000
SplashTextOff


2::
While GetKeyState("2", "P") & #IfWinActive ahk_pid %AhkPID%
{
ControlSend, ahk_parent, 2, ahk_pid %AhkPID%
sleep, 30
ControlClick, x595 y412, ahk_pid %AhkPID%, , Left, 1, NA
sleep, 65
}
return






end::exitapp
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: make macro work only when window is active?

06 Mar 2022, 08:37

Your #IfWinActive has to be on a seperate line above the hotkey, it will be valid from that position until the next #If or the end of script.

Compare
https://www.autohotkey.com/docs/commands/IfWinActive.htm
and
https://www.autohotkey.com/docs/commands/_IfWinActive.htm
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: make macro work only when window is active?  Topic is solved

06 Mar 2022, 08:39

Code: Select all

Home::
SoundBeep, 1500
WinGet, AhkPID, PID, A
ToolTip, PID: %AhkPID% ACTIVATED
Sleep, 2000
ToolTip
Return

#If WinActive("ahk_pid" AhkPID)
$2::
ToolTip, 2 is pressed
While GetKeyState("2", "P")
{
 ControlSend, ahk_parent, 2, ahk_pid %AhkPID%
 Sleep, 30
 ControlClick, x595 y412, ahk_pid %AhkPID%,, Left, 1, NA
 Sleep, 65
}
ToolTip
Return
#If

End::ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GooGooPark and 147 guests