How to toggle a key?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

How to toggle a key?

Post by plautus » 04 Oct 2023, 14:36

I read a lot already about the instruct3ions, but the documentation is just too much, i don ty know when im done reading, and i still havent gotten to the point where i foudn the instruction to where i can do what i wanted to do.

I want a key toggle that toggles hold right mouse click and a toggle for clicking the key 'E' repeatively.

I hope someone can help.

thx

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 04 Oct 2023, 15:32

There isn't an "instruction" for everything you might want to do with the language. It's up to the user to put the code together from the basic building blocks of the language to accomplish what you want it to do. In the case of a toggle, it is accomplished by assigning a variable one value or another and having a section of code execute (or not) depending on the value of that variable.

Using very basic and easy to understand language elements, a toggle might be accomplished like this:

Code: Select all

#Requires AutoHotkey v2.0

F1:: {
	static toggle := 0
	if (toggle = 1)
		toggle := 0
	else
		toggle := 1
	ToolTip toggle
}

Then you might learn on your own or by seeing others implement a toggle that it is more efficiently accomplished like this:

Code: Select all

#Requires AutoHotkey v2.0

F1:: {
	static toggle := 0
	ToolTip toggle := !toggle
}

To do what you asked about:

Code: Select all

#Requires AutoHotkey v2.0
#MaxThreadsPerHotkey 2

F1:: {
	static toggle := 0
	Click 'R ' ((toggle := !toggle) ? 'down' : 'up')
}

F2:: {
	static toggle := 0
	toggle := !toggle
	while toggle {
		Send "e"
		Sleep 50
	}
}

plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

Re: How to toggle a key?

Post by plautus » 04 Oct 2023, 20:20

thanks, i was trying to unde5rstand the code.
why is it click 'r ' and not click, Right?

static u set it to 0, meaning u turn f1 off by default>?, and u then execute the click function by click 'r '. whats in that brace is a bit confusing.

plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

Re: How to toggle a key?

Post by plautus » 04 Oct 2023, 20:32

also, when i hold the f1 key instead of just tapping it , the loop isnt stoppable anymore

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 04 Oct 2023, 21:01

plautus wrote: thanks, i was trying to unde5rstand the code.
why is it click 'r ' and not click, Right?
Click, Right (or Click, R) is v1 syntax. Are using v2? You posted in the v2 section. That explains both the quotes and the lack of comma. The reason for the space after the R is that it is followed by down or up. Without the space, when concatenated together, it would become Rdown or Rup.

plautus wrote: static u set it to 0, meaning u turn f1 off by default>?
It just means it starts with a value of 0, so the first time it’s toggled, it becomes 1.

plautus wrote: u then execute the click function by click 'r '. whats in that brace is a bit confusing.
It’s the ternary operator that is a shorthand for if/else. If the condition is true (meaning toggle has the value 1), then the result is down, otherwise it is up.

plautus wrote: also, when i hold the f1 key instead of just tapping it , the loop isnt stoppable anymore
I believe you said you wanted to toggle each time it’s pressed. I didn’t see where you described wanting it to do something else when held down. The thread topic is about how to toggle using a key. If you want to ask a different question, please start a new thread.

plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

Re: How to toggle a key?

Post by plautus » 07 Oct 2023, 12:06

((toggle := !toggle)
can u tell me what is !toggle?

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 07 Oct 2023, 12:41

It’s just a variable. It could have any name.

plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

Re: How to toggle a key?

Post by plautus » 07 Oct 2023, 13:41

ok. is there a way to actiate the key only after I release it? bc i might press a bit longer

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 07 Oct 2023, 13:50

As described in the Hotkeys documentation: F1 Up::.

plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

Re: How to toggle a key?

Post by plautus » 17 Nov 2023, 18:50

hello, instead of right mouse click I would also like to toggle space button:

Code: Select all

!3:: {
	static toggle := 0
	Click 'space' ((toggle := !toggle) ? 'down' : 'up')
	SoundBeep 1500
}
[Mod edit: [code][/code] tags added. Please use them yourself when posting code!]

I tried adding 'space' but instead i believe it types it out.
Is the name wrong:?

User avatar
DuckingQuack
Posts: 221
Joined: 20 Jan 2023, 18:20

Re: How to toggle a key?

Post by DuckingQuack » 17 Nov 2023, 19:08

Click is almost exclusively for mouse, you'll need Send for the space bar.
https://www.autohotkey.com/docs/v2/lib/Send.htm
Best of Luck,
The Duck

plautus
Posts: 89
Joined: 20 Aug 2020, 12:24

Re: How to toggle a key?

Post by plautus » 19 Nov 2023, 08:45

I tried using brackets but still bad results:

Code: Select all

!3:: {
	static toggle := 0
	Send '{Space}' ((toggle := !toggle) ? 'down' : 'up')
	SoundBeep 1500
}

vmech
Posts: 376
Joined: 25 Aug 2019, 13:03

Re: How to toggle a key?

Post by vmech » 19 Nov 2023, 09:33

Code: Select all

!3::
{
	Static toggle := 0
	Send( "{" ( toggle := !toggle ? "Space down" : "Space up" ) "}" ) ; which means {Space down} or {Space up}, depends on toggle value
	SoundBeep(1500)
}
plautus wrote:
04 Oct 2023, 14:36
I want a key toggle that toggles hold right mouse click and a toggle for clicking the key 'E' repeatively.
All the people here are just like you, but not telepaths. Be more clear - which button, or combination of buttons, do you want to use as a toggler ?
Last edited by vmech on 19 Nov 2023, 10:27, edited 1 time in total.
Please post your script code inside [code] ... [/code] block. Thank you.

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 19 Nov 2023, 09:49

plautus wrote:
07 Oct 2023, 12:06
((toggle := !toggle)
can u tell me what is !toggle?
I realize now you were likely asking about the ! that precedes the variable toggle. It is a symbol that can be used in place of the operator not, which means to apply the logical opposite. So when the result is True or 1, then applying not or ! makes it False or 0, and vice-versa. So when the variable toggle contains the value 0 (which is the same as False), then toggle := !toggle assigns 1 (or True) to it. And the if the value was 1, it assigns it the value of 0.

vmech
Posts: 376
Joined: 25 Aug 2019, 13:03

Re: How to toggle a key?

Post by vmech » 19 Nov 2023, 10:08

@boiler
Now try to explain in the same way what this expression means toggle ^= 1 :lol:

Code: Select all

0 := !1	; 0 != 1
1 := !0	; 1 != 0
Please post your script code inside [code] ... [/code] block. Thank you.

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 19 Nov 2023, 10:59

The operator ^ is exclusive-or (xor), which has the result for a value when xor-ed with 1 of the opposite of the original value from 0 to 1 or vice-versa. The assignment operator ^= means to perform the operation on the contents of the variable and assign the result back to the same variable, so toggle ^= 1 will assign toggle the value 1 if it originally contained 0 and 0 if it originally contained 1.

vmech
Posts: 376
Joined: 25 Aug 2019, 13:03

Re: How to toggle a key?

Post by vmech » 19 Nov 2023, 11:11

@boiler
Thank you, but the proposal was rhetorical, just to freshen up this evening and this boring topic a little ;)
Please post your script code inside [code] ... [/code] block. Thank you.

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 19 Nov 2023, 11:14

I assumed you knew the answer, but I didn't know why you asked it other than to challenge me or something. Maybe make it more obvious that it's rhetorical in the future so people don't take time answering a question asked of them that no one wanted an answer to. 8-)

User avatar
DuckingQuack
Posts: 221
Joined: 20 Jan 2023, 18:20

Re: How to toggle a key?

Post by DuckingQuack » 19 Nov 2023, 11:23

@vmech Thanks for asking the (rhetorical) question.
@boiler Thanks for answering the question because I saw this used and was stumped as to what the comparison was using to compare but your explanation made it wonderfully clear.
Best of Luck,
The Duck

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: How to toggle a key?

Post by boiler » 19 Nov 2023, 13:24

DuckingQuack wrote: @boiler Thanks for answering the question because I saw this used and was stumped as to what the comparison was using to compare but your explanation made it wonderfully clear.
Glad to hear it! :thumbup:

Post Reply

Return to “Ask for Help (v2)”