如何在程序启动时发送热键?

How to send hotkey when a program starts?

我在任何地方都找不到如何在程序启动时自动发送热键。这些简单的尝试根本不起作用:

#IfWinActive, ahk_class Notepad
Send abcd
Send !vw
return

ControlSend, Edit1, This is a line of text in the notepad window., Untitled

脚本应该能够:

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

发送与热键无关。当您按下某些键时,热键会执行一个操作。 Send 之类的命令只是模拟键盘输入。

我确实在你的评论中看到你确实想这样做 send abcd !ow ,但是你不能在同一代码行上 [发送文本] + [发送热键],

必须分两行代码写,

如果你想在程序启动时发送文本和热键,那么 Ahk 脚本必须是这样的。

; [+ = 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 
#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

编辑:

让它在每次启动新的 window 时都有效,但已经打开的不会受到第二次影响:

#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

也试试这个:

; 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/