Can SendKeys be used for Mouse Left Click???

MartyMcfly

New Member
Joined
Aug 10, 2016
Messages
3
Is it not possible to use SendKeys to perform a mouse left click?
Is below the only way to accomplish this?
If the only solution is below, is there a way to modify this code so that I do not have to select the cursor position, instead having the "left click" happen wherever the cursor is at the time? Thanks.

Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cbuttons As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetCursorPos Lib "user32" (lpPoint As pointapi) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Const MOUSEEVENTF_LEFTDOWN As Integer = 2
Const MOUSEEVENTF_LEFTUP As Integer = 4
Const MOUSEEVENTF_RIGHTDOWN As Integer = 8
Const MOUSEEVENTF_RIGHTUP As Integer = 16

Private Type pointapi
X As Long
Y As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Sub test()
SetCursorPos 400, 300
Call mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) 'click left mouse
end sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
SendKeys is generally not the ultimate solution. Sort of a "I could not find another way" solution. That said, I would suggest being careful when using API, as there is not nearly the "protect me from my own errors" that there is in VBA.

To eliminate setting the cursor position, simply rid the SetCursorPos line. You probably want to release the cursor as well, assuming you are just "clicking" on something?

Rich (BB code):
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cbuttons As Long, ByVal dwExtraInfo As Long)
  
Const MOUSEEVENTF_LEFTDOWN As Integer = 2
Const MOUSEEVENTF_LEFTUP As Integer = 4
Const MOUSEEVENTF_RIGHTDOWN As Integer = 8
Const MOUSEEVENTF_RIGHTUP As Integer = 16
  
Sub test()
  Application.Wait Now + TimeSerial(0, 0, 4)        'Wait long enough so you can see the click happen
  mouse_event MOUSEEVENTF_LEFTDOWN, 0&, 0&, 0&, 0&  'Left click down
  mouse_event MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&    'release left click
End Sub

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,223,417
Messages
6,171,996
Members
452,438
Latest member
jimmyleung

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top