Page 1 of 1

help with playing macro only once

Posted: 08 Jun 2018, 02:48
by cuaraid
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
 }


Re: help with playing macro only once

Posted: 08 Jun 2018, 07:32
by Qysh
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
 }

Re: help with playing macro only once

Posted: 08 Jun 2018, 09:21
by swagfag
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

Re: help with playing macro only once

Posted: 08 Jun 2018, 10:47
by cuaraid
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 :|

Re: help with playing macro only once

Posted: 08 Jun 2018, 10:59
by swagfag
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

Re: help with playing macro only once

Posted: 08 Jun 2018, 11:41
by cuaraid
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:

Re: help with playing macro only once  Topic is solved

Posted: 09 Jun 2018, 00:56
by Exaskryz
Use Qysh's code, but add KeyWait, \ before the return line. See KeyWait.

Re: help with playing macro only once

Posted: 09 Jun 2018, 07:33
by cuaraid
Exaskryz wrote:Use Qysh's code, but add KeyWait, \ before the return line. See KeyWait.
so simple! works like a charm, thanks!!! :bravo: