[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB.NET - Mouse Click * Help *

Posted on 2004-03-22
8
Medium Priority
?
742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 4

Accepted Solution

by:
Jarodtweiss earned 228 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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