Solved

VB.NET - Mouse Click * Help *

Posted on 2004-03-22
8
690 Views
Last Modified: 2012-08-14
how can i detect when a user clicks the mouse?
how can i call a mouse click? i.e. windows will think the user has clicked the mouse

i have found many examples of this in other versions of vb, but i can't find one that will work for vb.net.

thanks.
0
Comment
Question by:moe10
  • 5
  • 2
8 Comments
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10650969
To know when a user has clicked somewhere, you have to listen to the Click event which is present for any controls.
For example for a form :

private sub myForm_Click(sender as Object, e as EventArgs) handles myForm.Click
  'Implementation omitted
end sub

Note that there are many more events (eg MouseDown) which will give a MouseEventArgs, ie an object with many more information (where the user has clicked, the number of times, which button, ...)

To simulate the click on something, you can :
- Call that method :
  myForm_Click(me, EventArgs.Empty)
- Call the PerformClick method if you want to simulate the click on a button
  myButton.PerformClick()
0
 
LVL 1

Author Comment

by:moe10
ID: 10652740
thank you.
i will let you know if i have any problems useing your example.
0
 
LVL 1

Author Comment

by:moe10
ID: 10652839
is it possible to record a users mouse click if the click is not within the form?

i.e. the user minimizes the form and starts to use another program, and the mouse clicks the user makes can be recognized by the vb program?

thanks.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Accepted Solution

by:
Jarodtweiss earned 57 total points
ID: 10655852
Well... I don't think so !
As your application is not active, it won't receive any event unless the Activate event when the user maximize it again.
0
 
LVL 1

Author Comment

by:moe10
ID: 10660511
If my program can gather information about the mouse position when the program is minimized why couldn't the mouse click be recorded as well?

this is a part of the code i'm useing to capture the mouse movements:

LocalMousePosition = Control.MousePosition
            Pointa(a) = LocalMousePosition.X
            Pointb(b) = LocalMousePosition.Y

i was just wondering since i can capture mouse movements when the program is minimized why wouldn't i be able to capture the mouse clicks the user makes?

thanks.
0
 
LVL 1

Author Comment

by:moe10
ID: 10660535
forgot to include this:

Dim LocalMousePosition As Point
0
 
LVL 1

Author Comment

by:moe10
ID: 10660723
ok, i solved it:

Control.MouseButtons

this will monitor mouse clicks even when the program is minimized :)

thanks for your efforts.
0
 

Expert Comment

by:mkaus
ID: 10983749
I'm also trying to achieve this.
The code below partially works, however, the clicking of the mouse seems
ineffective, ie. it doesn't register the clicking of the mouse properly and
would not initiate the button_click event. Any advice would be much
appreciated.

   Declare Function SetCapture Lib "user32" Alias "SetCapture" (ByVal hwnd
As Long) As Long
    Public Declare Function SendInput Lib "user32" (ByVal nInputs As Long,
ByVal command As MOUSECOMMAND, ByVal cbSize As Long) As Long
    Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Long,
ByVal pInputs As GENERALINPUT, ByVal cbSize As Long) As Long

    'in .net: long --> integer (to change back, put integer --> long)
    Declare Function SetCursorPos Lib "user32" Alias "SetCursorPos" (ByVal x
As Integer, ByVal y As Integer) As Integer

    Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState"
(ByVal vKey As Long) As Integer

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
        x1 = Cursor.Position.X
        y1 = Cursor.Position.Y
   If GetAsyncKeyState(1) = 0 Then
            'Label3.Text = "mouse up"
            mouseState = "up"
        Else

            'Relinquish control back to windows
            Application.DoEvents()
            Label3.Text = "mouse down"
            mouseState = "down"

            MoveAndClick(cx1 + Button3.Width, cy1)
       While mouseState = "down"

                If GetAsyncKeyState(1) = 0 Then
                    Label3.Text = "mouse up"
                    mouseState = "up"
                End If

            End While

            Cursor.Position = New Point(cx1, cy1)
Sub MoveAndClick(ByVal X As Integer, ByVal Y As Integer)

        Dim mCOMMAND As MOUSECOMMAND
        '
        ' Move
        'Relinquish control back to windows
        Application.DoEvents()

        SetCursorPos(X, Y)
        'Cursor.Position = New Point(X, Y)

        'Relinquish control back to windows
        Application.DoEvents()

        'Move (vb method)
        'If Not Me.Cursor.Equals(Cursors.Default) Then
        '    Me.Cursor = New Cursor(Cursor.Current.Handle)
        'End If
        'Cursor.Position = New Point(X, Y)

        '
        ' Click
        '
        mCOMMAND.iFlags = MOUSEEVENTF_LEFTDOWN

        'added 1
        ListBox1.Items.Add(Marshal.GetLastWin32Error)

        SendInput(1&, mCOMMAND, Len(mCOMMAND))

        'added 2
        ListBox1.Items.Add(Marshal.GetLastWin32Error)

        mCOMMAND.iFlags = MOUSEEVENTF_LEFTUP
        SendInput(1&, mCOMMAND, Len(mCOMMAND))

        'in 6.0 (also worked in .net)
        'ListBox1.Items.Add(Err.LastDllError())

        'in .NET
        '\\ Print last api error number
        'Debug.Write(Marshal.GetLastWin32Error)
        'added 3
        ListBox1.Items.Add(Marshal.GetLastWin32Error)

        'Relinquish control back to windows
        Application.DoEvents()
End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

813 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

14 Experts available now in Live!

Get 1:1 Help Now