Detecting key pressed and change variable in loop

Get help with using AutoHotkey and its commands and hotkeys
Batsmaru0410
Posts: 11
Joined: 19 Jan 2019, 12:25

Detecting key pressed and change variable in loop

19 Jan 2019, 12:43

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
Rohwedder
Posts: 3980
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Detecting key pressed and change variable in loop

19 Jan 2019, 16:36

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
Batsmaru0410
Posts: 11
Joined: 19 Jan 2019, 12:25

Re: Detecting key pressed and change variable in loop

20 Jan 2019, 05:01

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
Rohwedder
Posts: 3980
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Detecting key pressed and change variable in loop

20 Jan 2019, 09:21

Hallo,
try:

Code: Select all

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

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], jrachr, neo256, scriptor2016 and 34 guests