Page 1 of 1

Making a key input rotate through four outputs

Posted: 22 May 2019, 14:19
by Enterusernamehere
Hi, I've been dabbling with code with ahk but running into issues, namely with duplicate hotkeys

What I'm trying to do is have my space bar (or any other key) rotate through four different outputs after being pressed before looping back to the first again, below is my awful code, any pointers?

Code: Select all

Loop
{Space::Send,1
if IsDone {
    Space::Send,2
} 
if IsDone {
    Space::Send,3
} 
if IsDone {
    Space::Send,4
}
}

Re: Making a key input rotate through four outputs

Posted: 22 May 2019, 14:32
by Sam_

Code: Select all

Idx:=1
Space::
	Send,%Idx%
	Idx:=(Idx=4?1:Idx+1)
Return
or even shorter

Code: Select all

Idx:=0
Space::Send,% Idx:=(Idx=4?1:Idx+1)
both of which are just shorthand forms of

Code: Select all

Idx:=1
Space::
	Send,%Idx%
	If (Idx=4)
		Idx:=1
	Else
		Idx:=Idx+1
Return

Re: Making a key input rotate through four outputs

Posted: 22 May 2019, 14:39
by Enterusernamehere
Thanks for this! Could we go one step further and make the first button press into a mouseclick before going to A then 1 then 2 and back to mouseclick?
1-2-3-4 were placeholders, sorry I should have been more specific

Re: Making a key input rotate through four outputs

Posted: 22 May 2019, 15:02
by Sam_
Enterusernamehere wrote:
22 May 2019, 14:39
Thanks for this! Could we go one step further and make the first button press into a mouseclick before going to A then 1 then 2 and back to mouseclick?
1-2-3-4 were placeholders, sorry I should have been more specific
More like this?

Code: Select all

WhatToSend:=["A",1,2]
Idx:=0
Space::
	If (Idx=0)
		{
		MouseClick ; https://autohotkey.com/docs/commands/MouseClick.htm
		Idx+=1
		}
	Else If (Idx<3)
		{
		Send,% WhatToSend[Idx]
		Idx+=1
		}
	Else
		{
		Send,%  WhatToSend[Idx]
		Idx:=0
		}
Return
Either replace the values in the array WhatToSend with what you want to send, or replace WhatToSend[Idx] with whatever other variable whose contents you want to send.

Re: Making a key input rotate through four outputs

Posted: 22 May 2019, 15:23
by Enterusernamehere
This is perfect, thank you!