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

时间:2018-01-23 14:57:13

标签: load autohotkey startup hotkeys

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

#IfWinActive, ahk_class Notepad
Send abcd
Send !vw
return

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

脚本应该能够:

  • 等待程序在执行前加载
  • 在启动同一程序的新窗口时执行
  • 但不会使用已执行且仍然存在的

4 个答案:

答案 0 :(得分:2)

#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

修改

每次启动新窗口时都可以使用,但已打开的窗口不会再次受到影响:

#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

答案 1 :(得分:1)

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

必须在两个代码行中单独编写,

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

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

答案 2 :(得分:1)

试试这个:

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

答案 3 :(得分:0)

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

发送与热键无关。按某些键时,热键会执行操作。像Send这样的命令只是模拟键盘输入。