【问题标题】:AHK Hotkeys not allowed inside function函数内部不允许使用 AHK 热键
【发布时间】:2013-09-12 22:30:40
【问题描述】:

我想创建一个脚本,让我可以通过按热键一个一个地从数组中发送字符串。 (按一次发送第一行,再按一次发送第二行,依此类推),但我(迄今为止有限)对 AutoHotKey 的理解失败了。

这是我到目前为止所拥有的(“借用”了从 ahk - 站点构建数组的部分)

;Write to the array:
ArrayCount = 0
Loop, Read, C:\My_little_dir\test.txt{ ;test.txt contains 6-digit numbers separated only by ENTER/newline.
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Arr_Bookings%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}

element=1

Change(direction, element, ArrayCount){
    if (direction = "next"){
        ;incrementing from the last element gets us back to the first element
        if (element = %ArrayCount%)
            {element=1}
        else
            {element+=1}
    }
    else{
        if (direction = "previous"){
            ;decrementing from the first element gets us back to the last element
            if (element=0)
            {element=%ArrayCount%}
        else
            {element-=1}
        }
    }
Return Arr_Bookings%element%
}

#N::Send % Change(next,element, ArrayCount)
#B::Send % Change(previous,element, ArrayCount)

但是,当我运行它时,我收到一条错误消息:

行文本:#N::Send Change(next,element, ArrayCount)

错误:函数内部不允许使用热键/热字符串。

我一遍又一遍地检查是否有乱七八糟的花括号,但无济于事(空格没有意义……对吧?)。

你知道是什么原因造成的吗?

此外,如果您发现此代码有任何其他严重错误,请随时提及。

提前致谢! /狮子座

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    Autohotkey 不喜欢你的缩进风格。使用Allman style

    即将每个括号放在自己的行中,如果不需要,请不要使用它;例如:

    if (element = %ArrayCount%)
        {element=1}
    else
        {element+=1}
    

    这里的大括号完全是多余的。

    我通常不会这样做,但由于我已经有了代码,这里是您的展开代码:

    ;Write to the array:
    ArrayCount = 0
    Loop, Read, C:\My_little_dir\test.txt
    { ;test.txt contains 6-digit numbers separated only by ENTER/newline.
        ArrayCount += 1  ; Keep track of how many items are in the array.
        Arr_Bookings%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
    }
    
    element=1
    
    Change(direction, element, ArrayCount)
    {
        if (direction = "next")
        {
            ;incrementing from the last element gets us back to the first element
            if (element = %ArrayCount%)
                {
                element=1
                }
            else
                {
                element+=1
                }
        }
        else
        {
            if (direction = "previous")
            {
                ;decrementing from the first element gets us back to the last element
                if (element=0)
                {
                element=%ArrayCount%
                }
            else
                {
                element-=1
                }
            }
        }
    Return Arr_Bookings%element%
    }
    
    #N::Send Change(next,element, ArrayCount)
    #B::Send Change(previous,element, ArrayCount)
    

    【讨论】:

    • 来自 Python,我对这些不同的括号样式的经验有限。谢谢指点!
    猜你喜欢
    • 2018-10-10
    • 2018-01-10
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    相关资源
    最近更新 更多