how can i detect which button was clicked on the mose to put in a click event?
Printable View
how can i detect which button was clicked on the mose to put in a click event?
use the
CommandButton MouseDown event
then Button refers to the button pushed
but i am making my own control and need to know how to make my own click event that has this:
Event IconClicked(button as integer)
and i need to know how to detect the button on the mouse that was clicked (either right or left) in the RaiseEvent function. I do not know how to raise the event and get the button which was pressed like this:
Public Sub Timer1_Timer()
RaiseEvent IconClicked(which button was clicked)
End Sub
thankyou
Edited by srhutch on 02-26-2000 at 10:14 PM
On a mouse down or mouse up event VB returns a value for the variable 'button'
If button = 1 then the left button was pushed
If button = 2 then the right button was pushed
If button = 3 then the centre button was pushed
I used this last week and it works great. I'm reasonably sure that the values are correct. I mas=y have them mixed up. Check them out
If you feel confused when due with all the number, why not using this:
vbRightButton
vbMiddleButton
vbLeftButton
srhutch:
I don't understand why they didn't make "Button" available to Click() as well. Just make a dim called MouseButton:
Code:'FORM DECLARATION
Dim MouseButton As Integer
'MOUSEDOWN EVENT
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then 'LEFT BUTTON
MouseButton = 1
ElseIf Button = 2 Then 'RIGHT BUTTON
MouseButton = 2
ElseIf Button = 4 Then 'MIDDLE BUTTON
MouseButton = 4
End If
End Sub
Private Sub Command1_Click()
If MouseButton = 1 Then
'CODE FOR LEFT CLICK
ElseIf MouseButton = 2 Then
'RIGHT CLICK
ElseIf MouseButton = 4 Then
'MIDDLE CLICK
End If
End Sub
Use the GetAsyncKeyState api function to do it.
Code:Private Declare Function GetAsyncKeyState _
Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbLeftButton) Then Caption = "Left Button"
If GetAsyncKeyState(vbMiddleButton) Then Caption = "Middle Button"
If GetAsyncKeyState(vbRightButton) Then Caption = "Right Button"
End Sub
srhutch:
The only problem you may face with this popular API method is that it will be initialized to the value of the left button as soon as the program starts. You may or may not want this to be the case. The way I understood it, you wanted to raise a special event depending on which button clicks a certain control.
I'm sorry wey97, but why would you use the Click event if you first use the KeyDown event? Why not put it in the KeyDown then :)
also, you can shorten this part:
Code:If Button = 1 Then 'LEFT BUTTON
MouseButton = 1
ElseIf Button = 2 Then 'RIGHT BUTTON
MouseButton = 2
ElseIf Button = 4 Then 'MIDDLE BUTTON
MouseButton = 4
End If
'to this :)
MouseButton = Button
BTW, if you press multiple button you will get mixed flag values for button so you need to check for these bit's independently using and operator:
Code:If MouseButton And 1 Then
'CODE FOR LEFT CLICK
ElseIf MouseButton And 2 Then
'RIGHT CLICK
ElseIf MouseButton And 4 Then
'MIDDLE CLICK
End If
I notice that everyone is using 4 as the Middle Button. What does that make 3?
The button is a bit field with bits corresponding to the left button (bit 0), the right button (bit 1), and the middle button (bit 2). This refers to the binary representation of 1, 2, and 4. For example, 001 represents the left button, 010 represents the right button, and 100 represents the middle button.
I hope that clears it up.Code:001 010 100
bit 0 bit 1 bit 2
1 2 4
left right middle