PDA

Click to See Complete Forum and Search --> : Emulate Left Mouse Button


Shivan
Mar 28th, 2001, 12:18 PM
This is for a school project. I'm building a program that receives a number from the serial port. If that number is number 4, for example, it simulates the left mouse button. So, if the mouse is over a command button, and the serial port receives the number 4, the command button will be selected (activated), as if it was clicked with the mouse. So far, all I can do is receiving that number from the serial port, through a microcontroller. The number is well sent to the PC, I've tested it. So far so good. What I can't do is simulating the mouse click, the left button. I've searched through Visual Basic 6.0 help, but can't figure out what to do! Should I use an API?? Can someone help me?? Thank you.

P.S.: Here is my program. The toggle button is to open/close the serial port. I'm reading the buffer from the serial port in a text box (more simple). Then I compare it to number 4. If it's true, how do I simulate the mouse click?? It will help if someone knows how to simulate if number 4 is pressed from keyboard, for example. The objective is to simulate the left mouse button...without using the mouse!!

Vlatko
Mar 28th, 2001, 01:44 PM
'Before you start this program, I suggest you save everything that wasn't saved yet.
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 = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Form_Activate()
Do
'Simulate a mouseclick on the cursor's position
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, cButt, dwEI
DoEvents
Loop
End Sub

Mar 28th, 2001, 03:17 PM
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202

Private Sub Command1_Click()
PostMessage mybutton.hwnd, WM_LBUTTONDOWN, 1, 0
PostMessage mybutton.hwnd, WM_LBUTTONUP, 1, 0
End Sub

gwdash
Mar 28th, 2001, 09:38 PM
it shouldn't be too hard:

'Before you start this program, I suggest you save everything that wasn't saved yet.
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 = &H2
Const MOUSEEVENTF_LEFTUP = &H4
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Form_Activate()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Do
'Simulate a mouseclick On the cursor's position
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, cButt, dwEI
DoEvents
Loop
End Sub

Modify it to fit your needs.