Solved

VB.NET - Mouse Click * Help *

Posted on 2004-03-22
8
688 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

20 Experts available now in Live!

Get 1:1 Help Now