Solved

VB.NET - Mouse Click * Help *

Posted on 2004-03-22
8
693 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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