Results 1 to 3 of 3

Thread: mouse hooking - help with code

  1. #1

    Thread Starter
    Hyperactive Member Sacofjoea's Avatar
    Join Date
    May 2000
    Location
    Never Never Land
    Posts
    472

    mouse hooking - help with code

    VB Code:
    1. Option Explicit
    2. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    3. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    4. Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    5. Private MouseHookHandle As Long
    6.  
    7. Public Sub SetMouseHook()
    8.    Const WH_MOUSE = 7&
    9.    MouseHookHandle = SetWindowsHookEx(WH_MOUSE, AddressOf MyMouseHandler, App.hInstance, App.ThreadID)
    10.    End Sub
    11.  
    12. Public Sub ReleaseMouseHook()
    13.    If MouseHookHandle = 0 Then Exit Sub
    14.    UnhookWindowsHookEx MouseHookHandle
    15.    MouseHookHandle = 0
    16.    End Sub
    17.    
    18. Public Function MyMouseHandler(ByVal HookID As Long, ByVal MsgCode As Long, ByVal pMouseHookInfo As Long) As Long
    19. On Error Resume Next
    20.    Const WM_LBUTTONDOWN = &H201&  ' MouseDown - Left button
    21.    Const WM_MBUTTONDOWN = &H207&  '             Middle
    22.    Const WM_RBUTTONDOWN = &H204&  '             Right
    23.    
    24.    Const HC_ACTION = 0&
    25.    
    26.    If HookID <> HC_ACTION Then GoTo PassItOn
    27.    
    28.    Select Case MsgCode
    29.       Case WM_RBUTTONDOWN
    30.       MsgBox ""
    31.       End Select
    32.  
    33.  
    34. PassItOn:
    35.    '
    36.    ' allow mouse event to be passed on and processed normally
    37.    '
    38.    MyMouseHandler = CallNextHookEx(MouseHookHandle, HookID, MsgCode, pMouseHookInfo)
    39.    Exit Function
    40.    End Function

    im using that to catch when the right mouse button is pressed..problem is, if I add just about anything else in there (other than some sort of msgbox) it just closes down VB and screws up.

    Is there any way to make that just not return any type or action.. or better yet, pop up a menu? (regular poping up of a menu does not work there..it just crashes)
    or have it perform some function or something..

  2. #2
    Member
    Join Date
    Sep 2002
    Location
    Sydney, Australia
    Posts
    37
    Popupmenu seems to work for me. Did you specify the parent of the menu?

    VB Code:
    1. Select Case MsgCode
    2.       Case WM_RBUTTONDOWN
    3.         frm.PopupMenu frm.test
    4.       End Select

    But
    VB Code:
    1. Select Case MsgCode
    2.       Case WM_RBUTTONDOWN
    3.         frm.PopupMenu test
    4.       End Select
    crashed VB as you had described.
    Sunny

    * No trees were harmed in the making or sending of this message. However a great number of electrons were terribly inconvenienced.

  3. #3

    Thread Starter
    Hyperactive Member Sacofjoea's Avatar
    Join Date
    May 2000
    Location
    Never Never Land
    Posts
    472
    oh ok, I was using it a different way

    Thanks

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