break loop when press key and when release resume loop Topic is solved

Ask gaming related questions (AHK v1.1 and older)
hakwish
Posts: 9
Joined: 15 May 2021, 16:30

break loop when press key and when release resume loop

Post by hakwish » 15 May 2021, 16:53

hi i have a code i use Xbutton1 , Xbutton2 and $ù to send an infinite loop
exemple : *when i presse Xbutton1 it sends {Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
what i want is : 1/ while Xbutton1 or Xbutton2 or $ù is running and i press a key on keyboard it stops the loop and send the key when i Release the key it resumes the loop againe
and also 2/ sometimes when i press Xbutton1 , Xbutton2 and $ù at the same time THE ahk script bugs and wont stop , ty for help

here is my script :

Code: Select all

*ESC:: reload
;=======================================================Mono============================================================== .

#MaxThreadsPerHotkey 3
XButton2::  ; XButton2 hotkey (change this hotkey to suit your preferences).
{
    
    if KeepXButton2Running  ; This means an underlying thread is already running the loop below.
    {
        KeepXButton2Running := false  ; Signal that thread's loop to stop.
        return  ; End this thread so that the one underneath will resume and see the change made by the line above.
    }
    ; Otherwise:
    KeepXButton2Running := true
    Loop
    {
        ; The next four lines are the action you want to repeat (update them to suit your preferences):
        ToolTip, MONO, 830, 660
        
        ; But leave the rest below unchanged.
 Sendinput {Blind}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
	Sleep, 700
        if not KeepXButton2Running  ; The user signaled the loop to stop by pressing XButton2 again.
            break  ; Break out of this loop.
    }

    KeepXButton2Running := false  ; Reset in preparation for the next press of this hotkey.
ToolTip, stoped, 830, 660 
    return
}
#MaxThreadsPerHotkey 1



;=======================================================Multi============================================================== .


#MaxThreadsPerHotkey 3
XButton1::  ; XButton1 hotkey (change this hotkey to suit your preferences).
{
    
    if KeepXButton1Running  ; This means an underlying thread is already running the loop below.
    {
        KeepXButton1Running := false  ; Signal that thread's loop to stop.
        return  ; End this thread so that the one underneath will resume and see the change made by the line above.
    }
    ; Otherwise:
    KeepXButton1Running := true
    Loop
    {
        ; The next four lines are the action you want to repeat (update them to suit your preferences):
        ToolTip, MULTI, 1050, 660 

        
        ; But leave the rest below unchanged.
 Sendinput {Blind}{Numpad0}{h}{w}{p}{j}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
	Sleep, 600
        if not KeepXButton1Running  ; The user signaled the loop to stop by pressing XButton1 again.
            break  ; Break out of this loop.
    }
    KeepXButton1Running := false  ; Reset in preparation for the next press of this hotkey.
    ToolTip, stoped, 1050, 660 
    return
}
#MaxThreadsPerHotkey 1


;=======================================================Burst============================================================ .

#MaxThreadsPerHotkey 3
$ù::  ; $ù hotkey (change this hotkey to suit your preferences).
{
    
    if Keep$ùRunning  ; This means an underlying thread is already running the loop below.
    {
        Keep$ùRunning := false  ; Signal that thread's loop to stop.
        return  ; End this thread so that the one underneath will resume and see the change made by the line above.
    }
    ; Otherwise:
    Keep$ùRunning := true
    Loop
    {
        ; The next four lines are the action you want to repeat (update them to suit your preferences):
        ToolTip, BURST, 935, 660  

        
        ; But leave the rest below unchanged.
 Sendinput {Blind}{y}{x}{m}{k}{v}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
	Sleep, 600
        if not Keep$ùRunning  ; The user signaled the loop to stop by pressing XButton1 again.
            break  ; Break out of this loop.
    }
    Keep$ùRunning := false  ; Reset in preparation for the next press of this hotkey.
    ToolTip, stoped, 935, 660 
    return
}
#MaxThreadsPerHotkey 1

tooltip,
sleep 3000
[Mod edit: [code][/code] tags added.]

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

Re: break loop when press key and when release resume loop  Topic is solved

Post by Rohwedder » 16 May 2021, 02:34

Hallo,
try:

Code: Select all

#InstallKeybdHook
*ESC:: reload
;=======================================================Mono============================================================== .
XButton2:: ; XButton2 hotkey (change this hotkey to suit your preferences).
SetTimer T1,% (T1:=!T1)?700:"Off"
Active := T1?Active "1":StrReplace(Active,1)
T1:
IF (A_TimeIdleKeyboard < 1500) Or !T1
	ToolTip, stoped, 830, 660, 1
Else If SubStr(Active, 0) = 1 ;only one timer should send at a time
{ ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, MONO, 830, 660, 1
	Sendinput {Blind}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
Return ; But leave the rest below unchanged.
;=======================================================Multi============================================================== .
XButton1:: ; XButton1 hotkey (change this hotkey to suit your preferences).
SetTimer T2,% (T2:=!T2)?600:"Off"
Active := T2?Active "2":StrReplace(Active,2)
T2:
IF (A_TimeIdleKeyboard < 1500) Or !T2
	ToolTip, stoped, 1050, 660, 2
Else If SubStr(Active, 0) = 2 ;only one timer should send at a time
{ ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, MULTI, 1050, 660, 2
	Sendinput {Blind}{Numpad0}{h}{w}{p}{j}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
Return ; But leave the rest below unchanged.
;=======================================================Burst============================================================ .
$ù:: ; $ù hotkey (change this hotkey to suit your preferences).
SetTimer T3,% (T3:=!T3)?600:"Off"
Active := T3?Active "3":StrReplace(Active,3)
T3:
IF (A_TimeIdleKeyboard < 1500) Or !T3
	ToolTip, stoped, 935, 660, 3
Else If SubStr(Active,0) = 3 ;only one timer should send at a time
{  ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, BURST, 935, 660, 3
	Sendinput {Blind}{y}{x}{m}{k}{v}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
return ; But leave the rest below unchanged.

hakwish
Posts: 9
Joined: 15 May 2021, 16:30

Re: break loop when press key and when release resume loop

Post by hakwish » 21 May 2021, 06:14

Hahahaha man your macro is MAGIC !!!!
but there is something that i want to be done to make it perfect :
1/ buttons of moovement in game such ' A Z D S ' they should not stop the sequence, they have to make exeption
2/ it has not to be a much big delay between - releasing a touche from keyboard - and -resuming the xbutton1, 2 and ù sequence - it hase to be almost instantly also when starting the sequence
3/ tooltips windows should disapear if stopped more than 5 seconds

THANK you verry much for your help

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

Re: break loop when press key and when release resume loop

Post by Rohwedder » 21 May 2021, 06:31

Now you can set the waiting time with the value of waiting_time_if_i_press_a_key_on_keyboard

Code: Select all

#InstallKeybdHook
waiting_time_if_i_press_a_key_on_keyboard = 1000 ;time/ms
*ESC:: reload
;=======================================================Mono============================================================== .
XButton2:: ; XButton2 hotkey (change this hotkey to suit your preferences).
SetTimer T1,% (T1:=!T1)?700:"Off"
Active := T1?Active "1":StrReplace(Active,1)
T1:
IF (A_TimeIdleKeyboard < waiting_time_if_i_press_a_key_on_keyboard) Or !T1
	ToolTip, stoped, 830, 660, 1
Else If SubStr(Active, 0) = 1 ;only one timer should send at a time
{ ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, MONO, 830, 660, 1
	Sendinput {Blind}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
Return ; But leave the rest below unchanged.
;=======================================================Multi============================================================== .
XButton1:: ; XButton1 hotkey (change this hotkey to suit your preferences).
SetTimer T2,% (T2:=!T2)?600:"Off"
Active := T2?Active "2":StrReplace(Active,2)
T2:
IF (A_TimeIdleKeyboard < waiting_time_if_i_press_a_key_on_keyboard) Or !T2
	ToolTip, stoped, 1050, 660, 2
Else If SubStr(Active, 0) = 2 ;only one timer should send at a time
{ ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, MULTI, 1050, 660, 2
	Sendinput {Blind}{Numpad0}{h}{w}{p}{j}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
Return ; But leave the rest below unchanged.
;=======================================================Burst============================================================ .
$ù:: ; $ù hotkey (change this hotkey to suit your preferences).
SetTimer T3,% (T3:=!T3)?600:"Off"
Active := T3?Active "3":StrReplace(Active,3)
T3:
IF (A_TimeIdleKeyboard < waiting_time_if_i_press_a_key_on_keyboard) Or !T3
	ToolTip, stoped, 935, 660, 3
Else If SubStr(Active,0) = 3 ;only one timer should send at a time
{  ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, BURST, 935, 660, 3
	Sendinput {Blind}{y}{x}{m}{k}{v}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
return ; But leave the rest below unchanged.

hakwish
Posts: 9
Joined: 15 May 2021, 16:30

Re: break loop when press key and when release resume loop

Post by hakwish » 23 May 2021, 14:56

dudue you rock !!! everything is going AMAZING but still just need the AHK to ignore these letters ' Z Q S D ' when - Holded -they have to not break the loop
exemple :( ignore_if_i_press or hold _a_key_Z_q_s_d_on_keyboard' )
i dont know how to make it work
thank you verry much for you help againe

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

Re: break loop when press key and when release resume loop

Post by Rohwedder » 24 May 2021, 03:30

Then try:

Code: Select all

#InstallKeybdHook
waiting_time_if_i_press_a_key_on_keyboard = 1000 ;time/ms
*ESC:: reload
~*z::
~*q::
~*s::
~*d::ZQSD := A_TickCount + waiting_time_if_i_press_a_key_on_keyboard
;=======================================================Mono============================================================== .
XButton2:: ; XButton2 hotkey (change this hotkey to suit your preferences).
SetTimer T1,% (T1:=!T1)?700:"Off"
Active := T1?Active "1":StrReplace(Active,1)
T1:
IF Wait() Or !T1
	ToolTip, stoped, 830, 660, 1
Else If SubStr(Active, 0) = 1 ;only one timer should send at a time
{ ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, MONO, 830, 660, 1
	Sendinput {Blind}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
Return ; But leave the rest below unchanged.
;=======================================================Multi============================================================== .
XButton1:: ; XButton1 hotkey (change this hotkey to suit your preferences).
SetTimer T2,% (T2:=!T2)?600:"Off"
Active := T2?Active "2":StrReplace(Active,2)
T2:
IF Wait() Or !T2
	ToolTip, stoped, 1050, 660, 2
Else If SubStr(Active, 0) = 2 ;only one timer should send at a time
{ ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, MULTI, 1050, 660, 2
	Sendinput {Blind}{Numpad0}{h}{w}{p}{j}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
Return ; But leave the rest below unchanged.
;=======================================================Burst============================================================ .
$ù:: ; $ù hotkey (change this hotkey to suit your preferences).
SetTimer T3,% (T3:=!T3)?600:"Off"
Active := T3?Active "3":StrReplace(Active,3)
T3:
IF Wait() Or !T3
	ToolTip, stoped, 935, 660, 3
Else If SubStr(Active,0) = 3 ;only one timer should send at a time
{  ; The next lines are the action you want to repeat (update them to suit your preferences):
	ToolTip, BURST, 935, 660, 3
	Sendinput {Blind}{y}{x}{m}{k}{v}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
}
return ; But leave the rest below unchanged.
Wait()
{	Global
	IF (ZQSD < A_TickCount)
		Return, A_TimeIdleKeyboard < waiting_time_if_i_press_a_key_on_keyboard
	Else
		Return, False
}

hakwish
Posts: 9
Joined: 15 May 2021, 16:30

Re: break loop when press key and when release resume loop

Post by hakwish » 24 May 2021, 14:37

Absolutely AMAZING , you solved my probleme at 100% i really dont know how to thank you dude , God bless your brain peace my friend

hakwish
Posts: 9
Joined: 15 May 2021, 16:30

Re: break loop when press key and when release resume loop

Post by hakwish » 27 May 2021, 06:12

hi againe, one last thing dude can we just make that ,
when Xbutton2 is running only mono 'tooltip' is showing
and when XButton1 is running only 'Multi tooltip' shows
and when $ù is running only Burst tooltip is showing
and we get rid of all ' Stopped tooltips ' so like this i will know what sequence is running without confusion
thank you

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

Re: break loop when press key and when release resume loop

Post by Rohwedder » 27 May 2021, 06:55

You're really inconveniencing me!
My mouse has neither XButton1 nor XButton2 and my keyboard no ù.
Try:

Code: Select all

#InstallKeybdHook
waiting_time_if_i_press_a_key_on_keyboard = 1000 ;time/ms
*ESC:: reload
~*z::
~*q::
~*s::
~*d::ZQSD := A_TickCount + waiting_time_if_i_press_a_key_on_keyboard
;=======================================================Mono============================================================== .
XButton2:: ; XButton2 hotkey (change this hotkey to suit your preferences).
SetTimer T1,% (T1:=!T1)?700:"Off"
IF !Active := T1?Active "1":StrReplace(Active,1)
	ToolTip
T1:
IF Wait() Or !T1 Or SubStr(Active, 0) <> 1
	Return
; The next lines are the action you want to repeat (update them to suit your preferences):
ToolTip, MONO
Sendinput {Blind}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
Return ; But leave the rest below unchanged.
;=======================================================Multi============================================================== .
XButton1:: ; XButton1 hotkey (change this hotkey to suit your preferences).
SetTimer T2,% (T2:=!T2)?600:"Off"
If !Active := T2?Active "2":StrReplace(Active,2)
	ToolTip
T2:
IF Wait() Or !T2 Or SubStr(Active, 0) <> 2
	Return
; The next lines are the action you want to repeat (update them to suit your preferences):
ToolTip, MULTI
Sendinput {Blind}{Numpad0}{h}{w}{p}{j}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
Return ; But leave the rest below unchanged.
;=======================================================Burst============================================================ .
$ù:: ; $ù hotkey (change this hotkey to suit your preferences).
SetTimer T3,% (T3:=!T3)?600:"Off"
If !Active := T3?Active "3":StrReplace(Active,3)
	ToolTip
T3:
IF Wait() Or !T3 Or SubStr(Active,0) <> 3
	Return
; The next lines are the action you want to repeat (update them to suit your preferences):
ToolTip, BURST
Sendinput {Blind}{y}{x}{m}{k}{v}{Numpad1}{Numpad2}{Numpad3}{Numpad4}{Numpad5}{Numpad6}{Numpad7}{Numpad8}{Numpad9}
return ; But leave the rest below unchanged.
Wait()
{	Global
	IF (ZQSD < A_TickCount)
		Return, A_TimeIdleKeyboard < waiting_time_if_i_press_a_key_on_keyboard
	Else
		Return, False
}

hakwish
Posts: 9
Joined: 15 May 2021, 16:30

Re: break loop when press key and when release resume loop

Post by hakwish » 27 May 2021, 10:29

so sorry dude i really appreciate your help, thank's a lot
ps : It works !!

Post Reply

Return to “Gaming Help (v1)”