How to toggle a key?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: How to toggle a key?

Post by vmech » 19 Nov 2023, 14:49

@boiler
Well, you see how everything turned out, the almost rhetorical Sunday conversation turned out to be not so useless ;)

PS. We were taught binary mathematics back in school, in the late 80s, in computer science classes. So I've known this for a very, very long time.
Please post your script code inside [code] ... [/code] block. Thank you.

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

Re: How to toggle a key?

Post by plautus » 21 Nov 2023, 09:37

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)
}
these are a lot of brackets. quite confusing. the code doesnt work.
what i want is a infinite click space button loop. That can be toggled on and off.

thx so far

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

Re: How to toggle a key?

Post by DuckingQuack » 21 Nov 2023, 13:43

@plautus
So you want a single toggle that when activated will send left click and then space in an infinite loop?

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
InstallMouseHook
InstallKeybdHook

Esc::ExitApp

F1:: {
    Static Toggle := False
        SetTimer(FnMain, 10 * Toggle := !Toggle)
}
FnMain(){
    Click
    Send("{Space}")
    Sleep(100)
}
Best of Luck,
The Duck

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

Re: How to toggle a key?

Post by vmech » 21 Nov 2023, 23:34

plautus wrote:
21 Nov 2023, 09:37
what i want is a infinite click space button loop.
It seems like no one here except you understands what click space button means :lol:
Please post your script code inside [code] ... [/code] block. Thank you.

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

Re: How to toggle a key?

Post by boiler » 22 Nov 2023, 02:45

I believe The Duck’s code is what is desired if the Click line is removed and the hotkey is changed to !3, but I find using Alt with spam scripts is problematic, so I would also use a single key like F1 I believe what is wanted is simply to spam the space bar with it being toggled on and off with a hotkey. Although there is no need to use Sleep since the timer is being used. And to simplify that, an anonymous fat arrow function can be used:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force

Esc::ExitApp

F1:: {
	Static Toggle := False
	SetTimer () => Send('{Space}'), 100 * Toggle := !Toggle ; use lower number for faster spamming
}

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

Re: How to toggle a key?

Post by DuckingQuack » 22 Nov 2023, 08:54

@boiler
Thanks! My intention was giving Plautus something simple and easily adapted. I see now I misread the requested key press, but when I read it, I was uncertain, which is why I described what that code would do.
Best of Luck,
The Duck

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

Re: How to toggle a key?

Post by boiler » 22 Nov 2023, 14:01

Sure. It’s not a typical way of stating the key press, so that was just my interpretation, but it does seem to likely be what was meant.

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

Re: How to toggle a key?

Post by plautus » 24 Nov 2023, 15:43

I want a space button repeater

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

Re: How to toggle a key?

Post by boiler » 24 Nov 2023, 15:46

Did you try my script?

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

Re: How to toggle a key?

Post by plautus » 24 Nov 2023, 17:36

boiler wrote:
24 Nov 2023, 15:46
Did you try my script?
it doesnt do anything

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

Re: How to toggle a key?

Post by boiler » 24 Nov 2023, 19:38

That’s not true. Test it in Notepad. Press F1 and it will spam spaces until you press F1 again.

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

Re: How to toggle a key?

Post by vmech » 25 Nov 2023, 00:28

2 pages... All this looks like very thick trolling.
Please post your script code inside [code] ... [/code] block. Thank you.

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

Re: How to toggle a key?

Post by plautus » 25 Nov 2023, 17:47

it works, but in game it doesnt

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

Re: How to toggle a key?

Post by niCode » 25 Nov 2023, 18:53

plautus wrote:
25 Nov 2023, 17:47
it works, but in game it doesnt
Try SendEvent instead of Send.

prz
Posts: 6
Joined: 27 Nov 2023, 23:39

Re: How to toggle a key?

Post by prz » 27 Nov 2023, 23:50

I was trying to make a version of this to toggle shift (for running in many games) and was having some issues before I discovered "Blind mode" https://www.autohotkey.com/docs/v2/lib/Send.htm#Blind, which attempts to unset/restore the currently pressed modifiers while your Send is running.

In my case, I able to turn LShift into a toggle using the following script

Code: Select all

LShift::
{
  static toggle := 0
  Send("{Blind}{LShift " ((toggle := !toggle) ? "down" : "up") "}")
}

prz
Posts: 6
Joined: 27 Nov 2023, 23:39

Re: How to toggle a key?

Post by prz » 28 Nov 2023, 00:11

I was trying to make a W key toggle, and no matter what I do I can't seem to get it to behave correctly.

Code: Select all

w::
{
  static toggle := 0
  Send("{Blind}{w " (toggle := !toggle) ? "down" : "up" "}")
}
I tried (with and without {Blind}) all of the following w, W, ASC 119
And, based on key history, https://www.autohotkey.com/docs/v2/lib/KeyHistory.htm vk57, sc011, vk57sc011

The closest I could get is {W down} would send a capital W, but doesn't hold the key down, and ASC 119 sends a lowercase w but again does not hold it down.

Ideally I'd like this toggle to not affect other keys that are being held down (e.g. if A or LShift etc. are being held down (or not) keep them that way, if other keys are subsequently pressed/released, keep W in whatever state was set). Any idea how to go about this?

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

Re: How to toggle a key?

Post by boiler » 28 Nov 2023, 02:10

If you’re sending the same key as the hotkey (w in this case), you need to precede the hotkey label with a $ so that the hotkey doesn’t trigger itself:

Code: Select all

$w::

prz
Posts: 6
Joined: 27 Nov 2023, 23:39

Re: How to toggle a key?

Post by prz » 28 Nov 2023, 18:46

boiler wrote:
28 Nov 2023, 02:10
If you’re sending the same key as the hotkey (w in this case), you need to precede the hotkey label with a $ so that the hotkey doesn’t trigger itself:

Code: Select all

$w::
Hmm, I tried adding the $ but it still doesn't seem to be working for me. I also added some "debugging beeps" and it doesn't seem to be triggering itself when the $ isn't present. The behavior is the same with and without (I hear a higher pitched beep pressing W once and a lower pitched beep pressing it again, but the key still isn't being held down.)

Code: Select all

w::
{
  static toggle := 0
  Send("{Blind}{w " ( (toggle := !toggle) ? "down" : "up") "}")
  if (toggle)
    SoundBeep 1500
  else
    SoundBeep 500
}
Additionally, is there a way to make this trigger whenever the W key is pressed? I noticed that it does not trigger if I'm pressing Shift-W, Alt-W, etc.

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

Re: How to toggle a key?

Post by boiler » 28 Nov 2023, 19:32

Did you test it in Notepad? What did it do? By the way, holding the w will not cause it to auto-repeat like when you physically hold it down. If you want it to do that, then you need to have it send the keystroke over and over until you toggle it off.

prz wrote: Additionally, is there a way to make this trigger whenever the W key is pressed? I noticed that it does not trigger if I'm pressing Shift-W, Alt-W, etc.
Put *$ in front of the hotkey instead of just $. The * is the wildcard prefix. See Hotkey Modifier Symbols.

prz
Posts: 6
Joined: 27 Nov 2023, 23:39

Re: How to toggle a key?

Post by prz » 29 Nov 2023, 01:15

boiler wrote:
28 Nov 2023, 19:32
Put *$ in front of the hotkey instead of just $. The * is the wildcard prefix. See Hotkey Modifier Symbols.
Aha! * did seem to fix the detection, however it still doesn't seem to make the key repeat. I also tried adding a KeyWait to see if maybe the physical key release was causing issues, but still not working.

Code: Select all

*$w::
{
static KeyDown := 0
;KeyWait "w"
Send("{Blind}{w " ((KeyDown := !KeyDown) ? "down" : "up") "}")
if(KeyDown)
  SoundBeep 1500
else
  SoundBeep 500
}

Post Reply

Return to “Ask for Help (v2)”