Automatically detect if a certain window becomes active? - autohotkey

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.

Related

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.

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

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

Suspend AHK script

I'm writing a script for AHK which automate few things in my MMORPG game.
I want to perform 2 different automation when F2 or F3 is pressed.
Sometimes this script is with interaction with another action from keyboard/mouse so I need to suspend script when one of the following keys is pressed: 2,3,4,5,E,R
But when I press 1 the script need to be unsuspended.
I was trying many different options but I can't write it. I can't even google anything similiar.
My code looks like that:
key_a:="F2"
key_b:="F3"
loop {
sleep 1
if GetKeyState(key_a) {
a:=true
b:=false
}
if GetKeyState(key_b) {
a:=false
b:=true
}
if a {
MsgBox, Do something
}
if b {
MsgBox, Do somethiung else
}
}
This script language is bit strange for me.
Can you help me.
Thanks
Try:
key_a:="F2"
key_b:="F3"
SetTimer, keyStates, 100
KeyStates: ; SubRoutine
if GetKeyState(key_a, "P") {
a:=true
b:=false
}
if GetKeyState(key_b, "P") {
a:=false
b:=true
}
if a {
ToolTip, F2 is doing something
}
if b {
ToolTip, F3 is Doing Something!
}
return
2::
3::
4::
5::
E::
R::SetTimer, keyStates, Off
1::SetTimer, keyStates, on

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: Open windows explorer and wait till active window before proceeding

Really struggling with this.
I need to simply open a windows explorer window at a specified domain, wait until it is active then proceed. This is what I have so far:
#::
{
WinGet, old_active, ID, A
Run, explore C:\Users\Nathan\Documents\Test FDA
loop{
WinGet, new_active, ID, A
if(ahk_id %new_active% != ahk_id %old_active%)
{
WinMaximize, A
break
}
}
return
}
EDIT SOLVED ?>>>
DIDNT KNOW WINDOW SPY EXISTED CAME WITH IT :(((
Long time wasted, this simply works.
[::
{
Run explore C:\Users\Nathan\Documents\Test FDA
WinWaitActive Test FDA
WinMaximize A
return
}
how about this?:
"
F12::
WINDOWEXPLORER:
WinWaitActive, Windows Explorer,, 0.01
if ErrorLevel
{
Goto WINDOWEXPLORER
}
else
{
; SoundBeep 4500, 30
Return
}
"
I think you are looking for the WinWaitActive function. From the documentation:
Waits until the specified window is active
Put it after the line with Run...:
WinWaitActive, WinTitle
You will need to replace WinTitle with the title of the window that comes up after the Run command. There are other options available, such as duration to wait, titles to exclude, etc.
See the documentation for more details.