0
votes

A simple code I whipped up to toggle something. I'm currently trying to make this script work on a game like Fallout4 only when it is present. Pauses automatically when it loses focus on that window.

*RButton::
*Control::
*Shift::

while (GetKeyState("Ctrl", "T") ^ GetKeyState("Shift", "T"))
{
    while (GetKeyState("RButton", "P"))
    {
        ;Some Action
    }
        ;Some Action
        return  
}
return

*F8::Suspend
*F9::Exitapp

SetTitleMatchMode, 2
#IfWinActive SomeApplication.exe
     ;Run This Script
#IfWinNotActive SomeApplication.exe
     ;Suspend+Pause This Script

return

Exit: 
    ExitApp 
Return

I'm having trouble making this part work with Fallout4, it doesn't detect it at all but works well with notepad but I would have to manually execute the part after #IfWinActive

 SetTitleMatchMode, 2
 #IfWinActive ahk_exe Fallout4.exe
         ;Run THIS Script
 #IfWinNotActive ahk_exe Fallout4.exe
         ;Suspend+Pause This Script
1

1 Answers

1
votes

All you need in this case is to make your hotkeys context-sensitive, that is, to make them only work if the defined window is active, using the #IfWinActive directive:

#IfWinActive ahk_exe Fallout4.exe

    *RButton::
    *Control::
    *Shift::

    while (GetKeyState("Ctrl", "T") ^ GetKeyState("Shift", "T"))
    {
        while (GetKeyState("RButton", "P"))
        {
            ;Some Action
        }
            ;Some Action
            return  
    }
    return

    *F8::Suspend
    *F9::Exitapp

#IfWinActive ; turn off context sensitivity

https://www.autohotkey.com/docs/commands/_IfWinActive.htm