【问题标题】:How to send hotkey when a program starts?程序启动时如何发送热键?
【发布时间】:2018-07-02 10:43:30
【问题描述】:

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

#IfWinActive, ahk_class Notepad
Send abcd
Send !vw
return

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

脚本应该能够:

  • 等待程序加载后再执行
  • 在同一程序的新窗口启动时执行
  • 但对于已经执行且仍然存在的不这样做

【问题讨论】:

    标签: load autohotkey startup hotkeys


    【解决方案1】:
    #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
    

    【讨论】:

    • 非常封闭。能不能让它在每次启动新窗口时都能正常工作,而已经打开的窗口第二次不受影响?
    • 300是执行的等待时间吗?我将其设置为 1000,但 abcd 会立即发送到记事本。当我将所有记事本替换为 SALFRAME(作家的 ahk_class)时,它不起作用
    • 300 是period(计时器的重复时间)。如果要发送延迟一秒的命令,请在 WinActivate 之前添加Sleep 1000。我没有 Writer 程序,所以我不能说 ahk_class SALFRAME 有什么问题。尝试在其后使用Multiple Criteria(例如,WinTitle/WinClass 参数中的 ahk_exe 或 ahk_exe 路径)或WinText,以缩小对窗口的搜索范围。
    【解决方案2】:

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

    必须分开写在两个代码行中,

    如果你想在程序启动时发送文本并发送热键,那么 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 
    

    【讨论】:

    • 它几乎可以工作。每次程序启动时都能让它工作吗?此时我必须在启动程序之前启动它,完成后它会退出
    • 这有点棘手,我确实重新编辑了我的答案,你现在可以每次运行程序,如果它被激活,它会执行操作,然后它会等到它不是积极的。以便您可以再次运行它。
    • 它可以工作,但如果我切换到另一个程序(比如 Firefox)然后切换回来,它会重新应用命令。可以使每个现有窗口只应用一次吗?
    • 不使用您启动的每个记事本文件中的标题名称,您不能在自动热键中这样做, ahk_class Notepad 适用于所有记事本文件。并且无法使用这些命令编写脚本#IfWinActive#IfWinNotActive#IfWinExist#IfWinNotExist - 你需要像 WinGet、Notepad_ID 这样的命令
    • 这是在the other answer 中建议的。你能看一下它,看看它在 Writer 中失败的地方吗?谢谢。
    【解决方案3】:

    也试试这个:

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

    【讨论】:

    • 只有在虚拟桌面来回切换(使用VirtuaWin)时才与SALFRAME配合使用
    • DetectHiddenWindows OnWinShow ahk_id %lParam% 试试我编辑的答案。
    • 假设有 1、2、3 三个窗口在不同的时间以相同的顺序启动。那么它只适用于1;来回切换桌面将保持字符串发送到 1。但是如果我关闭 1,那么 2 是下一个接收这样的字符串的窗口。然后,如果 2 接近,那么 3 就是一个。无论如何,我认为这是这个特定程序的问题,我可以为此制作一个宏。非常感谢您的努力。
    • 我已经下载并测试了 LibreOfficePortable_5.4.3_MultilingualStandard 和 VirtuaWinPortable_4.4_English。在我的系统(Win10)上没有这样的问题。我的两个代码都可以在每个新创建的 Writer 窗口上完美运行,并且没有 DetectHiddenWindows 。
    • 我也不知道为什么。我在Win 7 32位。然后我会接受票数最高的答案
    【解决方案4】:
    Loop {
        WinWait, ahk_class Notepad
        WinWaitActive, ahk_class Notepad
        Send abcd
    }
    

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

    【讨论】:

    • 我打开记事本,但它不会自动在上面输入abcd。我可以做类似Send abcd !ow 的事情吗?
    • 对不起,它有效。是我把剧本放在我的另一个剧本上。但它只是一遍又一遍地发送密钥。我希望它在程序启动时只发送一次
    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2013-03-20
    • 2013-12-11
    相关资源
    最近更新 更多