条件键重映射 AHK

Conditional Key Remapping AHK

我想重新映射 'j' 键,以便它在 ergo 为真时按 n,或在 AutoHotKey 为假时按 y。例如,当我通常使用“j::n”重新映射时,shift+j 输出大写 N,其他使用 'j' 键的修饰符也是如此。但是,我下面的代码仅在没有修饰符的情况下按下字母时才有效。有没有办法解决这个问题,并有条件地使用 AutoHotKey 重新映射键?

j::
    if (ergo) ;inverted use of the ergo variable to make the code more efficient
        Send {n}
    else
        Send {y}
return

When I remap normally using "j::n" for example, shift+j outputs a capital N, and so do other modifiers with the 'j' key. However, my code below only works when the letters are pressed without modifiers.

您似乎在寻找通配符 * 修饰符。

来自docs

Wildcard: Fire the hotkey even if extra modifiers are being held down. This is often used in conjunction with remapping keys or buttons.

所以在这次更改之后,您的代码将类似于:

*j::
    if (ergo) ;inverted use of the ergo variable to make the code more efficient
        Send {n}
    else
        Send {y}
return

您只想在 { } 中的发送命令中包含 special meaning 的字符。基本上是转义,如果你熟悉那是什么。
所以你不想在 { } 中包装 ny。它甚至可以导致 undesired behavior.

有很多方法可以做到这一点。不能说哪个最好,因为不知道你的完整脚本是什么样的。
我将提出两个选项,我认为这两个选项最有可能是最适合您的方法。


首先,像您尝试过的发送命令方式。只要做对了:

*j::
    if (ergo)
        SendInput, {Blind}n
    else
        SendInput, {Blind}y
return

因此,使用 *(docs) 修饰符,这样即使按住额外的修饰符,热键也能正常工作。
然后使用 blind send mode 这样在触发热键时你可能持有的修改键将不会被释放。
也切换到 SendInput,因为它是推荐的更快、更可靠的发送模式。


第二种方法是使用 #If(docs).

创建上下文相关的热键
#If, ergo
j::n
#If
j::y

这是一种方便易行的方法。但可能会导致其他问题。
为什么? #If 有一些缺点,您可以阅读更多关于 here 的内容,但长话短说:
除非你有一个更复杂的脚本,否则你可能不会遇到任何麻烦。