I just can't find anywhere how to automatically send a hotkey when a program starts. These simple tries don't work at all:
#IfWinActive, ahk_class Notepad
Send abcd
Send !vw
return
or
ControlSend, Edit1, This is a line of text in the notepad window., Untitled
The script should be able to:
#Persistent
SetTimer, SendHotkey, 300
return
SendHotkey:
If !WinExist("ahk_class Notepad")
return ; do nothing
; otherwise:
SetTimer, SendHotkey, off
WinActivate, ahk_class Notepad
WinWaitActive, ahk_class Notepad
Send abcd
WinWaitClose, ahk_class Notepad
SetTimer, SendHotkey, on ; repeat the action next time the program starts
return
https://autohotkey.com/docs/commands/SetTimer.htm#Examples
EDIT:
make it works every time a new window is launched, but the already opened ones won't get affect a second time:
#Persistent
SetTimer, SendHotkey, 300
return
SendHotkey:
If !WinExist("ahk_class Notepad")
return ; do nothing
; otherwise:
WinGet, Notepad_ID, list, ahk_class Notepad ; Get ID list of all opened Notepad windows
Loop, %Notepad_ID% ; retrieves each ID from the list, one at a time
{
this_Notepad_ID := Notepad_ID%A_Index% ; "A_Index" contains the number of the current loop iteration
If !InStr(Notepad_IDs, this_Notepad_ID) ; If the variable "Notepad_IDs" does not contain the current ID
{
WinActivate, ahk_id %this_Notepad_ID% ; "ahk_id" is used to identify a window based on the windows unique ID
WinWaitActive, ahk_id %this_Notepad_ID%
Send abcd
Notepad_IDs .= this_Notepad_ID ? this_Notepad_ID " " : "" ; The dot is used to concatenate (join) the IDs into a single variable. See Operators in expressions
}
}
return