how can i detect left and right mouse click not only at the form but everywhere from desktop to taskbar to any open application to form of my app
thnks
Printable View
how can i detect left and right mouse click not only at the form but everywhere from desktop to taskbar to any open application to form of my app
thnks
If you are using Windows NT / XP / 2000 you can install a system wide WH_MOUSE_LL hook or if the code has to work in all systems install a WH_JOURNALRECORD hook and filter out the mouse messages only.
Demo code using EventVB.dll release H which must be downloaded and registered on your machine:
WH_MOUSE_LL code:
VB Code:
Option Explicit Dim WithEvents vbLink As EventVB.APIFunctions Dim WithEvents vbHook As EventVB.ApiSystemHook Private Sub Form_Load() Set vbLink = New EventVB.APIFunctions Set vbHook = vbLink.System.Hooks vbHook.StartHook WH_MOUSE_LL, HOOK_GLOBAL End Sub Private Sub vbHook_MouseButtonDown(ByVal Button As Integer, ByVal Location As EventVB.APIPoint, ByVal TargetWindow As EventVB.ApiWindow, Cancel As Boolean) Debug.Print "Mouse button down at " & Location.x & "," & Location.y '\\ To discard this message, set Cancel = True End Sub Private Sub vbHook_MouseButtonUp(ByVal Button As Integer, ByVal Location As EventVB.APIPoint, ByVal TargetWindow As EventVB.ApiWindow, Cancel As Boolean) Debug.Print "Mouse button up at " & Location.x & "," & Location.y '\\ To discard this message, set Cancel = True End Sub Private Sub vbHook_MouseMove(ByVal Location As EventVB.APIPoint, ByVal TargetWindow As EventVB.ApiWindow, Cancel As Boolean) Debug.Print "Mouse move to " & Location.x & "," & Location.y '\\ To discard this message, set Cancel = True End Sub Private Sub vbHook_MouseWheelMove(ByVal Rotation As Integer, ByVal Location As EventVB.APIPoint, ByVal TargetWindow As EventVB.ApiWindow, Cancel As Boolean) Debug.Print "Mouse wheel moved by " & Rotation & " at " & Location.x & "," & Location.y '\\ To discard this message, set Cancel = True End Sub
WH_JOURNALRECORD code:
VB Code:
Option Explicit Dim WithEvents vbLink As EventVB.APIFunctions Dim WithEvents vbHook As EventVB.ApiSystemHook Private Sub Form_Load() Set vbLink = New EventVB.APIFunctions Set vbHook = vbLink.System.Hooks vbHook.StartHook WH_JOURNALRECORD, HOOK_GLOBAL End Sub 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) If msg = WM_LBUTTONDOWN Or msg = WM_MBUTTONDOWN Or msg = WM_RBUTTONDOWN Then Debug.Print "Mouse button down at " & x & "," & y ElseIf msg = WM_LBUTTONUP Or msg = WM_MBUTTONUP Or msg = WM_RBUTTONUP Then Debug.Print "Mouse button up at " & x & "," & y End If End Sub
Hope this helps,
Duncan