# How can I simulate a left button mouse click?



## bradle95 (Jul 27, 2003)

How can I simulate a left button mouse click without specifying the handle of the button that it is clicking. The following works if I want to simulate a click on the command1 button.

y = SendMessage(command1.hwnd, WM_LBUTTONDOWN, 0, ByVal 0&)
x = SendMessage(command1.hwnd, WM_LBUTTONUP, 0, ByVal 0&)

However, I am using the SetCursorPos api, and I would just like to send a mouse down/up command to click at the point that I specify. How can I do this?


----------



## aewarnick (Sep 3, 2002)

I'm pretty sure that windows uses SendMessage to send a mouse click. SendMessage is your best choice. You can still use SetCursorPos and then send the mouse down message then a mouse up.


----------



## IMM (Feb 1, 2002)

http://msdn.microsoft.com/library/e...seinputmessages/wm_lbuttondown.asp?frame=true


----------



## bradle95 (Jul 27, 2003)

I ended up finding a mouseevent api that works jut the way I needed.

On a whole other topic, any idea how I can set multiple hotkeys for one form. I would like to be able to hit something like control F1 to do one thing, control F2 to do another... without my form having focus. I found code for a createhotkey module, but it only works if I create one hotkey, if I have more than one the form immediately closes as soon as I run it. No error or anything, it just closes.


----------



## aewarnick (Sep 3, 2002)

You can make as many hotkeys as you need. But you should post another thread for that question.


----------



## lotuseclat79 (Sep 12, 2003)

bradle95 said:


> I ended up finding a mouseevent api that works jut the way I needed.
> 
> On a whole other topic, any idea how I can set multiple hotkeys for one form. I would like to be able to hit something like control F1 to do one thing, control F2 to do another... without my form having focus. I found code for a createhotkey module, but it only works if I create one hotkey, if I have more than one the form immediately closes as soon as I run it. No error or anything, it just closes.


Hi bradle95,

Just out of curiosity, what mouseevent api did you find, and where can one get it?

-- Tom


----------



## IMM (Feb 1, 2002)

lotuseclat79 said:


> Hi bradle95,
> Just out of curiosity, what mouseevent api did you find, and where can one get it?
> -- Tom


I'm guessing that this one is a candidate?
http://msdn.microsoft.com/library/e...yboardinputfunctions/sendinput.asp?frame=true
but I play with C and am not sure how easy/difficult that is in VB


----------



## bradle95 (Jul 27, 2003)

I have used several api's for this project including:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wmsg As Long, ByVal wParam As Long, lParam As Any) 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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpclassname As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, _
ByVal ID As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long

Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, _
ByVal ID As Long) As Long

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long






I'm not really sure where I got them, I just scoured the web for examples, and massaged them to fit my application.


----------

