Blowing past key 'events'

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Fran D
Newbie
Posts: 15
Joined: Mon Aug 03, 2009 5:32 pm
Location: USA

Blowing past key 'events'

Post by Fran D » Wed Aug 05, 2009 7:00 pm

Originally I was building a script using Pro v7.4 using the WaitKeyDown>VK32 to hold off going into a loop until the user presses the SPACE bar. Very simply, its:

Code: Select all

Label>MeasureLoop 
WaitKeyDown>VK 32 
  do the measurement stuff here 
Goto>MeasureLoop 
I was building the whole thing in the script editor and was actually working very well, regardless of whether I ran from the start or stepped my way through the script.

Now, however, I seem to have lost the ability to WaitKeyDown>VK32, or any other key for that matter, as it will just blow through that point in the code and enter the loop. Whether I run from the beginning or step through the script, it seems to ignore the WaitKeyDown statement.

So, for other reasons, I have recently upgraded to Pro v11, and I still have the same issue. The script just runs right through the WaitKeyDown. I've also tried OnEvent (the reason I upgraded), but that's no better.

What would this happen? Why would these waiting for a key press statements be ignored? Is there some software 'switch' that must be set that, somehow, was inadvertently turned off? I have no idea how to attack this problem at the moment. Any help is appreciated.

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 05, 2009 8:50 pm

Unless you have a typo in the WaitKeyDown> line or your spacebar is stuck down, I don't know why WaitKeyDown> would stop pausing your script.

As for OnEvent> I'm suspecting you're trying to make it work while stepping using F8 in the editor. It won't work that way. The script has to be runing full tilt for OnEvent>KEY_DOWN to detect a key press.

You can still test OnEvent> in the editor by placing a **BREAKPOINT** in your code, then running the macro. Place the breakpoint someplace in the subroutine called by OnEvent>. Breakpoints can easily be placed in your code from the menu. They're found under "Debug" then "Insert Breakpoint". They only function in the editor, running scripts will be unaffected by breakpoints.

Here's an example. "Run" this from the editor (pick the "Run" button) then press the spacebar. The script will stop at the breakpoint. You are still in debug mode and continue through by pressing F8. Or, with MS version 11 you can even press the "Run" button again and run to another breakpoint.

Code: Select all

OnEvent>KEY_DOWN,VK32,0,SubRoutine

SRT>SubRoutine

**BREAKPOINT**
//Other stuff
Exit>0
END>SubRoutine

Label>Loop
  Wait>0.01
Goto>Loop

Fran D
Newbie
Posts: 15
Joined: Mon Aug 03, 2009 5:32 pm
Location: USA

Post by Fran D » Wed Aug 05, 2009 9:14 pm

JRL, I'll give the BREAKPOINT a try, but it's already running past the OnEvent when using RUN in the editor. I know this because it gets to the first part of my loop that changes focus to one of the programs I want to run and starts (i.e., SetFocus>ThisApp, Start). As I said, it was working like a charm previously. I can't figure out what changed.

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 05, 2009 9:26 pm

I may be misunderstanding you but...

OnEvent sets a condition that will interrupt script processing when the condition defined in the OnEvent> is met. for example when a specified key is pressed. The script processing should "blow past" the OnEvent line. It does not pause the script like WaitKeyDown.

Fran D
Newbie
Posts: 15
Joined: Mon Aug 03, 2009 5:32 pm
Location: USA

Post by Fran D » Wed Aug 05, 2009 9:34 pm

JRL wrote:I may be misunderstanding you but...

OnEvent sets a condition that will interrupt script processing when the condition defined in the OnEvent> is met. for example when a specified key is pressed. The script processing should "blow past" the OnEvent line. It does not pause the script like WaitKeyDown.
Ah, then this is a rookie mistake on my part. My guess is, then, that if I change my loop to a subroutine, then the OnEvent will work similarly to how I had WaitKeyDown protect the entrance to a loop. Let me give that a try.

User avatar
JRL
Automation Wizard
Posts: 3518
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Aug 05, 2009 10:00 pm

My guess is, then, that if I change my loop to a subroutine, then the OnEvent will work similarly to how I had WaitKeyDown protect the entrance to a loop.
That's correct.

As an attempt for clarity, lets examine the use of OnEvent>Key_Down one section at a time.

First, the Line:

Code: Select all

OnEvent>Key_Down,VK32,0,SubRoutineName
Sets a condition for the script. When this condition is met the script will immediately jump to the subroutine called by the OnEvent. For this OnEvent to function two things have to happen.

1) The script must be processing, it can't be in a "Wait" mode. If the script is "waiting" (Wait>, WaitWindowOpen>, WaitKeyDown>, etc) OnEvent events are not processed.

2) The OnEvent condition must be met. In this case the space key must be pressed.

Typically the script will be in an idling loop waiting for a OnEvent condition to be met. While in the "idling" loop, the script can be checking for and or setting conditions on the computer that might be needed by the script. For example making sure a particular window maintains focus. Or checking to see what applications are running.

Code: Select all

Label>Loop
  SetFocus>WindowThatWillBeTheSpaceKeyTrashCan
  Wait>0.01
Goto>Loop
At the moment the space key is pressed the script immediately jumps to the subroutine specified in the OnEvent line. When the subroutine has finished, unless the script is terminated during the subroutine, the script returns to the next line after the last line processed in the "idle" loop.

Code: Select all

SRT>SubRoutineName
//Do stuff
END>SubRoutineName

Put the sections together and you get pretty much the sample I posted above.

Code: Select all

OnEvent>Key_Down,VK32,0,SubRoutineName

SRT>SubRoutineName
//Do stuff
END>SubRoutineName

Label>Loop
  SetFocus>WindowThatWillBeTheSpaceKeyTrashCan
  Wait>0.01
Goto>Loop

Fran D
Newbie
Posts: 15
Joined: Mon Aug 03, 2009 5:32 pm
Location: USA

Post by Fran D » Wed Aug 05, 2009 11:19 pm

Yep, that was it! Reconfigured my measurement loop into a subroutine and OnEvent falls into it on the SPACEBAR press. The idle loop works perfectly with the dummy dialog off screen.

Now, it's just down to debugging the control of the measurement apps!

Thanks for all the help! It's all working like a charm!

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