Global Toggle? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Cakes
Posts: 23
Joined: 23 Apr 2018, 09:56

Global Toggle?

02 Aug 2018, 14:04

How could I make toggle global? The problem I'm having is if I don't turn the toggle off before pressing another key, it keeps going. I want to ensure when another key is pressed it turns off any previously toggled keys.

Code: Select all

*v::	
	toggle:=toggle
	if (toggle)
		ToolTip, Blah
	else
	{	
		Tooltip
	}
	while (toggle)
	{
		Send {something}
	}
return
*c::
	toggle:=toggle
	if (toggle)
		ToolTip, Blah 2
	else
	{	
		Tooltip
	}
	while (toggle)
	{
		Send {something}
	}
return
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Global Toggle?

02 Aug 2018, 15:48

Code: Select all

*v::
    ; Toggle this hotkey. The label 'SendV' will be run every 100 ms. The 'Toggle' funtion returns 1 if the current 
    ; hotkey was turned on, and 0 if the current hotkey was turned off.
    x := Toggle(A_ThisHotkey, "SendV", 100)
    if (x = 1)
        ToolTip, Blah
    else
        Tooltip
return

SendV:
    Send ____
return

*c::
    if (Toggle(A_ThisHotkey, "SendC", 200) = 1)
        ToolTip, Blah 2
    else
        Tooltip
return

SendC:
    Send ----
return

; This function will toggle the key passed to it (NewKey) and will turn off the other toggle that is currently active
; if one exists.
Toggle(NewKey, NewLabel, NewInterval)
{
    ; Saves the currently active toggle key
    static ActiveToggleKey := ""
    
    ; Saves the currently active label
    static ActiveLabel := ""
    
    ; if 'ActiveToggleKey' is not blank, then it needs to be turned off
    if (ActiveToggleKey != "")
    {
        ; Turn off the active timer
        SetTimer, % ActiveLabel, Off
    }
    
    ; If 'NewKey' is currently active then clear the 'active' variables and return 0 to indicate that it was turned off
    if (NewKey = ActiveToggleKey)
    {
        ; Clear the saved active toggle key and label
        ActiveToggleKey := ""
        ActiveLabel := ""
        
        ; Return 0 to indicate that the current label was turned off
        return 0
    }
    ; else turn on the new toggle
    else
    {
        ; Turn on the new timer
        SetTimer, % NewLabel, % NewInterval
        
        ; Update the active toggle key and label
        ActiveToggleKey := NewKey
        ActiveLabel := NewLabel
        
        ; Return 1 to indicate that the new label was turned on
        return 1
    }
}
Edit: typo
Last edited by awel20 on 02 Aug 2018, 16:12, edited 1 time in total.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Global Toggle?

02 Aug 2018, 15:55

Code: Select all

#MaxThreadsPerHotkey 2
*v::	
toggle:=!toggle
while (toggle)
	ToolTip vvvv
ToolTip
return

*c::
toggle:=!toggle
while (toggle)
	ToolTip cccc
ToolTip
return
Cakes
Posts: 23
Joined: 23 Apr 2018, 09:56

Re: Global Toggle?  Topic is solved

03 Aug 2018, 13:00

Cakes wrote:How could I make toggle global? The problem I'm having is if I don't turn the toggle off before pressing another key, it keeps going. I want to ensure when another key is pressed it turns off any previously toggled keys.

Code: Select all

#MaxThreadPerHotKey 2
toggle := 0
*v::	
	toggle := !toggle
	if (toggle)
		ToolTip, Blah
	else
	{	
		Tooltip
	}
	while (toggle)
	{
		Send {something}
	}
return
*c::
	toggle := !toggle
	if (toggle)
		ToolTip, Blah 2
	else
	{	
		Tooltip
	}
	while (toggle)
	{
		Send {something}
	}
return
#MaxThreadPerHotKey 1
This was a lot simpler than I thought. I just declared and initialize the toggle variable outside of the hotkeys.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], JKJadan, Psych0p4th and 262 guests