Perform an action repeatedly but only while a key is pressed

Get help with using AutoHotkey and its commands and hotkeys
roysubs
Posts: 171
Joined: 29 Sep 2018, 16:37

Perform an action repeatedly but only while a key is pressed

16 May 2019, 10:07

I want to detect when a key is pressed down, then something will happen repeatedly until the key is lifted up.

So, on pressing RAlt DOWN, this triggers the repeated pressing of the space key (NOT holding space down, but Space DOWN, Space Up, Space DOWN, Space UP ... etc). Then on lifting my finger off the RAlt key (so this would be RAlt UP I guess), the action would immediately stop.

Detecting the pressing of a key is easy, but how you get a loop to run UNTIL it sees the RAlt key being UN-pressed that is confusing me.

Any ideas on this please?
Rohwedder
Posts: 2049
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Perform an action repeatedly but only while a key is pressed

16 May 2019, 14:05

Hallo,
try:

Code: Select all

RAlt::
SetTimer, Space, 100
KeyWait, RAlt
SetTimer, Space, Off
Return
Space:
    Send, {Space}
Return 
roysubs
Posts: 171
Joined: 29 Sep 2018, 16:37

Re: Perform an action repeatedly but only while a key is pressed

17 May 2019, 10:37

Hi Rohwedder,

This is not doing anything that I can see unfortunately :(
I reformatted a little and changed Space to Spacer in case it didn't like the "Space" keyword, but I can't see this doing anything when I hit RAlt. Any ideas?

Code: Select all

RAlt::
	SetTimer, Spacer, 100
	KeyWait, RAlt
	SetTimer, Spacer, Off
Return

Spacer:
    Send, {Space}
Rohwedder
Posts: 2049
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Perform an action repeatedly but only while a key is pressed

17 May 2019, 10:53

Hallo,
my German keyboard has no RAlt key.
Therefore I test with LAlt-Hotkey. Here it works.
roysubs
Posts: 171
Joined: 29 Sep 2018, 16:37

Re: Perform an action repeatedly but only while a key is pressed

18 May 2019, 03:01

Yes, that works very nicely, thank you! I guess that my right alt key actually says "Alr Gr" so maybe AutoHotkey uses a different reference for that key. Thanks again.

Return to “Ask For Help”

Who is online

Users browsing this forum: flyingDman, Geriko, Odius, RsTxSpyMaster, yonirots and 159 guests