Mouse Hook

Category: Hooking

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
Declare Function WinMsg Lib "WINMSG.DLLAlias "WindowMessageA" (ByVal MsgNum As LongAs String
 
Global hDlg,hConsole,ghHook As Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Toolbar Test",400,400,400,300, %WS_OverlappedWindow To hDlg
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case Cb.Msg
      Case %WM_InitDialog
         ghHook = SetWindowsHookEx(%WH_Mouse, CodePtr(MouseProc), GetModuleHandle(""), GetCurrentThreadId)
      Case %WM_Destroy
         UnhookWindowsHookEx ghHook
   End Select
End Function
 
Function MouseProc(ByVal nCode As LongByVal wParam As LongByVal lParam As LongAs Long
   If nCode < 0 Then Exit Function   'prevent from doing stuff below
   'do stuff ...
   CPrint WinMsg(wParam)  '<---- in mouse hook, wParam contains the Msg ID
   Function = CallNextHookEx(ByVal ghHook, ByVal nCode, ByVal wParam, ByVal lParam)
End Function
 
Sub CPrint (SOut As String)
   Static cWritten,iCount As Long
   Incr iCount
   SOut = Format$(iCount,"000000") + SOut
   If hConsole = 0 Then AllocConsole: hConsole = GetStdHandle(-11&)
   WriteConsole hConsole, ByCopy sOut + $CrLfLen(sOut) + 2, cWritten, ByVal 0&
End Sub
 
'gbs_00971
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm