Solved

Catch global mouse events

Posted on 2004-08-24
2
247 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:mannycalaveras
2 Comments
 
LVL 3

Accepted Solution

by:
Scoobyed earned 400 total points
ID: 11892141
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 11893881
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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