Wait for leftclick then continue script

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
simonwilliams
Newbie
Posts: 19
Joined: Tue Feb 20, 2007 9:43 am

Wait for leftclick then continue script

Post by simonwilliams » Wed Sep 22, 2010 9:53 am

I need to be able to have a left click in the middle of my macro, but where the left click takes place will be different each time.

I need the macro to run, get to a point where it waits for the left click & then once clicked it carries on the script?.

Onevent maybe but I couldn't get it to work.

Any ideas please?
Simon Williams

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Detect left mouse clicks

Post by adroege » Wed Sep 22, 2010 1:56 pm

Try this:

Code: Select all

//Detect left mouse clicks
//Click the left mouse button and watch the message popup

Label>Loop
  LibFunc>user32,GetAsyncKeyState,keystate,1
  Wait>0.01
  If>keystate<>0
    Message>Left mouse button clicked
    Wait>0.4
    PushButton>Macro Scheduler Message,&OK
  endif
Goto>Loop


SteenJakobsen
Pro Scripter
Posts: 110
Joined: Thu Apr 08, 2010 6:11 am
Location: Hørsholm, Denmark
Contact:

Post by SteenJakobsen » Thu Sep 23, 2010 6:53 am

Hi adroege,

Now THAT is NICE :-)

Can I somehow put it into an OnEvent handler..?


Best regards Steen
Best Regards
Steen Jakobsen
DM Software A/S

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Feb 08, 2013 9:21 pm

Hi adroege,

Thanks for you macro as posted above!

I have read up on GetAsyncKeyState here...
http://msdn.microsoft.com/en-us/library ... s.85).aspx

...but have been unable to modify your code to detect ONLY left-DOUBLE-click.

Also I note we have...

VK_LBUTTON (1) Left mouse button

...but no separate virtual-key code to "represent" a left double-click.

Is it possible to detect left-DOUBLE-click?

Or is this one of the few things that is impossible to do with Macro Scheduler?

Marcus or anyone?

Any help greatly appreciated... and take care.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

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

Post by JRL » Sat Feb 09, 2013 7:01 am

Or is this one of the few things that is impossible to do with Macro Scheduler?
.............What!!!

Just use the good ole left mouse click OnEvent> then a bunch-o-flags to control the detection of the double click.

Code: Select all

Let>ClickFlag=-1
Let>ClickCount=0
OnEvent>key_down,VK1,0,Click

SRT>Click
  If>ClickFlag<0
    Let>ClickCount=1
    Let>ClickFlag=-5
  EndIf
  If>ClickCount=2
    Let>ClickFlag=-1
    Let>ClickCount=0
    message>Double Click
    Wait>1
    CloseWindow>Macro Scheduler Message
  EndIf
END>Click

Label>Loop
  Wait>0.01
  If>ClickCount=1
    Add>ClickFlag,1
    //This ClickFlag number determines how long the wait must be between clicks
    //smaller allows a faster double click
    If>{(%ClickFlag%>5)and(%ClickCount%=1)}
      Let>ClickFlag=1
      Let>ClickCount=2
    EndIf
  EndIf
  If>ClickCount=2
    Add>ClickFlag,1
    //This ClickFlag number determines how long the wait can be between clicks
    //bigger allows a slower double click
    If>ClickFlag>45
      Let>ClickFlag=-1
      Let>ClickCount=0
      Message>reset
    EndIf
  EndIf
Goto>Loop

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Feb 11, 2013 12:48 am

Hi JRL,

Thank you very much for posting the above script. After I commented out the reset message you were popping up, I did get it working.

Then I was thinking that on a faster PC, a person would have to adjust those counter values to properly detect the double left click and I wanted a method that would not be affected by the speed of the PC... so I came up with the method below.

It uses the VBScript Timer function to retrieve the current time (in seconds past midnite and down to many decimal points) and it compares successive time samples... and if the difference in time between two successive samples is above a min and below a max time, we have "detected" a double left click.

Also, I only cared about detecting a double left click if the topmost window was a certain application... so I re-wrote the example below to work with a Notepad window... i.e. the only type of Window it will detect a double left click in is a Notepad window... (actually... any window with Notepad in its title but you get the idea...)

Code: Select all

VBSTART
VBEND

//all values in seconds
Let>min_interclick_time=0.13
Let>max_interclick_time=0.5
Let>old_left_click_time=0

OnEvent>key_down,VK1,0,Click

SRT>Click
  //Does Notepad window have focus? 
  GetActiveWindow>active_win_title,active_win_x,active_win_y
  Position>Notepad,active_win_title,1,StartPos
  If>StartPos>0
    //Yes - Notepad window has focus
    VBEval>Timer,left_click_time
    Let>time_diff=left_click_time-old_left_click_time
    If>{(%time_diff%>%min_interclick_time%)and(%time_diff%<%max_interclick_time%)}
      MDL>double left click detected in Notepad window
    Else
      //proper interclick interval for a double click not detected
      //so we save left_click_time as old_click_time for "next time"
      Let>old_left_click_time=left_click_time
    EndIf
  EndIf
END>Click

Label>Loop
Wait>0.1
//do other stuff
Goto>Loop
I also ran across a way to pull the following value from the settings on the machine... but didn't end up using this as I wanted my code to behave exactly the same over many machines regardless of what any one machine had for a double click setting. The default on the work machine turned out to be 500 (milliseconds). See code below:

Code: Select all

VBSTART
function fnGetDoubleClickTimeInMillisecondsVB()
  dim objShell
  dim vReturn
  set objShell = CreateObject("Shell.Application")
  vReturn = objShell.GetSystemInformation("DoubleClickTime")
  set objShell = nothing
  fnGetDoubleClickTimeInMillisecondsVB = vReturn
end function
VBEND

//This gets the value DoubleClickTimeInMilliseconds
//Some users may have a faster or slower setting
//for double-click on their machine
VBEval>fnGetDoubleClickTimeInMillisecondsVB,DoubleClickTimeInMilliseconds
I hope this is helpful to someone out there... and thanks again JRL!
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Mon Feb 11, 2013 11:27 am

And this is in the "Beginners" section...

Wow , I would dread to think what you guys consider advanced... :shock:

:D :D :D
Phil Pendlebury - Linktree

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