?
Solved

Catch global mouse events

Posted on 2004-08-24
2
Medium Priority
?
250 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
[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 Comments
 
LVL 3

Accepted Solution

by:
Scoobyed earned 1600 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 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