Results 1 to 11 of 11

Thread: How to detect a mouse click???

  1. #1

    Thread Starter
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340

    How to detect a mouse click???

    Hello every1,
    i wanna know if there is any API which will let me know if any of the mouse button was pressed. I wanna know which button was pressed and this must be global, i.e., i must be able to press anywhere, my app must report whether a mouse click was detected. Thanx in advance. I also want to know if it was a left or right click.
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    To do this you would need to install a windows hook. If you are using only windows NT/2000/XP then the WH_MOUSE_LL hook is the most appropriate otherwise a WH_JOURNALRECORD hook can be used but you need to skip all the non-mouse messages it recieves.

  3. #3
    Addicted Member
    Join Date
    Sep 2002
    Location
    Middle Earth
    Posts
    156
    The WH_GETMESSAGE hook would work too for any OS seeing
    as mouse clicks are posted.

  4. #4

    Thread Starter
    Hyperactive Member toughcoder's Avatar
    Join Date
    Nov 2002
    Location
    Near, Very Near
    Posts
    340
    hey guyz,
    u'd b doing me a favor if u posted some code. I dont know much abt message hooking. a sample code will be gr8ly appreciated. thanx
    If Not VB Then Exit
    ------------------------------------------------
    visit me @ http://mzubair.50g.com/

  5. #5
    Addicted Member
    Join Date
    Sep 2002
    Location
    Middle Earth
    Posts
    156
    Use the 'search' functionality of the forums to find hooking code.
    There are MANY examples.

  6. #6
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Using the EventVB.dll you could do this thus:-

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WithEvents vbLink As EventVB.APIFunctions
    4. Dim WithEvents vbHook As EventVB.ApiSystemHook
    5.  
    6. Private Sub Form_Load()
    7.  
    8. Set vbLink = New APIFunctions
    9.  
    10. Set vbHook = vbLink.System.Hooks
    11. vbHook.StartHook WH_JOURNALRECORD, HOOK_GLOBAL
    12.  
    13.  
    14. End Sub
    15.  
    16. Private Sub vbHook_JournalMouseMessage(ByVal msg As EventVB.WindowMessages, ByVal x As Long, ByVal y As Long, ByVal time As Long, ByVal TargetWindow As EventVB.ApiWindow)
    17.  
    18. If msg = WM_LBUTTONUP Then
    19.     If Not TargetWindow Is Nothing Then
    20.         '\\ Click was over a window...is it a button?
    21.         If Not TargetWindow.Button Is Nothing Then
    22.             Debug.Print TargetWindow.WindowText & " button pressed"
    23.         End If
    24.     End If
    25. End If
    26.  
    27. End Sub
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  7. #7
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    Or if you don't have the vb event.dll..
    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    2. Private Const VK_LBUTTON = &H1
    3. Private Const VK_RBUTTON = &H2
    4. '
    5. ' following is only needed if you want to identify the window
    6. ' in which the mouse is being clicked
    7. '
    8. Private Type POINTAPI
    9.    X As Long
    10.    Y As Long
    11.    End Type
    12. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    13. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    14.  
    15. Private Sub Timer1_Timer()
    16.    Static lastvalL%, lastvalR%
    17.    Dim thisvalL%, thisvalR%
    18.    Dim cpoint As POINTAPI
    19.    
    20.    thisvalL = GetAsyncKeyState(VK_LBUTTON)
    21.    If thisvalL <> lastvalL Then
    22.       lastvalL = thisvalL
    23.       If thisvalL <> 0 Then
    24.          GetCursorPos cpoint
    25.          List1.AddItem "Left Click - " & Hex(WindowFromPoint(cpoint.X, cpoint.Y))
    26.          End If
    27.       End If
    28.    
    29.    thisvalR = GetAsyncKeyState(VK_RBUTTON)
    30.    If thisvalR <> lastvalR Then
    31.       lastvalR = thisvalR
    32.       If thisvalR <> 0 Then
    33.          GetCursorPos cpoint
    34.          List1.AddItem "Right Click - " & Hex(WindowFromPoint(cpoint.X, cpoint.Y))
    35.          End If
    36.       End If
    37.    End Sub

  8. #8
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    In general polling the mouse key state with a timer is not a great idea as it will only fire if the state changed when the timer fires - if the mouse is clicked down then up before the timer fires this event will be missed.

    If you don't want to use the dll (which is free and includes full source) I recommend you read up MSDN on installing a WH_JOURNALRECORD hook...
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  9. #9
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    Really? I couldn't find that dll for free with full source code. Where did you get it from?

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Last edited by MerrionComputin; Mar 29th, 2003 at 09:35 AM.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  11. #11
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    Thank You Very Much!!

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