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

时间:2013-03-27 14:45:51

标签: autohotkey

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

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

该脚本的目的是:

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

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

现在,在它的当前形式中,定义了热键,它运行得很好。

我的问题是:

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

2 个答案:

答案 0 :(得分:2)

正如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

答案 1 :(得分:1)

看看#Persistent,它将使脚本继续运行。

如果此指令存在于脚本中的任何位置,则该脚本将在自动执行部分(脚本的顶部)完成后保持运行

相关问题