【问题标题】:Simple AHK script not working简单的 AHK 脚本不起作用
【发布时间】:2016-07-13 05:27:07
【问题描述】:

我已经阅读了很多关于 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.

【问题讨论】:

    标签: c++ string replace autohotkey


    【解决方案1】:

    执行多行的热键(或热字串)必须在热键(或热字串)下方列出其第一行。 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
    

    【讨论】:

    • 非常感谢!我将使用这个例子来做 while 和 foreach 循环:)
    • 如果您使用 send raw,则不需要使用剪贴板来发送文本(例如,请参阅我的替代答案)。
    【解决方案2】:

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

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

    【讨论】:

    • 刚刚评论说没有为我做标签,但我测试错了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多