Switch between toggles Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
philsi
Posts: 5
Joined: 08 Apr 2017, 04:50

Switch between toggles

12 Aug 2018, 08:23

Hi, i want to be able to switch between each toggle. Currently i press XButton2 to start the toggle and i would press XButton2 to stop the toggle, but i would like to also press XButton1 to break the Xbutton2 toggle and visa vera.

Code: Select all


OneToggle=0
#IfWinActive Gdk

XButton2::
if (OneToggle := !OneToggle)
SetTimer, timer, -1
return
 
#ifWinActive
; to end the key binding
 
timer:
While (OneToggle)
{  Send 8
     sleep 80
}

Return

OneToggle2=0
#IfWinActive Gdk

XButton1::
if (OneToggle2 := !OneToggle2)
SetTimer, timer2, -1
return
 
#ifWinActive
; to end the key binding
 
timer2:
While (OneToggle2)
{  Send 9
     sleep 80
}

Return

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Switch between toggles

12 Aug 2018, 09:19

youre misusing the timers and loops here. u tell a timer to run once, very nearly immediately, then enter an infinite loop right after. just.. dont do this.
what u need is a timer thats gonna tick every 80ms and run the send command once. To set it up, u first define the function that u want ur timers to carry out. Then make a bound function object out of it, storing it into a variable. U pass arguments to the function using the Bind() method. Finally u call the timer and point it to the bound function object that u had created.

Code: Select all

send8 := Func("sendKey").Bind(8)
send9 := Func("sendKey").Bind(9)

#IfWinActive Gdk
XButton1::SetTimer % send8, % (timerToggle1 := !timerToggle1) ? "80" : "Off"
XButton2::SetTimer % send9, % (timerToggle2 := !timerToggle2) ? "80" : "Off"

sendKey(key) {
	Send % key
}
philsi
Posts: 5
Joined: 08 Apr 2017, 04:50

Re: Switch between toggles

12 Aug 2018, 12:37

I tried your code but doesn't seem to switch between the timers. Basically i want to end one timer by activating another instead of having to turn the first one off with the same button (but also still want to turn them off by using the same button)

Example:

1. I press and release XButton1 which activates a timer that continuously presses key 9.
2. I press and release Xbutton2 which stops the first timer then activates a timer that continuously presses key 8.

And visa versa.
philsi
Posts: 5
Joined: 08 Apr 2017, 04:50

Re: Switch between toggles

15 Aug 2018, 08:43

Is this going to be possible?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Switch between toggles  Topic is solved

15 Aug 2018, 10:55

Code: Select all

send8 := Func("sendKey").Bind(8)
send9 := Func("sendKey").Bind(9)

#IfWinActive Gdk
XButton1::
	SetTimer % send8, % (timerToggle1 := !timerToggle1) ? "80" : "Off"
	SetTimer % send9, % (timerToggle2 := false) ? "80" : "Off"
return
XButton2::
	SetTimer % send8, % (timerToggle1 := false) ? "80" : "Off"
	SetTimer % send9, % (timerToggle2 := !timerToggle2) ? "80" : "Off"
return

sendKey(key) {
	Send % key
}
philsi
Posts: 5
Joined: 08 Apr 2017, 04:50

Re: Switch between toggles

15 Aug 2018, 20:56

Tried it again and it does work, thanks very much!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, RandomBoy and 417 guests