Page 1 of 1

Detecting key pressed and change variable in loop

Posted: 19 Jan 2019, 12:43
by Batsmaru0410
Hi, below the loop I have now. It presses {Space} and arrow key {Right} alternatively.

I am looking for a way to modify the code slightly, so that it detects the arrow key I press while in the loop, and implement that arrow key I just pressed inside the loop without affecting {Space}.

So when is going on and I press {Left}, the loops changes to {Space} + {Left},
when I press {Right}, the loops goes to {Space} + {Right}
when I press {Up}, the loop goes to {Space} + {Up}
when I press {Down}, the loop goes to {Space} + {Down}

Thank you.

Code: Select all

Delete::	
    
 	if (HitToggle := !HitToggle)
  	SetTimer, Hit, -1
  	return
  
  	Hit:
  	While (HitToggle)
  	{
	 ControlSend, ahk_parent, {Space}, John

	 sleep 350
	 ControlSend, ahk_parent, {Right}, John

	 sleep 350
  	}

  	return

  	HitToggle := 0

Re: Detecting key pressed and change variable in loop

Posted: 19 Jan 2019, 16:36
by Rohwedder
Hallo,
try:

Code: Select all

Left::
Right::
Up::
Down::
Arrow := A_ThisHotkey
Return
Delete::
if (HitToggle := !HitToggle)
	SetTimer, Hit, -1
return
Hit:
While (HitToggle)
{
	ControlSend, ahk_parent, {Space}, John
	sleep 350
	ControlSend, ahk_parent, {%Arrow%}, John
	sleep 350
}
return
HitToggle := 0

Re: Detecting key pressed and change variable in loop

Posted: 20 Jan 2019, 05:01
by Batsmaru0410
Thank you Rohwedder for helping. I tried your code and it works.

However, since I have to use arrow key in other windows, binding the arrow keys will effect those keys in my other window. Is there another method that you don't have to bind the window keys, but detect them when pressed. Thank you.

I added Input to detect my arrow key pressed in order to change the direction , but still not working. Can you help me with this.

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, %Direction%, Warrior
	 ControlSend, ahk_parent, %Direction%, Poet
	 sleep 350
  	 
	}
	
	Input, Direction, L1, {Left}{Right}{Up}{Down}

  	return

  	HitToggle := 0

Re: Detecting key pressed and change variable in loop

Posted: 20 Jan 2019, 09:21
by Rohwedder
Hallo,
try:

Code: Select all

Input, q, T1L1, {Left}{Right}{Up}{Down}
If InStr(Errorlevel, "Endkey")
	Direction := "{" SubStr(Errorlevel,8) "}"