如何在 AutoHotkey 中将多个相似的热键压缩为一个?

How do I condense multiple similar hotkeys into one in AutoHotkey?

我正在尝试创建热键来模拟我正在玩的在线游戏中的输入缓冲,该游戏本身不支持输入缓冲,这意味着混合一个拼写键,以便在前一个拼写完成后关闭手动操作是最好的方法。

在热键的帮助下,我可以通过按住我的键以最小的延迟循环按键,以便它在我完成前一个咒语的瞬间发送。

然而,为我在键盘上绑定到一个咒语的每个按钮创建多个热键似乎很乏味,我不确定如何使用一组定义的键(即 1 到 6,和F1 到 F6)

到目前为止,我的代码示例片段仅考虑了 3 个键:

::
{
    Loop
    {
        Loop, 5
        {
            Send, 5
            Sleep, 1
        }
        GetKeyState, state, 5
        if state = U
            break
    }
    return
}

::
{
    Loop
    {
        Loop, 5
        {
            Send, 2
            Sleep, 1
        }
        GetKeyState, state, 2
        if state = U
            break
    }
    return
}

$F2::
{
    Loop
    {
        Loop, 5
        {
            Send, {F2}
            Sleep, 1
        }
        GetKeyState, state, F2
        if state = U
            break
    }
    return
}

如果可能的话,我想把它压缩成这样:

hotkeys := [5, 2, F2]
hotkeyCount := hotkeys.MaxIndex()
curKey := 1

Loop, hotkeyCount
{
    hotkeyIndex := hotkeys[curKey]
    $%hotkeyIndex%::
    {
        Loop
        {
            Loop, 5
            {
                Send, {%hotkeyIndex%}
                Sleep, 1
            }
            GetKeyState, state, %hotkeyIndex%
            if state = U
                break
        }
        return
    }
    curKey := curKey + 1
}

创建一个 FIFO 堆栈来保护预设操作并在它们准备就绪时调用它们。

数组 functions 包含以下函数:Function_aFunction_bFunction_c,它们由各自的热键 a、[=17] 触发=]、c。 热键不直接调用函数,而是将它们的数字索引添加到堆栈 stack.

计时器 checkstack 中检索数字索引,然后调用数组 functions 中该索引处的函数。当函数returns时,如果有则检索下一个索引。一次只有一个功能 运行。

SetBatchLines, -1

global stack := Object()
global stack_head = 0
global stack_tail = 0
global functions := [Func("Function_a"),Func("Function_b"),Func("Function_c")]

SetTimer, check , 25

return

check:
    if( stack_head > stack_tail )
    {
        i := stack[stack_tail]
        functions[i]()
        stack_tail++
    }
return  

Function_a()
{
    tooltip, Function_a running...
    Sleep, 1000
    tooltip, 
    return
}

Function_b()
{
    tooltip, Function_b running...
    Sleep, 1000
    tooltip,
    return
}

Function_c()
{
    tooltip, Function_c running...
    Sleep, 1000
    return
}

a::
    stack[stack_head] := 1
    stack_head++
return

s::
    stack[stack_head] := 2
    stack_head++
return

d::
    stack[stack_head] := 3
    stack_head++
return

这会启用并发 运行 函数,它们可以做任何你想做的事,同时热键可以将动作(函数索引)添加到堆栈中,这些函数将按添加的顺序调用一次一个。


我已经编辑了您的示例以使其发挥作用:

$a::
$s::
$d::
::

key := A_ThisHotkey
key :=Trim(key,"$")
Loop
{
    Loop, 5
    {
        SendInput, %key%
        Sleep, 1
    }
    state := GetKeyState(key , "P" )
    if state = 0
    {
        break
    }
}

return