Solved

VB.NET - Mouse Click * Help *

Posted on 2004-03-22
8
686 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
Comment Utility
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
Comment Utility
thank you.
i will let you know if i have any problems useing your example.
0
 
LVL 1

Author Comment

by:moe10
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:moe10
Comment Utility
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
Comment Utility
forgot to include this:

Dim LocalMousePosition As Point
0
 
LVL 1

Author Comment

by:moe10
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now