How do you record where the mouse clicks

ryodoan

Well-known member
Joined
Jul 16, 2004
Messages
65
Location
Ohio
Programming Experience
3-5
How would I capture where a mouse clicks? I know how to simulate movements but I was wondering if there was a way I could make a program that does this:
  1. User Clicks button on the form
  2. program starts to listen for mouse clicks
  3. On a mouse click computer adds x,y coordinates into an array
  4. User clicks button again
  5. program stops listening for mouse clicks
It seems to me that I will need to use threading, which I have never done before and know very very little about. I am not even sure if I fully understand what threading is.
 
There is no need for multi-threading here. Are you talking about mouse clicks in your own application only? If so all you have to do is have the button toggle a Boolean variable. You then handle the MouseDown or MouseUp (depending which one you want to record) event for all the other controls within the area for which you want to record clicks and record a click if the variable is set, e.g.:
VB.NET:
    Private recordClicks As Boolean = False
    Private clickLocations As New ArrayList

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.recordClicks = Not Me.recordClicks
        Me.clickLocations.Clear()
    End Sub

    'The mouse coordinates reported are relative to the top-left corner of the control that was clicked.
    'This method will convert that to be relative to the form no matter what control was clicked.
    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        If Me.recordClicks Then
            'Cast the sender as a Control so we can use it as one.
            Dim ctl As Control = DirectCast(sender, Control)

            'Get the mouse coordinates relative to the top-left corner of the screen.
            Dim screenPoint As Point = ctl.PointToScreen(New Point(e.X, e.Y))

            'Get the mouse coordinates relative to the top-left corner of the form.
            Dim formPoint As Point = Me.PointToClient(screenPoint)

            Me.clickLocations.Add(formPoint)
        End If
    End Sub
 
Well, when I first posted this I was talking about outside of the form but upon reading another post here I found out that is pretty darn hard.

I am creating a program that will type my serials into the correct areas on the screen and I just need the starting point to click on the screen for the initial text box, after that I am set.

After seeing how hard it would be to get the clicks from outside the form area, I decided to instead maximize the form, and make the background transparent, so you can click in the nessesary areas while still collecting the clicks off the form.

Thanks for the code, that will probably work perfectly for what I need now.
 
The SendKeys class is normally used for that purpose, including TABs to move between fields/buttons and ENTERs to submit.
 
Back
Top