如何在两个 Autohotkey 配置之间切换

How to toggle between two Autohotkey configurations

我需要在两个 AutoHotkey 键映射配置之间切换。我想通过 F3 切换两者。根据我在网上和 Whosebug 上的研究,我认为以下内容应该符合我的要求:

#ifwinactive

next_make_mac = %1%
msgbox next_make_mac: %next_make_mac%

#If next_make_mac
    msgbox Setting Mac settings.
    RAlt::Control
    Escape::Delete

    RWin::Escape
    LWin::LAlt
    LAlt::LWin
    next_make_mac := false
    msgbox Mac settings set.
#If

#If !next_make_mac
    msgbox Setting PC settings.
    Ralt::Escape
    msgbox PC settings set.
    next_make_mac := true
#If

msgbox %next_make_mac%

F3:: 
    Run %A_AhkPath% %A_ScriptName% %next_make_mac%
return

但是,#If next_make_mac 指令的计算结果始终为真。我不确定这是为什么。事实上,即使我输入 next_make_mac := false,它的计算结果仍然为真。有没有更好的方法来做我正在做的事情?

我是 运行 AutoHotkey 1.1.21.03

首先,#If 语句中的消息框不会按预期运行。第一个,第 7 行总是会熄灭,告诉你它是 Setting Mac settings。但是,您的热键将被正确设置。

我认为这是因为自动执行部分一直到达它找到的第一个热键。

仅将热键放在 #If 语句中。


接下来,在您的第一个 #If 语句中,您要检查 next_make_mac 是否包含 other 而不是 false 或 [=18] =].意思是字符串 "false" 的计算结果为真。

注意,AHK 中的 false0 相同。

在您的第二个 #If 语句中,您正在检查 next_make_mac 是否包含 false0


至于切换,因为您不能直接在 #If 语句中更改变量的值,您必须将其添加到 F3 热键.

像这样:

F3::
    next_make_mac := !next_make_mac ; Flip the value
    msgBox % next_make_mac
    Run %A_AhkPath% %A_ScriptName% %next_make_mac%
return

该行将切换 next_make_mac,假设它包含 truefalse10.


因此,请确保在 #If 语句中只有热键,并将 10 作为参数传递,而不是 truefalse所以您不会不小心使用字符串,您的脚本应该会按预期工作。


以下是更改的完整示例:

#SingleInstance, force
#ifwinactive

next_make_mac = %1%

; Check which settings we'll be using
if (next_make_mac)
    msgBox, Using Mac Settings
else
    msgBox, Using PC Settings

; Only hotkeys inside here
#If next_make_mac
    RAlt::Control
    Escape::Delete

    RWin::Escape
    LWin::LAlt
    LAlt::LWin
#If

; Only hotkeys inside here
#If !next_make_mac
    Ralt::Escape
#If

F3::
    next_make_mac := !next_make_mac ; Flip the value
    Run %A_AhkPath% %A_ScriptName% %next_make_mac%
return

编辑:虽然超出了问题的范围,但您还可以在脚本顶部添加 #SingleInstance, force 以摆脱询问您是否您想在每次按 F3.

时重新启动脚本
OS:=["Mac","PC",""],i:=0

#If (map="Mac")
RAlt::Control
Escape::Delete
RWin::Escape
LWin::LAlt
LAlt::LWin

#If (map="PC")
Ralt::Escape

#If

F3::
 map:=OS[i:=i<os.MaxIndex()?++i:1]
 tooltip,% map,10000,10000
return