Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Autohotkey 如何在不定义热键的情况下使脚本自动运行?_Autohotkey - Fatal编程技术网

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

Autohotkey 如何在不定义热键的情况下使脚本自动运行?,autohotkey,Autohotkey,我想创建一个“PolyEdit”脚本。 下面是脚本 !M:: IfWinActive, ahk_class TMainForm { sleep 2000 Send Now is the time Return } 脚本的目的是: 将按键和鼠标单击发送到我的默认程序以打开文本文件。 该默认程序称为“PolyEdit” 但是我需要脚本在没有定义热键的情况下运行 现在,以它的当前形式,定义了热键,它运行得很好 我的问题是: 如何在不定义热键的情况下使脚本自动运行?看看哪一个将保持脚本运行 如果此指令

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

!M::
IfWinActive, ahk_class TMainForm
{
sleep 2000
Send Now is the time
Return
}
脚本的目的是:

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

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

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

我的问题是:

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

看看哪一个将保持脚本运行


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

正如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
行中,则使用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
如果希望在每次(重新)激活(聚焦)应用程序时(而不是每两秒)执行脚本,请使用以下命令:

#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