Subroutine / OnEvent problem with auto triggering

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Willmaster
Newbie
Posts: 1
Joined: Thu Feb 03, 2022 6:43 pm

Subroutine / OnEvent problem with auto triggering

Post by Willmaster » Thu Feb 03, 2022 9:18 pm

I lost 3 hours...

I wanna personally RDown to trigger Subroutine with onevent, but when leave Subroutine i wanna have blocked auto triggering this onevent for 8 sec. without blocking other onevent key's. This is not my first attempt, but it only gets worse with time.
I even considered BlockInput> for a while when"OnEvent>KEY_DOWN,VK02,0," trigger... but I'd rather avoid it.

Code: Select all

// random code with some options
....
....
....

// other keys I can use and wanna use when R button is unactive.
OnEvent>...
OnEvent>...

// Is one of the possibilities I tried for active/unactive
let>r=false
label>loop_rdown
OnEvent>KEY_DOWN,VK02,0,rdown
if>r=false,loop_rdown

// No triggering event for Right mouse button with loop enabling use other onevent keys
OnEvent>KEY_DOWN,VK02,0,

// simple repeat instead of a some counter + change r=true  for  false
let>x=0
Repeat>x
let>x=x+1
until>x=8
r=false
goto>loop_rdown

srt>rdown
let>r=true
...
// fast code take 1sec and I wanna do not repeat it less than 
...
end>rdown

// Other OnEvent keys
srt>...
...
end>...

srt>...
...
end>...

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1386
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Subroutine / OnEvent problem with auto triggering

Post by Dorian (MJT support) » Fri Feb 04, 2022 10:08 am

That was a nice morning wake up call for my brain. I wrestled with a few faulty solutions before hitting on the idea of making the OnEvent disable itself in it's own subroutine, having a custom OnEvent which monitors a timer, and if the elapsed seconds are greater than 8 it will re-enable the OnEvent. The other OnEvents will still be active.

Here's a working example. I don't know if it will fit your exact scenario but it will hopefully give you enough to adapt to suit your needs.

The main trigger is simply "H". "H" will be disabled for 8 seconds once pressed. "I" is the second trigger. "I" will still work. Once "H" is enabled after 8 seconds the timer and variables are reset so it will work again.

Code: Select all

Let>elapsed_seconds=0
OnEvent>KEY_DOWN,H,0,hdown
OnEvent>KEY_DOWN,I,0,idown
OnEvent>CUSTOM,MyTriggerSub,DoIT,DoSomething


label>start
wait>0.5
If>H_Activated=1
  Timer>endTime
  Let>elapsed_seconds={(%endTime%-%startTime%)/1000}
Endif
goto>start

SRT>MyTriggerSub
  If>elapsed_seconds>8
    Let>DoIT=TRUE
  Endif
END>MyTriggerSub

SRT>DoSomething
  //Re-enable "H" onevent
  OnEvent>KEY_DOWN,H,0,hdown
  mdl>H was enabled after %elapsed_seconds% seconds
  //Reset the variables
  Let>DoIT=FALSE
  let>H_Activated=0
  Let>elapsed_seconds=0
END>DoSomething

SRT>hdown
  mdl>H was pressed (disabling H for 8 seconds)
  //This disabled THIS OnEvent - keep this at the end of this SRT
  OnEvent>KEY_DOWN,H,0,
  Timer>startTime
  Timer>endTime
  let>H_Activated=1
END>hdown

SRT>idown
  mdl>I
END>idown
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts