Trying to autorepeat pressing 2 different keys?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
peachtheleech
Posts: 1
Joined: 26 Jun 2022, 09:33

Trying to autorepeat pressing 2 different keys?

Post by peachtheleech » 26 Jun 2022, 09:49

Hello! I'm trying to simulate holding down a key or auto-clicking and would like some help.

I would like to effectively auto-press the 1 or 2 key, depending on what hotkey I press. I'm currently using ctrl+1 for 1 and ctrl+2 for 2, with ctrl+3 to pause the program, but I'm running into trouble with alternating between the two without it just doing 2 forever. Additionally, I've had times when I get stuck in an infinite loop and none of the hotkeys stop it and I don't know why (I have to reset my computer). It would be ideal to have a failsafe hotkey or a better pause feature!

So if I press Ctrl+1, I want it to go like '111111111' to infinity (or for a maximum of 2 minutes if that's helpful!). I want it to stop when I press something else, whether it's ctrl + 1 again, ctrl+ 3 or any key at all, I don't really mind! The ideal would be for any key at all to interrupt the program but I'm not sure if that's possible.

Same if I press Ctrl+2.

This is the code I found online and just altered the numbers and doubled it.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#MaxThreadsPerHotkey, 2
looping = 0

$^2:: ;<-- the hotkey with a $ so it won't re-trigger itself

keywait, 2 ;<-- make sure to let go of the 2 key

looping := !looping ;<-- is the switch to tell the loop to start or stop
if (!looping)  ;<-- if the toggle is now off don't do the loop
   Return
loop ;<-- an infinite loop with a break condition inside
{   If (!looping) ;<-- the loop will break once the looping flag is flipped off
        Break
    send, {2 down}
    ;send {k down}
    sleep, 200
}
Send, {2 up}
; send {k up}

$^1:: ;<-- the hotkey with a $ so it won't re-trigger itself

keywait, 1 ;<-- make sure to let go of the 2 key

looping := !looping ;<-- is the switch to tell the loop to start or stop
if (!looping)  ;<-- if the toggle is now off don't do the loop
   Return
loop ;<-- an infinite loop with a break condition inside
{   If (!looping) ;<-- the loop will break once the looping flag is flipped off
        Break
    send, {1 down}
    ;send {k down}
    sleep, 200
}
Send, {1 up}
; send {k up}

^3::Pause ;<-- Pauses the script

ExitApp

Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to autorepeat pressing 2 different keys?

Post by Rohwedder » 26 Jun 2022, 10:45

Hallo,
try:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#MaxThreadsPerHotkey, 1 ;Increase only if absolutely necessary
#InstallKeybdHook
#InstallMouseHook ;A_PriorKey needs this Hooks
^1 Up:: ;$ not required
^2 Up::
Key := SubStr(A_ThisHotkey, 2, 1) ;1 or 2
KeyWait, Ctrl
SetTimer, Key, 200 ;every 200 ms
Send, {1 Up}{2 Up} ;so that 1 and 2 do not remain down
Return
Key:
IF (A_PriorKey <> Key)
	SetTimer,, Off
Else
	Send, {%Key% Down}
Return
^3::
Pause,,1 ;<-- Pauses the script and 
Send, {1 Up}{2 Up} ; release pressed keys
Return

Post Reply

Return to “Ask for Help (v1)”