【问题标题】:How to toggle between two Autohotkey configurations如何在两个 Autohotkey 配置之间切换
【发布时间】:2015-04-25 19:08:05
【问题描述】:

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

#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

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

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

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

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


    接下来,在您的第一个#If 语句中,您将检查next_make_mac 是否包含除false0 之外的任何其他内容。这意味着字符串"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 的时间。

    【讨论】:

    • 谢谢西多拉!显然,我不知道#If 指令的这种性质,即禁止在其中设置变量。运行您的脚本似乎可以满足我的要求,因此我接受了这个答案。
    • @firebush 很高兴我能帮上忙。为了清楚起见,您可以在 #If 指令中运行消息框并设置变量,但前提是您通过热键执行它。否则代码被认为不可访问。
    【解决方案2】:
    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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多