• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

simulate a mouse click at coordinates on screen

I would like to know how to simulate a click/release, double click, right click at specified coordinates on the screen.Also how can i set cursor position to coordinates without clicking.
Thx in advance.
0
macaila
Asked:
macaila
  • 7
  • 2
  • 2
  • +1
1 Solution
 
softplusCommented:
Set cursor position:
http://www.freevbcode.com/ShowCode.Asp?ID=165

Simulating click: Sorry, don't know ...
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

Further to @softplus' URL...

To generate a click at the current pointer location:

Option Explicit

Private Declare Sub mouse_event _
                Lib "user32.dll" _
             (ByVal dwFlags As Long, _
              ByVal dx As Long, _
              ByVal dy As Long, _
              ByVal cButtons As Long, _
              ByVal dwExtraInfo As Long)
             
Private Const MOUSEEVENTF_LEFTDOWN                      As Long = &H2
Private Const MOUSEEVENTF_LEFTUP                        As Long = &H4
Private Const MOUSEEVENTF_MIDDLEDOWN                    As Long = &H20
Private Const MOUSEEVENTF_MIDDLEUP                      As Long = &H40
Private Const MOUSEEVENTF_MOVE                          As Long = &H1
Private Const MOUSEEVENTF_ABSOLUTE                      As Long = &H8000
Private Const MOUSEEVENTF_RIGHTDOWN                     As Long = &H8
Private Const MOUSEEVENTF_RIGHTUP                       As Long = &H10
Public Sub Left_Mouse_Click()

  On Error Resume Next
 
  Call mouse_event(MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&)
  Call mouse_event(MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&)
 
End Sub


BFN,

fp.
0
 
[ fanpages ]IT Services ConsultantCommented:
...Obviously just add further calls to mouse_event() using the appropriate "MOUSEEVENTF_" constant as necessary for your other requirements.

eg.

Public Sub Left_Mouse_Click()

  On Error Resume Next
 
  Call mouse_event(MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&)
  Call mouse_event(MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&)
 
End Sub
Public Sub Left_Mouse_Double_Click()

  On Error Resume Next

  Call Left_Mouse_Click
  Call Left_Mouse_Click
 
End Sub
Public Sub Right_Mouse_Click()

  On Error Resume Next
 
  Call mouse_event(MOUSEEVENTF_RIGHTDOWN, 0&, 0&, 0&, 0&)
  Call mouse_event(MOUSEEVENTF_RIGHTUP, 0&, 0&, 0&, 0&)
 
End Sub
Public Sub Right_Mouse_Double_Click()

  On Error Resume Next

  Call Right_Mouse_Click
  Call Right_Mouse_Click
 
End Sub


etc.

BFN,

fp.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
edwardiiiCommented:
Hi, macaila.

See this URL for setting mouse position programatically:

     http://www.freevbcode.com/ShowCode.Asp?ID=533

Per this EE post:
 (http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_10092464.html?query=sendkeys+mouse+click&topics=93),
you can use SendKeys to simulate mouse clicks. For example, to send a right mouse click:

     SendKeys "{Right}"

Based on that, a left-click would be:

    SendKeys "{Left}"

A double click should be:

     SendKeys "{Left}"
     SendKeys "{Left}"

However, see the EE URL above if you're trying to highlight text with the mouse actions.
0
 
[ fanpages ]IT Services ConsultantCommented:
And to move to co-ordindates X,Y:

Public Sub Move_Mouse(ByVal lngX As Long, ByVal lngY As Long)

  On Error Resume Next
 
  Call mouse_event(MOUSEEVENTF_MOVE, lngX, lngY, 0&, 0&)
 
End Sub


So... put all together...

Public Sub Move_And_Click

  Call Move_Mouse(600, 600)
  Call Left_Mouse_Click

End Sub

BFN,

fp.
0
 
softplusCommented:
cool stuff fp :)
0
 
[ fanpages ]IT Services ConsultantCommented:
Thanks :)
0
 
edwardiiiCommented:
I'd like to correct the info I posted earlier--the SendKeys Right/Left handle keyboard right/left arrow functions--apparently there is no way to use SendKeys to control the mouse.  And with the excellent Mouse Event API breakdown fanpages provided, I wouldn't want to use SendKeys even if it did work:)
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi all,

FYI:

To complement the "mouse_event" function, Microsoft chose to 'hide' the "keybd_event" function also:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/keybd_event.asp

More information on "mouse_event" is available here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputfunctions/mouse_event.asp


However, I see from the links that the "SendInput" function is now deemed more suitable in Windows NT/2000/XP:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/keyboardinput/keyboardinputreference/keyboardinputfunctions/sendinput.asp

Either will work, obviously, but if you wanted to emulate 'SendKeys' then you may use the "keybd_event" or "SendInput" functions.

A thread on the VBForums site may prove useful:

http://www.vbforums.com/showthread.php?t=323180


BFN,

fp.
0
 
GPrentice00Commented:
well put together, fp
0
 
[ fanpages ]IT Services ConsultantCommented:
Thanks.

Another question I'd forgotten about :)


If anybody wished to take the code further, you could, for example, write an automated test script for your application using these API calls to test various clicks at relevant screen locations in rapid succession without actually having to move the mouse pointer manually.

Just a thought I had after the, erm, event, and didn't mention it at the time.

Thank you for taking the trouble to clean-up, too.

BFN,

fp.
0
 
GPrentice00Commented:
>> Thank you for taking the trouble to clean-up, too.

Given that my code library or link list for future projects or upgrade enhancment ideas grows from testing some code and chasing links doing it, its not as much trouble as it is reward, and more rewarding when finding such well-packaged self-delivering comments that bring praise from fellow experts too.  
0
 
[ fanpages ]IT Services ConsultantCommented:
Thanks guys.

BFN,

fp.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now