【问题标题】:How can I make the script run automatically, without a hotkey being defined?如何在不定义热键的情况下使脚本自动运行?
【发布时间】:2013-03-28 02:01:48
【问题描述】:

我想创建一个“PolyEdit”脚本。 下面是脚本。

!M::
IfWinActive, ahk_class TMainForm
{
sleep 2000
Send Now is the time
Return
}

脚本的目的是:

将击键和鼠标点击发送到我的默认程序以打开文本文件。 该默认程序称为“PolyEdit”。

但我需要在没有定义热键的情况下运行脚本。

现在,以现在的形式,定义了一个热键,它运行得很好。

我的问题是:

如何在不定义热键的情况下使脚本自动运行?

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    正如 Armin 已经写的,使用#Persistent。此外,如果您想创建仅在特定应用程序处于焦点时才处于活动状态的热键,您可以执行以下操作:在这种情况下,脚本将不再在启动时执行,只有当您按下热键时...

    #Persistent
    #SingleInstance
    #IfWinActive, ahk_class TMainForm
    !M::
        sleep 2000
        Send Now is the time
    Return
    !n::SoundBeep, 500, 500
    !o::MsgBox, OK
    #IfWinActive
    

    这样所有 3 个(虚拟)热键将仅在您的应用程序处于焦点时才处于活动状态!您可以为另一个应用程序定义相同的热键,只需重复代码,但如果 #IfWinActive, ahk_class TMainForm 行中的另一个应用程序使用 ID。

    如果您想在应用程序处于活动状态时每 2 秒发送一条消息,请执行以下操作:

    #Persistent
    #SingleInstance
    SetTimerMatchMode, CheckApp, 2000
    Return
    
    CheckApp:
    IfWinActive, ahk_class TMainForm
    {
        Send, Now is the time
    }
    Return
    

    如果您想在每次(重新)激活(聚焦)您的应用程序时执行一个脚本(所以不是每两秒一次),请使用以下命令:

    #Persistent
    #installKeybdHook
    #SingleInstance
    
    Gui +LastFound 
    hWnd := WinExist()
    DllCall( "RegisterShellHookWindow", UInt,Hwnd )
    MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
    OnMessage( MsgNum, "ShellMessage" )
    Return
    
    ShellMessage( wParam )
    {
        If (wParam = 4)
        {
            IfWinActive ahk_class TMainForm
            {
                Send, Now is the time
            }
        }
    }
    Return
    

    【讨论】:

      【解决方案2】:

      看看#Persistent,它将保持脚本运行。

      如果此指令出现在脚本中的任何位置,则该脚本将在自动执行部分(脚本的顶部)完成后继续运行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        相关资源
        最近更新 更多