Autofire and toggle

Put simple Tips and Tricks that are not entire Tutorials in this forum
Geff T
Posts: 6
Joined: 07 May 2016, 15:16

Re: Autofire and toggle

Post by Geff T » 09 May 2016, 18:25

tidbit wrote:Welcome to ahk, Geff T :)
ty

i have had to give in and ask a Q here https://autohotkey.com/boards/viewtopic ... 827#p85827but i wont go to OT
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Autofire and toggle

Post by evilC » 30 Jun 2016, 05:30

Please can we remove the GetKeyState technique for detecting release of a key? IMHO it is bad advice (Only works with one key) and is inefficient (Potentially introduces up to 10ms lag if you use a Sleep 10)

See here: https://autohotkey.com/boards/viewtopic.php?f=7&t=19745
wuuii

Re: Autofire and toggle

Post by wuuii » 05 Mar 2017, 15:01

ok but how do i change the hotkey
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Autofire and toggle

Post by tidbit » 05 Mar 2017, 16:22

please read before asking.
Helpful links:
1. Don't know how to use these? READ THIS: https://autohotkey.com/docs/Tutorial.htm
2. AHK not working in your game/program? READ THIS: https://autohotkey.com/boards/viewtopic.php?f=7&t=11084
3. List of keys to use as HOTKEYS (the line with "::"): https://autohotkey.com/docs/KeyList.htm
...
and also a link explaining.
as well as a tutorial teaching you the very basics.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
landfillbaby
Posts: 3
Joined: 27 Jan 2018, 18:56
Contact:

Re: Autofire and toggle

Post by landfillbaby » 27 Jan 2018, 19:01

Code: Select all

*Space::
Send {Space}
Sleep 250
Return
works for me (holding space presses it exactly 4 times per second), no need for a loop, i guess my keyboard works weird?
GreatGazoo
Posts: 69
Joined: 28 Dec 2017, 02:53

Re: Autofire and toggle

Post by GreatGazoo » 27 Jan 2018, 19:07

landfillbaby wrote:

Code: Select all

*Space::
Send {Space}
Sleep 250
Return
works for me (holding space presses it exactly 4 times per second), no need for a loop, i guess my keyboard works weird?

could it be that the hotkey and the key sent being the same key, that it's creating a loop on it's own
landfillbaby
Posts: 3
Joined: 27 Jan 2018, 18:56
Contact:

Re: Autofire and toggle

Post by landfillbaby » 28 Jan 2018, 02:06

GreatGazoo wrote:could it be that the hotkey and the key sent being the same key, that it's creating a loop on it's own
ok, i've just checked, it still works when the input isn't in the output

Code: Select all

Space::
Send hello
Sleep 250
Return
this would probably be a bad thing if i wasn't trying to make a loop anyway
GreatGazoo
Posts: 69
Joined: 28 Dec 2017, 02:53

Re: Autofire and toggle

Post by GreatGazoo » 28 Jan 2018, 02:50

landfillbaby wrote:
GreatGazoo wrote:could it be that the hotkey and the key sent being the same key, that it's creating a loop on it's own
ok, i've just checked, it still works when the input isn't in the output

Code: Select all

Space::
Send hello
Sleep 250
Return
this would probably be a bad thing if i wasn't trying to make a loop anyway


yeah i noticed that too with my random number generator, while holding control j it sends random number, enter, and it sends J's too

Code: Select all


^j::
    SoundBeep, 525, 100 
    Random, rand, 1, 10000
    send, %rand% {enter}
	return
	
landfillbaby
Posts: 3
Joined: 27 Jan 2018, 18:56
Contact:

Re: Autofire and toggle

Post by landfillbaby » 28 Jan 2018, 04:21

it turns out it's tied to the physical keyboard's auto-repeat feature, and is interrupted by other keypresses
i used this instead:

Code: Select all

SetKeyDelay -1
t=
Space Up::t=
Space::
t=1
While t {
Send % " "
Sleep 250
}
Return
it's basically just the toggle method but without the threading problem
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: Autofire and toggle

Post by Delta Pythagorean » 30 Jan 2018, 13:45

landfillbaby wrote:it turns out it's tied to the physical keyboard's auto-repeat feature, and is interrupted by other keypresses
i used this instead:

Code: Select all

SetKeyDelay -1
t=
Space Up::t=
Space::
t=1
While t {
	Send, % " "
	Sleep, 250
}
Return
it's basically just the toggle method but without the threading problem
If you want to use a spammer for a key, just use this. It's small, simple, and can be used anywhere in a script, not just a hotkey.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

biti
Posts: 1
Joined: 25 May 2019, 14:34

Re: Autofire and toggle

Post by biti » 25 May 2019, 15:05

hi guys i need a little script! very easy, activate/deactivate with F4, when i hold primary mousbutton pressed the mouse moves down with same speed without stop, and when i reallease the primarymousebutton it stops. can sombody help me pls?
rfxcasey
Posts: 8
Joined: 17 Jul 2020, 11:38

Re: Autofire and toggle

Post by rfxcasey » 17 Jul 2020, 19:45

I know this thread is super old but as it's about a tool it seems like to place to request support for said tool. Have a weird problem, while running the test from this tool in game, the key inputs are indeed making it in game through several commands like Send, SendInput, SendEvnt and what have you. So I have a script that sends the same command that works in the test tool but it's not working in game. Autohotkey is set to run as Admin, I'm on the latest version, I've even tried compiling the script into a .exe file but it doesn't work in game. The script in question DOES work in a text window, toggle the key repeat on and on with the push of a button and that does indeed work in notepad. So what can the problem be, is it a problem with the window the script is focusing on? I've got the game in windowed mode, everything running as admin and just won't work. As said, keystrokes are making their way into the game via the test tool just not with my script.
LeafyKeaf
Posts: 1
Joined: 06 Aug 2020, 02:37

Re: Autofire and toggle

Post by LeafyKeaf » 06 Aug 2020, 02:43

would there be a way to make it so it would click faster
ahk1
Posts: 9
Joined: 24 Sep 2020, 07:12

Re: Autofire and toggle

Post by ahk1 » 24 Sep 2020, 07:48

Hello,
If you're interested, I created a script for: Autofire toggle using the Keyboard or a Game Controller
You can use the analog thumbstick or controller keys to toggle the autofire of a key, too.
The script's post is called: Autofire toggle using Keyboard or Game Controller (can be used for Game Emulators Speed Toggle) and has the address:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=81380
william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

Re: Autofire and toggle

Post by william_ahk » 24 Dec 2020, 20:05

For the second method. If I press the hotkey 3 times during the while loop it will never stop again, is there any workaround for this?
shitcake96
Posts: 8
Joined: 02 Mar 2021, 09:43

Re: Autofire and toggle

Post by shitcake96 » 02 Mar 2021, 12:06

tidbit wrote:
17 Dec 2015, 14:33
Here are 2 of the most commonly asked things (like, 5+ times a day). I'm not here to explain them as you probably don't care (but if you do care, ask below).
Choose which one you want. If you need to modify it, see "Helpful links" below.
Please try changing stuff yourself before asking. We appreciate any effort.

1. Press-and-Hold: Send or click while a button or key is held down

Code: Select all

setKeyDelay, 50, 50
setMouseDelay, 50

$~lbutton::
	while (getKeyState("lbutton", "P"))
	{
		send, {lbutton}
		sleep, 100
	}
return
2. Toggle: Press once to activate, press again to turn it off
How do you turn off the first one? Sorry, I'm new here.
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Autofire and toggle

Post by tidbit » 02 Mar 2021, 14:46

release
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Faffy_Waffles
Posts: 1
Joined: 30 Dec 2021, 15:41

Re: Autofire and toggle

Post by Faffy_Waffles » 30 Dec 2021, 15:43

shitcake96 wrote:
02 Mar 2021, 12:06
tidbit wrote:
17 Dec 2015, 14:33
Here are 2 of the most commonly asked things (like, 5+ times a day). I'm not here to explain them as you probably don't care (but if you do care, ask below).
Choose which one you want. If you need to modify it, see "Helpful links" below.
Please try changing stuff yourself before asking. We appreciate any effort.

1. Press-and-Hold: Send or click while a button or key is held down

Code: Select all

setKeyDelay, 50, 50
setMouseDelay, 50

$~lbutton::
	while (getKeyState("lbutton", "P"))
	{
		send, {lbutton}
		sleep, 100
	}
return
2. Toggle: Press once to activate, press again to turn it off
How do you turn off the first one? Sorry, I'm new here.

After a lot of trial and error, this is what I came up with;

Code: Select all

state:=true
$f14::
state:=!state
#if (state==true)
*LShift::return
return
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Autofire and toggle

Post by tidbit » 31 Dec 2021, 14:13

it's "Press-and-Hold". to turn it 'off' you just release the key, don't hold it anymore.
if you copied both of them into your code and don't want one of them, don't copy both. Just the one you want.
if by "off" you mean you want to disable the hotkey without closing the script then do either:
- 1) right-click the scripts tray icon (by the clock. it's an H) and click "suspend hotkeys"
- or 2) use #if as Faffy did and program your own logic

... how do we not have an ahk tray icon emote like discord does.
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
Post Reply

Return to “Tips and Tricks (v1)”