Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
14 ratings:
 4.28 out of 5   

How Can I Programmatically Simulate A Mouse Click?



Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 1.0
Primary Software Fixed Version: N/A
Secondary Software: N/A

Problem:
I would like to programmatically control my mouse so that I can move to a specific control and click on it without user interaction.  This would allow me to use a Mouse Up or Mouse Down Event without using Value Signaling or User Events.  It will also allow me to see the behavior of the mouse on the front panel.

Solution:

This will involve calling Windows' functions from the User32.dll using a "Call Library Function Node".  First, the power-off mode must be terminated in case a screensaver is running.  This is done using the function SystemParametersInfoA in the User32.dll.  



After you do this, you should give the system a delay in order for it to respond before the next step.



You then move the mouse by specifying horizontal and vertical positions in pixel coordinates with the SetCursorPos function in the same library.   

The mouse click and release can then be specified.  This will be set by using the mouse_event function of the User32.dll.  The click and release must be programmed with two separate function calls.  It is good to have a small delay, such as 10 milliseconds, in between them.

Using this method will allow mouse events to be fired with the event structure.  If the mouse coordinates are set so that they correspond to the position of a control, an event can be fired as if the user had clicked on the control.

The example below uses the MOUSEEVENTF_LEFTDOWN and MOUSEEVENTF_LEFTUP flags to simulate a left-click and release which correspond to 0x2 and 0x4 respectively.  For right-clicks, use the MOUSEEVENTF_RIGHTDOWN (0x8) and MOUSEEVENTF_RIGHTUP (0x10) flags.

ButtonExample.zip shows how to simulate a mouse click on a specific button by creating a reference to a control and using property nodes to extract the coordinates of that control.



Related Links:
KnowledgeBase 0YR9TGW1: How Do I Run an Executable or External File From Within LabVIEW?
Microsoft Developer Network: mouse_event Function

Attachments:
ButtonExample.zip




Report Date: 06/20/2007
Last Updated: 08/28/2017
Document ID: 4AJIL2LC

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit