Need help detecting a key pressed while in loop

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Batsmaru0410
Posts: 12
Joined: 19 Jan 2019, 12:25

Need help detecting a key pressed while in loop

21 Jan 2019, 05:55

I am playing a game where I have to hit spacebar and direction arrow alternatively and repeatedly.

Because of my limited coding experience, I was only able to create a code that hit space bar and ONE direction only (either Right, Left, Up or Down by changing it in the code).

I am looking for an someone who can help me modify the code, so that when I press an arrow key while this loop is going on, it detects and change to the direction of my choice anytime I want while the loop is on going.

Thank you. Below is my code. Thank you.

Code: Select all

Delete::	
    
 	if (HitToggle := !HitToggle)
  	SetTimer, Hit, -1
  	return
  
  	Hit:
  	While (HitToggle)
  	{
	 ControlSend, ahk_parent, {Space}, Warrior
	 ControlSend, ahk_parent, {Space}, Poet
	 sleep 350
	 ControlSend, ahk_parent, {Right}, Warrior
	 ControlSend, ahk_parent, {Right}, Poet
	 sleep 350
  	 
	}
	
  	return

  	HitToggle := 0
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Need help detecting a key pressed while in loop

21 Jan 2019, 07:44

Code: Select all

#SingleInstance force

HitToggle := 0
CurrentDir := "Right"
return

Right::
Left::
Up::
Down::
	CurrentDir := A_ThisHotkey
	return

Delete::	
 	if (HitToggle := !HitToggle)
		SetTimer, Hit, -1
  	return
  
Hit:
  	While (HitToggle)
	{
		ControlSend, ahk_parent, {Space}, Warrior
		ControlSend, ahk_parent, {Space}, Poet
		sleep 350
		ControlSend, ahk_parent, % "{" CurrentDir "}", Warrior
		ControlSend, ahk_parent, % "{" CurrentDir "}", Poet
		sleep 350
	}
  	return
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Need help detecting a key pressed while in loop

21 Jan 2019, 07:47

Although it has to be said, that using a hotkey to set off a loop using a SetTimer -1 is not ideal.
It is better practice to use the timer to handle the looping, something like this:

Code: Select all

#SingleInstance force

IsSpace := 0
HitToggle := 0
CurrentDir := "Right"
return

Right::
Left::
Up::
Down::
	CurrentDir := A_ThisHotkey
	return

Delete::	
 	if (HitToggle := !HitToggle){
		SetTimer, Hit, 350	; Will start in 350ms time...
		GoSub, Hit		; ... so fire one immediately
	} else {
		SetTimer, Hit, Off
	}
  	return
  
Hit:
	if (IsSpace := !IsSpace)
		key := "Space"
	else
		key := CurrentDir
	ControlSend, ahk_parent, % "{" key "}", Warrior
	ControlSend, ahk_parent, % "{" key "}", Poet
  	return
Batsmaru0410
Posts: 12
Joined: 19 Jan 2019, 12:25

Re: Need help detecting a key pressed while in loop

21 Jan 2019, 10:19

Thank you evilC. Thanks for the help and suggestion. However, is there other way to do this without binding the arrow keys as hotkeys, since I have other window running at same time, and those arrow keys needs to be used as it is while not effecting the Poet/Warrior windows. Thank you.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Need help detecting a key pressed while in loop

21 Jan 2019, 10:28

Batsmaru0410 wrote:
21 Jan 2019, 10:19
Thank you evilC. Thanks for the help and suggestion. However, is there other way to do this without binding the arrow keys as hotkeys, since I have other window running at same time, and those arrow keys needs to be used as it is while not effecting the Poet/Warrior windows. Thank you.

Have a look at this:

https://autohotkey.com/docs/commands/_IfWinActive.htm
Batsmaru0410
Posts: 12
Joined: 19 Jan 2019, 12:25

Re: Need help detecting a key pressed while in loop

21 Jan 2019, 13:18

Thanks hellbent. But where should I put IfWinActive in the code, in the loop? or before the loop
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Need help detecting a key pressed while in loop

21 Jan 2019, 19:20

Batsmaru0410 wrote:
21 Jan 2019, 13:18
Thanks hellbent. But where should I put IfWinActive in the code, in the loop? or before the loop
You need to place your hotkeys after the " #IfWinActive , "


Like this

Code: Select all


#IfWinActive,  ; put the name of the window here

; Put the hotkeys that you want to only work when the window is active in here

Right::
Left::
Up::
Down::
	CurrentDir := A_ThisHotkey
	return

#If ; put this after your hotkeys to end the section.


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, Bing [Bot], mikeyww and 242 guests