Link to home
Start Free TrialLog in
Avatar of mannycalaveras
mannycalaveras

asked on

Catch global mouse events

I want to be able to catch global mouse events from an application that would basically be window-less (at least in its normal functionning). I would like the application to catch all mouse events, regardless of which program has the focus and do some handling while allowing the message to go through the application that has the focus. The app I have to build will need to respond to specific combinations of mouse input in order to pop the user dialog.

Is that possible under .NET? I have checked and cannot find anything useful for that goal. Something similar was available in Win32, if I remember correctly, and I would doubt it would not be possible.

Just to reassure, it's not spyware or something like that :) Just a neat idea I had and my boss likes enough to add to our main application.
ASKER CERTIFIED SOLUTION
Avatar of Scoobyed
Scoobyed

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iboutchkine
iboutchkine

Try this

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure POINTAPI
        Dim x As Int32
        Dim y As Int32
    End Structure

    Private Declare Function GetCursor Lib "user32" () As Int32
    Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (ByRef lpPoint As POINTAPI) As Int32
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Int32, ByVal yPoint As Int32) As Int32
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Int32, ByRef lpdwProcessId As Int32) As Int32
    Private Declare Function AttachThreadInput Lib "user32" Alias "AttachThreadInput" (ByVal idAttach As Int32, ByVal idAttachTo As Int32, ByVal fAttach As Int32) As Int32
    Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Int32) As Int16

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim pt As POINTAPI
        Call GetCursorPos(pt)
        Me.Label1.Text = "X: " + pt.x.ToString + " Y: " + pt.y.ToString

        Dim currentThread = GetWindowThreadProcessId(WindowFromPoint(pt.x, pt.y), vbNull)
        Dim meThread = GetWindowThreadProcessId(Me.Handle.ToInt32, vbNull)
        Dim hCursor As Int32
        If meThread <> currentThread Then
            AttachThreadInput(meThread, currentThread, True)
            hCursor = GetCursor()
            AttachThreadInput(meThread, currentThread, False)
            Me.Label2.Text = "Mouse outside form"
        Else
            hCursor = GetCursor()
            Me.Label2.Text = "Mouse inside form"
        End If

        Static currentCursor As Int32 = 0
        If currentCursor <> hCursor Then
            ' only change image when cursorimage changed
            Dim ico As Icon = Icon.FromHandle(New IntPtr(hCursor))
            Me.PictureBox1.Image = ico.ToBitmap
            currentCursor = hCursor
        End If

        Static sleeper As Long = 0
        If GetAsyncKeyState(1) = -32767 Then
            sleeper = 0
            Me.Label3.Text = "Left Mouse Down"
        ElseIf GetAsyncKeyState(2) = -32767 Then
            sleeper = 0
            Me.Label3.Text = "Right Mouse Down"
        ElseIf GetAsyncKeyState(4) = -32767 Then
            sleeper = 0
            Me.Label3.Text = "Middle Mouse Down"
        Else
            sleeper += 1
            If sleeper >= 50 Then
                Me.Label3.Text = ""
            End If
        End If

    End Sub

End Class