How do I combine this toggle

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jellopudding
Posts: 6
Joined: 04 Aug 2016, 11:30

How do I combine this toggle

Post by jellopudding » 27 Mar 2024, 01:56

Code: Select all

#Requires AutoHotkey >=2.0
#SingleInstance Force
#HotIf WinActive("ahk_exe Chrome.exe")

b::
{
    static toggle := 1
    toggle := !toggle
}


v::
{	
	If toggle
	{
	Loop
		{
		Send ("{z down}")
		Sleep 30
		Send ("{z up}")
		If not GetKeyState("v", "P")
		Break
		}
	else
		Send "V"
	}
}

Thank you

I would like for "b" to act as a toggle and for "v" to rapid press z if toggle is on, and as regular v when toggle is off.

reddyshyam
Posts: 38
Joined: 24 Jul 2023, 04:34

Re: How do I combine this toggle

Post by reddyshyam » 27 Mar 2024, 02:36

Hi @jellopudding

Please use Global variable instead of static.

Code: Select all

#Requires AutoHotkey >=2.0
#SingleInstance Force
#HotIf WinActive("ahk_exe Chrome.exe")

toggle := true

b::{
    Global toggle := !toggle
}


v::{	

	if toggle {
		Loop {
			Send ("{z down}")
			Sleep 30
			Send ("{z up}")
			If not GetKeyState("v", "P")
				Break
		}
	} else 
		Send "V"
}

jellopudding
Posts: 6
Joined: 04 Aug 2016, 11:30

Re: How do I combine this toggle

Post by jellopudding » 27 Mar 2024, 08:47

Thank you. Tried global toggles a few times but couldn't figure out where to place it to make it work (and kept reading not to use it when I looked it up).

niCode
Posts: 295
Joined: 17 Oct 2022, 22:09

Re: How do I combine this toggle

Post by niCode » 27 Mar 2024, 13:17

For fun, and because I don't like globals, here's a way to do it without global variables:

Code: Select all

#Requires AutoHotkey >=2.0
#SingleInstance Force


#HotIf WinActive('ahk_exe Chrome.exe')
$b::ChromeToggle(true)


#HotIf ChromeToggle()
$v:: {
    SetKeyDelay(, 30)
    while GetKeyState('v', 'P') {
        SendEvent('z')
	}
}
#HotIf


ChromeToggle(flip := false) {
    static toggle := false
    flip ? toggle := !toggle : 0
    return toggle and WinActive('ahk_exe Chrome.exe')
}

Post Reply

Return to “Ask for Help (v2)”