help with playing macro only once Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
cuaraid
Posts: 4
Joined: 08 Jun 2018, 02:35

help with playing macro only once

08 Jun 2018, 02:48

Hey, I have this double-click (I think it is double-click, lol, if it's not, please help me make it double-click) macro with random sleep times that is played repeatedly, it works just fine, but the problem is, I want it to be only played once when the key is pressed, how can I make it work? I'm sure there's a simple answer for that but I am still a newbie and can't really find anything, please help.

The script:

Code: Select all

\::
 While GetKeyState("\","P")
{   
send, {LCtrl down}
   sleep, % _ran(18, 53)
   send, {LCtrl up}
   sleep, % _ran(19, 55)
}
return

_ran(min, max)
 {
   random, ran, %min%, %max%
   return ran
 }

Qysh
Posts: 143
Joined: 24 Apr 2018, 09:16

Re: help with playing macro only once

08 Jun 2018, 07:32

Like this?

Code: Select all

\::
send, {LCtrl down}
   sleep, % _ran(18, 53)
   send, {LCtrl up}
   sleep, % _ran(19, 55)
return

_ran(min, max)
 {
   random, ran, %min%, %max%
   return ran
 }
swagfag
Posts: 3001
Joined: 11 Jan 2017, 17:59

Re: help with playing macro only once

08 Jun 2018, 09:21

heres something to suppress keyboard autorepeat, which is what i imagine youre after:

Code: Select all

#If !keyDown ; blank variables initialized as false
*q:: ; u press the key down, this label fires
	ToolTip % A_ThisHotkey " pressed!`n" A_TickCount ; ur computation goes here
	keyDown := true ; finally set this flag. This label can no longer fire
Return
#If

#If keyDown ; we're here now, flag is true, ur holding the key down and ur keyboard autorepeat keeps sending the key over and over again
*q::return ; this label will fire, we want to suppress the key, so we do
#If

; this label fires whenever u release the key unconditionally
*q Up::keyDown := false ; unset the flag

Esc::ExitApp
cuaraid
Posts: 4
Joined: 08 Jun 2018, 02:35

Re: help with playing macro only once

08 Jun 2018, 10:47

Qysh wrote:Like this?

Code: Select all

\::
send, {LCtrl down}
   sleep, % _ran(18, 53)
   send, {LCtrl up}
   sleep, % _ran(19, 55)
return

_ran(min, max)
 {
   random, ran, %min%, %max%
   return ran
 }
it doesn't really do anything for me, it keeps running, i only want it to play once when i press or hold the key, just the single senquence you know, like double-click 'x' - so i get 'xx' every time, not 'xxxxxxxx...'
swagfag wrote:heres something to suppress keyboard autorepeat, which is what i imagine youre after:

Code: Select all

#If !keyDown ; blank variables initialized as false
*q:: ; u press the key down, this label fires
	ToolTip % A_ThisHotkey " pressed!`n" A_TickCount ; ur computation goes here
	keyDown := true ; finally set this flag. This label can no longer fire
Return
#If

#If keyDown ; we're here now, flag is true, ur holding the key down and ur keyboard autorepeat keeps sending the key over and over again
*q::return ; this label will fire, we want to suppress the key, so we do
#If

; this label fires whenever u release the key unconditionally
*q Up::keyDown := false ; unset the flag

Esc::ExitApp
well, thank you, but i don't really know how to implement it into my script :|
swagfag
Posts: 3001
Joined: 11 Jan 2017, 17:59

Re: help with playing macro only once

08 Jun 2018, 10:59

replace q with a trigger key of your choosing
replace the line which reads "ur computation goes here" with the commands you want called
copy over your rand() function
get rid of the while loop, not needed
cuaraid
Posts: 4
Joined: 08 Jun 2018, 02:35

Re: help with playing macro only once

08 Jun 2018, 11:41

didn't know it was so hard to do it, nevermind, i guess i will just give up, thanks for the help guys anyways! :thumbup:
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: help with playing macro only once  Topic is solved

09 Jun 2018, 00:56

Use Qysh's code, but add KeyWait, \ before the return line. See KeyWait.
cuaraid
Posts: 4
Joined: 08 Jun 2018, 02:35

Re: help with playing macro only once

09 Jun 2018, 07:33

Exaskryz wrote:Use Qysh's code, but add KeyWait, \ before the return line. See KeyWait.
so simple! works like a charm, thanks!!! :bravo:

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], TAC109 and 78 guests