Toggle any number of keys with 1 simple function call.
Posted: 14 Nov 2015, 14:01
Code: Select all
toggleKeys(keys*){
for i, key in keys
if (!GetKeyState(key)) {
Send {%key% Down}
}
else {
Send {%key% Up}
}
}
Code: Select all
^Right::
toggleKeys("Up", "Right")
return
Caveat : This basically reverses whatever state the keys are in. So if you have a toggle that activates A, then another one that activates Shift + A, if you activate the A toggle then the Shift + A toggle without deactivating the previous one, only Shift will end up toggled since the 2 toggles will reverse each other's A toggle. Keys can also be toggled off individually, which means that if you had Shift + A toggled on and you were to toggle off A by pushing it, Shift would still be toggled. Activating the Shift + A binding would then toggle Shift off and toggle A on. This can lead to a lot of confusion. So be aware of that.
You can prevent toggles from interfering with each other by using the following. However, note that it does NOT take care of keys being individually toggled off.
Code: Select all
toggleKeysStrict(ByRef toggle, keys*){
toggle := !toggle
if (toggle) {
for i, key in keys
Send {%key% Down}
}
else {
for i, key in keys
Send {%key% Up}
}
}
Code: Select all
CtrlRightToggle := 0
^Right::
toggleKeysStrict(CtrlRightToggle, "Up", "Right")
return
To prevent keys from being individually turned off, if you find that scripts using "#MaxThreadsPerHotkey 2" do not work for you, you can use a while loop and a different key binding to toggle off the toggle, like so :
Code: Select all
^Right::
CtrlRightActive := !CtrlRightActive
while (CtrlRightActive) {
Send {"Up" Down}
Send {"Right" Down}
}
return
^+Right::
CtrlRightActive := 0
return