[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How can I intercept a mouse click event for Win XP or Win Vista in VB.NET

Posted on 2008-06-18
8
Medium Priority
?
699 Views
Last Modified: 2013-11-26
I am writing a windows service (non-forms based) that needs to intercept ANY mouse click made (anywhere) in Windows XP or Windows Vista (in C# or VB.NET).  I don't need the coordinates or anything like that, just need to know if either mouse button was clicked so that the service will intercept the click (without affecting any other program running) and play a "click" noise.
I'm not sure where to begin on this one (the Internet has tons of stuff on events WITHIN a forms program, but nothing outside).
Any ideas?
0
Comment
Question by:bd9000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:BitRunner303
ID: 21816522
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 21816756
Pretty sure you won't be able to get the hooks to work from a SERVICE...
0
 
LVL 3

Expert Comment

by:BitRunner303
ID: 21816952
Oops my bad.

You could do it just as easily with a WinForms app with Opacity set to 0 and not shown in the taskbar.  I know the requestor said he didn't want it in a WinForms app but that may be the most plausible method since you'd have to be running on an interactive user.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:bd9000
ID: 21817789
Thanks for the links.  I think this is way out of my league understanding how to actually implement the class.
How would I go about it in just a windows form app (when the form is out of focus?)


0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 21817929
For WinForms you can do this:
(note that in VB.Net 2005, or above, the hook does NOT work in the IDE...run the executable itself from the bin folder)
Imports System.Runtime.InteropServices
Public Class Form1
 
    Private WithEvents llmh As New LowLevelMouseHook
 
    Private Sub llmh_LeftClick() Handles llmh.LeftClick
        Debug.Print("Left Click @ " & DateTime.Now.ToString)
    End Sub
 
    Private Class LowLevelMouseHook
 
        Private Const HC_ACTION As Integer = 0
        Private Const WH_MOUSE_LL As Integer = 14
        Private Const WM_LBUTTONDOWN As Integer = &H201
 
        Public Structure POINTAPI
            Public x As Int32
            Public y As Int32
        End Structure
 
        Public Structure MSLLHOOKSTRUCT
            Public pt As POINTAPI
            Public mouseData As Int32
            Public flags As Int32
            Public time As Int32
            Public dwExtraInfo As Int32
        End Structure
 
        Private Declare Function SetWindowsHookEx Lib "user32" _
            Alias "SetWindowsHookExA" ( _
            ByVal idHook As Integer, _
            ByVal lpfn As LowLevelMouseProcDelegate, _
            ByVal hmod As Integer, _
            ByVal dwThreadId As Integer) As Integer
 
        Private Declare Function CallNextHookEx Lib "user32" ( _
            ByVal hHook As Integer, _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As MSLLHOOKSTRUCT) As Integer
 
        Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
            ByVal hHook As Integer) As Integer
 
        Private Delegate Function LowLevelMouseProcDelegate( _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As MSLLHOOKSTRUCT) As Integer
 
        Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" ( _
            ByRef lpPoint As POINTAPI) As Integer
 
        Private hhkLowLevelMouse As Integer
        Private mouseDelegate As LowLevelMouseProcDelegate
 
        Public Event LeftClick()
 
        Public Sub New()
            Hook()
        End Sub
 
        Public Sub Hook()
            ' remove existing hook if there is one
            If hhkLowLevelMouse <> 0 Then
                UnHook()
            End If
 
            ' make a new hook
            mouseDelegate = New LowLevelMouseProcDelegate(AddressOf Me.LowLevelMouseProc)
            hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, mouseDelegate, _
                Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub
 
        Public Sub UnHook()
            If hhkLowLevelMouse <> 0 Then
                UnhookWindowsHookEx(hhkLowLevelMouse)
            End If
            hhkLowLevelMouse = 0
        End Sub
 
        Private Function LowLevelMouseProc( _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As MSLLHOOKSTRUCT) As Integer
 
            If (nCode = HC_ACTION) Then
                Select Case wParam
                    Case WM_LBUTTONDOWN
                        RaiseEvent LeftClick()
 
                End Select
            End If
 
            Return CallNextHookEx(hhkLowLevelMouse, nCode, wParam, lParam)
        End Function
 
        Protected Overrides Sub Finalize()
            UnHook()
            MyBase.Finalize()
        End Sub
 
    End Class
 
End Class

Open in new window

0
 

Author Comment

by:bd9000
ID: 21951057
Sorry it's taking so long to reply - I haven't had time to try this code.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question