Run/Pause Script When an "X" window is active/inactive respectively - autohotkey

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

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

Related

How to activate button in a loop

I want to activate the button called EditBUtton2, but I'm finding it difficult. I've only been able to successfully identify it in a loop, just not activate it.
^+f7::
WinGet, OutputVar, ID
MsgBox, "%OutputVar%"
WinGet, ActiveControlList, ControlList, A
Loop, Parse, ActiveControlList, `n
{
if(A_LoopField = "EditButton2")
MsgBox, "found button successfully."
; I want to activate this button, but this doesn't work:
Send, {Space}
;controlsend,, {Space}, %A_Loopfield%
}
return

Automatically detect if a certain window becomes active?

I mean detect it without pressing a hotkey that will call the lines of code that will check it if said window is active
Below example works fine but pressing 4 is required.
4::
if WinActive("*Untitled - Notepad ahk_exe notepad.exe")
{
MsgBox, Found Notepad
}
else
{
MsgBox, Did Not Find Notepad
}
Return
I have also tried this, but it just spams me with MsgBoxes.
Loop, 50
{
if WinActive("ahk_class #32770")
{
MsgBox, Found Notepad
}
else
{
MsgBox, Did Not Find Notepad
}
}
Thanks.

WinActivate on window by process name?

Simply put, I want Ctrl+Alt+T to activate the Windows Terminal window. Previously I've used this:
^!T::
if WinExist("Windows PowerShell")
WinActivate
else
Run, wt
Return
But this doesn't cut it anymore, because the Windows Terminal changes its title when I am using Posh Git.
So I need to activate the window on whatever window that has the process name "WindowsTerminal.exe".
I've tried this but for some reason it does not recognize the correct window:
^!T::
if WinExist(ahk_exe "WindowsTerminal.exe")
WinActivate
else
Run, wt
Return
Your syntax for invoking WinExist with the name of a process/ exe is incorrect
Instead of:
if WinExist(ahk_exe "WindowsTerminal.exe")
You need to also include the ahk_exe part of it within the quotation marks.
So like this:
if WinExist("ahk_exe WindowsTerminal.exe")
Final Code:
^!T::
if WinExist("ahk_exe WindowsTerminal.exe")
WinActivate
else
Run, wt
Return
Solution:
^!T::
_WindowId = -1
WinGet _Windows, List
Loop %_Windows%
{
_Id := _Windows%A_Index%
WinGet, _PName , ProcessName, ahk_id %_Id%
if (_PName == "WindowsTerminal.exe")
{
_WindowId = %_Id%
break
}
}
if (_WindowId != -1)
{
WinActivate, ahk_id %_WindowId%
} else
{
Run, wt
}
Return
There's probably a shorter way to do this with AHK but I can't be bothered with that gross syntax anymore.

How do I send a singular key press automatically?

Update 05.08.16 - 12:08 AM:
Here is a video of me trying to get this to work: https://youtu.be/h9gAirlUGyI
So, I'm trying to have a key pressed once on program launch.
This is what I have:
{
Send, {F11}
}
Now when I launch the program it doesn't send the key.
Help?
flag = 0
processName := "xenia.exe"
Loop
{
If ProcessExist(processName)
{
WinGetTitle, title, ahk_exe %processName%
IfWinActive, %title%
{
if flag = 0
{
Send, {F11}
flag = 1
}
}
else
{
flag = 0
}
}
Sleep, 1000
}
ProcessExist(Name)
{
Process,Exist,%Name%
return Errorlevel
}
Per your other question and my comment there:
; This code both launches and fullscreens XENIA . . .
#Persistent
#SingleInstance, Force
Run, xenia.exe
Sleep, 500
GoSub, DoIt
return
numpad0::
GoSub, DoIt
return
DoIt:
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Now, if the question is REALLY about launching xenia with a file you've clicked on, e.g., using an explorer extension, there are ways of making it work (by adding the script to the registry and/or passing the file name and path into the command line. Again, please give us details as to what you want to happen!

AutoHotKey - Send singular key press on program launch

I'd like to have the F11 key pressed as soon as the program opens without having to press any hotkeys or such, basically automatic key press on launch. Here is my code:
; This section fullscreens XENIA on Play
#SingleInstance, Force
numpad0::
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Any help appreicated, thank you!
Oh, I see. Then this is how you do it. You run the function and you can also call it by pressing numpad0 (but, you have to launch this script after running the program, or see my answer on previous post):
; This section fullscreens XENIA on Play
#Persistent
#SingleInstance, Force
GoSub, DoIt
return
numpad0::
GoSub, DoIt
return
DoIt:
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Hth, lmk . . .
Maybe like this?
#Persistent
SetTimer, XeniaWatcher
XeniaWatcher() {
WinWaitActive, ahk_class XeniaWindowClass
Send, {F11}
WinWaitNotActive, ahk_class XeniaWindowClass
}
Or this?
#Persistent
SetTimer, XeniaWatcher
XeniaWatcher() {
WinWaitActive, ahk_class XeniaWindowClass
Send, {F11}
WinWaitClose, ahk_class XeniaWindowClass
}
; This section fullscreens XENIA on Play
#SingleInstance, Force
SetInterval, check, 200 ; check every 200ms
sentB4:=0
check:
If ( (WinExist("ahk_class XeniaWindowClass")) && (WinExist("ahk_exe xenia.exe")) ) {
If (sentB4 == 0) {
WinActivate, ahk_class XeniaWindowClass
If ( WinActive("ahk_class XeniaWindowClass") ) {
send, {F11} ; send desired key (fullscreen in this case)
sentB4:=1 ; update status
}
}
} else {
sentB4:=0 ; reset status
}
return
I updated your outdated IfWinExist functions because they were deprecated, read comments for more info
numpad0::
gosub, check
return
Add that for manual triggering as well...