Results 1 to 12 of 12

Thread: detecting the button pressed on a mouse

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    3

    Post

    how can i detect which button was clicked on the mose to put in a click event?

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111

    Post MouseDown

    use the
    CommandButton MouseDown event
    then Button refers to the button pushed

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Posts
    3

    Post

    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

  4. #4
    New Member
    Join Date
    Feb 2000
    Location
    Moncton, NB, Canada
    Posts
    4

    Post Mouse Down event

    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

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Post

    If you feel confused when due with all the number, why not using this:

    vbRightButton
    vbMiddleButton
    vbLeftButton

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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

  7. #7
    Guest
    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

  8. #8
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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.

  9. #9
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Guest
    I notice that everyone is using 4 as the Middle Button. What does that make 3?

  12. #12
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Wink

    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.

    Code:
     001        010        100
     bit 0      bit 1      bit 2
     1          2          4
     left       right      middle
    I hope that clears it up.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width