Solved

Catch global mouse events

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now