Ooker
Ooker

Reputation: 3022

How to send hotkey when a program starts?

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:

Upvotes: 2

Views: 1568

Answers (4)

Relax
Relax

Reputation: 10543

Try also this:

; DetectHiddenWindows On
Gui +LastFound
DllCall("RegisterShellHookWindow", UInt,WinExist())
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
return

ShellMessage( wParam,lParam ){
If ( wParam = 1 ) ;  1 means HSHELL_WINDOWCREATED
{           
    WinGetTitle, title, ahk_id %lParam%     
    If (title != "")
    {
        WinGetClass, class, ahk_id %lParam%
        If (class = "Notepad")
        {
            ; Sleep, 1000
            ; WinShow ahk_id %lParam%
            WinActivate, ahk_id %lParam%
            WinWaitActive, ahk_id %lParam%
            Send abcd
        }
    }
 }
}

https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/

Upvotes: 1

G. user17
G. user17

Reputation: 130

i did saw in your comment that you did want to do this send abcd !ow , but you can not [Send text] + [send Hotkey] on the same codeline,

It must be written seperate in two codelines,

And if you want to send text and send a hotkey if a program starts, then The Ahk Script must be something like this.

; [+ = Shift] [! = Alt] [^ = Ctrl] [# = Win] 
#SingleInstance force
Doloop=1
a=1
mode=1

while Doloop=1
{
#IfWinActive, ahk_class Notepad
{
if (a=1)
{
WinWaitActive, ahk_class Notepad
sleep 150 ;use this if WinWaitActive does need a litte bit more time. (WinActive-WinShowup)
sendinput, abcd
sleep 150
sendinput, !ow
a=0
}else{
sleep 150
IfWinNotActive, ahk_class Notepad
{
a=1
;WinWaitClose ;you can use this if you want, to wait until the [ahk_class Notepad] is closed.
}}}

} ; end Doloop

#if mode
esc::exitapp
#if 

Upvotes: 1

Relax
Relax

Reputation: 10543

#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

Upvotes: 3

Forivin
Forivin

Reputation: 15488

Loop {
    WinWait, ahk_class Notepad
    WinWaitActive, ahk_class Notepad
    Send abcd
}

Send had nothing to do with hotkeys. A hotkey executes an action when you press certain keys. Commands like Send simply simulate keyboard input.

Upvotes: 0

Related Questions