Solved

Catch global mouse events

Posted on 2004-08-24
2
243 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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