简单的AHK脚本不起作用

Simple AHK script not working

本文关键字:不起作用 脚本 AHK 简单      更新时间:2023-10-16

我在AHK上读了很多页面,但没有找到任何解释如何制作一个脚本的页面,当它被以下内容键入时,我可以替换"for":

for(int i=0;i<CONDITION;i++)
{
}

我希望它将光标焦点设置在括号内,以便立即开始编写循环代码。

以下是我到目前为止的想法:

::for::for(int i=0;i<CONDITION;i++),
{,
    ,
}

应该将"for"替换为帖子顶部的代码,但会出现以下错误:

Error at line 2.
linetext: ,,
Error: this line does not contain recognised action.
The program will exit.

执行多行的热键(或hotstring)必须在热键(或hotstring)下列出其第一行。https://autohotkey.com/docs/FAQ.htm#autoexec

逗号、分号和其他字符,如{}^!+#在AHK中具有特殊含义,需要转义,以便与正常情况下进行不同的解释。https://autohotkey.com/docs/commands/_EscapeChar.htm

::for::for(int i=0`;i<CONDITION`;i`{+`}`{+`})`n`{`{`}`n`n`{`}`}{Up}

发送此类文本的最简单方法是:

; #If WinActive("ahk_class Notepad")
::for::
ClipSaved := ClipboardAll  ; save clipboard
clipboard := ""            ; empty clipboard
clipboard =                ; send this text to the clipboard:
(
for(int i=0;i<CONDITION;i++)
{
}
)
ClipWait, 1               ; wait for the clipboard to contain data
Send, ^v
Send, {Up}
clipboard := ClipSaved    ; restore original clipboard
return
; #If

这种方法也相当简单,它在Scite和Notepad++中运行良好,可以自动处理选项卡:

::for::
SendRaw,
(
For(int i=0;i<CONDITION;i++)
{
}
)
Send, {Up}{End}
return