|
-
Oct 5th, 2009, 08:12 AM
#1
Thread Starter
Member
[RESOLVED] mouse hook problem
i'm using a mouse hook in order to trap mouse clicks as following :
Code:
Option Explicit
Public Const WH_MOUSE = 7
Public Const WM_LBUTTONDOWN = 513
Public Const WM_RBUTTONDOWN = 516
Public Const HC_ACTION = 0
Public IsHooked As Boolean
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
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Long _
) As Long
Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any _
) As Long
Public hMhook As Long
Public rmouseButtonWasPressed As Boolean
Public Sub SetMouseHook()
If IsHooked = False Then
hMhook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHookProc, App.hInstance, 0)
IsHooked = True
End If
End Sub
Public Sub RemoveMouseHook()
UnhookWindowsHookEx hMhook
IsHooked = False
End Sub
Public Function MouseHookProc(ByVal nCode As Long, ByVal wParam As Long, lParam As Long) As Integer
If (nCode >= 0 And nCode = HC_ACTION) Then
If wParam = WM_RBUTTONDOWN And rmouseButtonWasPressed = False Then
rmouseButtonWasPressed = True
Form1.Text1.Text = "rmouse"
Else
rmouseButtonWasPressed = False
Form1.Text1.Text = ""
End If
End If
MouseHookProc = CallNextHookEx(hMhook, nCode, wParam, lParam)
End Function
it works fine, until i alt-tab to another window and switch back to this, the procedure doesn't get the event any more. anybody knows why ?
Last edited by mirrormirror; Oct 6th, 2009 at 05:10 AM.
-
Oct 5th, 2009, 08:24 AM
#2
Re: mouse hook problem
Are you releasing the hook when your app loses focus? P.S. You should be getting events anytime the mouse moves over your app even if your app does not have focus.
Here is something else.
1. The MouseHookProc function should be returning Long, not Integer
2. Be careful passing parameters ByRef vs ByVal, this can cause you problems if you are not careful. Your MouseHookProc is receiving lParam ByRef and your CallNextHookEx is passing lParam ByRef. I don't see an issue there, just a cautious warning.
-
Oct 5th, 2009, 08:35 AM
#3
Thread Starter
Member
Re: mouse hook problem
no i don't release the hook when the app loses focus.
i fixed those two thing but no change. same behaviour. once focus is lost and regained, events not taking place any more
windows xp, vb6, just a simple app like that one ( that is the module actually )
edit : why shouldn't be both byref? lost me there
Last edited by mirrormirror; Oct 5th, 2009 at 08:45 AM.
-
Oct 5th, 2009, 09:53 AM
#4
Re: mouse hook problem
 Originally Posted by mirrormirror
... i fixed those two thing but no change. same behaviour.
I did point out one thing and offered a caution about another. Did you change your MouseHookProc to receive lParam ByVal vs ByRef? If so, did you change your CallNextHookEx API declaration to pass lParam ByVal also? You should if that is the case.
Example using ByVal:
Code:
Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByVal lParam As Long _
) As Long
Looking at your code a bit closer, your hook should be assigned as follows:
Code:
hMhook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHookProc, 0&, App.ThreadID)
Per MSDN, the 3rd & 4th params are described:
 Originally Posted by MSDN
hMod: Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.
dwThreadId: Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread.
And finally regarding ByRef vs ByVal. Since the lParam passed by windows is a pointer to a MOUSEHOOKTSTRUCT UDT, in order to reference lParam in your function (if needed), you would have to change your MouseHookProc (assuming your function receives it ByRef):
Code:
' from
Public Function MouseHookProc(ByVal nCode As Long, ByVal wParam As Long, lParam As Long) As Long
' to
Public Function MouseHookProc(ByVal nCode As Long, ByVal wParam As Long, lParam As MOUSEHOOKTSTRUCT) As Long
Last but not least, you do realize you will not be receiving hook messages from other apps; just your app?
Last edited by LaVolpe; Oct 5th, 2009 at 10:06 AM.
-
Oct 5th, 2009, 10:38 AM
#5
Thread Starter
Member
Re: mouse hook problem
yes i just cared about my app. using 0&, App.ThreadID solved the problem
( i had it according to the previous use because it was cp-ed from the keybhookproc which needed it to be so ).
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|