firebush
firebush

Reputation: 5850

How to toggle between two Autohotkey configurations

I need to toggle between two AutoHotkey key mapping configurations. I'd like to toggle the two through F3. From my research online and on StackOverflow, I think the below should do what I want:

#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

However, the #If next_make_mac directive always evaluates as true. I'm not sure why this is. In fact, even if I slip in a next_make_mac := false it still evaluates to true. Is there a better way to do what I'm doing?

I'm running AutoHotkey 1.1.21.03

Upvotes: 1

Views: 238

Answers (2)

Travis Quick
Travis Quick

Reputation: 71

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

Upvotes: 1

Sid
Sid

Reputation: 1719

First of all, the message-boxes you have inside the #If statements will not behave as expected. The first one, on line 7 will always go off, telling you it's Setting Mac settings. Your hotkeys however, will be setup properly.

I think this is due to the auto-exec section reaching all the way to the first hotkey it finds.

Only put hotkeys inside #If statements.


Next, in your first #If statement, you're checking if next_make_mac contains anything other than false or 0. Meaning the string "false", will evaluate to true.

Note, false in AHK is the same as 0.

In your second #If statement, you're checking if next_make_mac contains either false or 0.


As for the toggling, since you can't change the values of variables directly inside #If statements, you'll have to add that to your F3 hotkey.

Like this:

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

That line will toggle next_make_mac, assuming it contains either true, false, 1 or 0.


So, make sure you only have hotkeys inside the #If statements, and pass 1 or 0 as params instead of true or false so you don't accidentally use a string, and your script should work as expected.


Here's a full example of the changes:

#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

Edit: While outside the scope of the question, you can also add #SingleInstance, force at the top of your script to get rid of that dialog that asks you if you want to restart the script every time you press F3.

Upvotes: 5

Related Questions