Detect mouse click anywhere..

lrvar

Newcomer
Joined
Jan 17, 2008
Messages
3
Hi, I want to detect a mouse event anywhere on the screen,By now Im only detecting the click inside the form:

Private Sub Main_MouseDown(ByVal sender As Object, ByVal e As_
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right")
End If
End Sub

In advance thanks!!
 
Done, I found a way!!

Option Strict On
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Public Class Form1
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove, Label1.MouseMove, Label2.MouseMove, Label3.MouseMove
Label3.Text = "Usando los valores del parámetro: " & vbCrLf & "x= " & e.X & ", Y = " & e.Y
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim x As Integer = Cursor.Position.X
Dim y As Integer = Cursor.Position.Y
Dim p As POINT_API
GetCursorPos(p)
Label1.Text = "Usando Cursor.Position: " & vbCrLf & "x= " & x & ", Y = " & y
Label2.Text = "Usando GetCursorPos: " & vbCrLf & "x= " & p.X & ", Y = " & p.Y
If GetAsyncKeyState(1) = 0 Then
Label4.Text = "Your Left Mouse Button Is UP"
Else
Label4.Text = "Your Left Mouse Button Is Down"
End If
If GetAsyncKeyState(2) = 0 Then
Label5.Text = "Your Right Mouse Button Is UP"
Else
Label5.Text = "Your Right Mouse Button Is Down"
End If
End Sub
End Class

Module WinAPI
Public Structure POINT_API
Public X As Integer
Public Y As Integer
End Structure
Public Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINT_API) As Boolean
End Module
 
There you go. But what happens if someone is a really fast clicker and presses the button down and releases it, both between two timer ticks.
 
Although I've never used it myself, there is a 'low level mouse hook' in windows. You might want to search on LowLevelMouseProc if you're interested.

As I understand it, this is a callback that is called by the OS when the mouse is operated. You can set the hook with SetWindowsHookEx.
 
Back
Top