Using mouse programatically

Daniel McElhinney 120 Reputation points
2023-02-25T16:40:57.76+00:00

Is it possible to use the mouse programmatically.

I have an app that uses Process.Start to open a webpage.

I would like to programmatically Left Click the mouse and choose "Select All" form the popup menu.

then programmatically Left Click the mouse and choose "Copy"

Can anyone help

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,823 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,347 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,564 questions
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2023-02-26T13:34:16.34+00:00

    I want to programmatically copy the contents of the webpage. At present i start a Process.Start("firefox.exe", "https://www.thelott.com/powerball/results")

    You can get the JSON response and parse it

    Test with Newtonsoft.Json :

    I get

    Value: ProductId=Powerball

    Value: DrawNumber=1397

    Value: DrawDate=23/02/2023 00:00:00

    etc...

    Test code :

               Try
                    Dim webRequest As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create("https://data.api.thelott.com/sales/vmax/web/data/lotto/latestresults"), Net.HttpWebRequest)
                    If webRequest IsNot Nothing Then
                        webRequest.Method = "POST"
                        webRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br")
                        webRequest.Headers.Add("Accept-Language", "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3")
                        Dim encoder As ASCIIEncoding = New ASCIIEncoding()
                        Dim data = encoder.GetBytes("{""CompanyId"":""NTLotteries"",""MaxDrawCountPerProduct"":1,""OptionalProductFilter"":[""Powerball""]}")
                        webRequest.ContentType = "application/json"
                        webRequest.ContentLength = data.Length
                        webRequest.Expect = "application/json"
                        webRequest.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
                        webRequest.GetRequestStream().Write(data, 0, data.Length)
                        Using s As Stream = webRequest.GetResponse().GetResponseStream()
                            Using sr As StreamReader = New StreamReader(s)
                                Dim httpWebResponse = sr.ReadToEnd()
                                Console.WriteLine(String.Format("Response: {0} {1}", Environment.NewLine, httpWebResponse))
                                Dim respJson = JsonConvert.DeserializeObject(httpWebResponse)
                                Dim firstResult = DirectCast(DirectCast(DirectCast(respJson, Newtonsoft.Json.Linq.JContainer).First,
                                Newtonsoft.Json.Linq.JContainer).First, Newtonsoft.Json.Linq.JContainer).First
                                For Each result In firstResult
                                    Dim sName = DirectCast(result, Newtonsoft.Json.Linq.JProperty).Name
                                    Dim sValue = DirectCast(result, Newtonsoft.Json.Linq.JProperty).Value
                                    Console.WriteLine(String.Format("Value: {0}={1}", sName, sValue))
                                Next
                            End Using
                        End Using
                    End If
                Catch ex As Exception
                    Console.WriteLine("Error : {0} - {1}", ex.Message, String.Format("0x{0:X}", ex.HResult))
                End Try
    

4 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Vignesh E 0 Reputation points
    2023-02-25T19:08:56.5166667+00:00

    Its actually very simple to do with keyboard mouse simulation libraries in python. There is a python library called pyautogui which can be used to simulate all kinds of keyboard and mouse inputs.
    Refer https://pyautogui.readthedocs.io/en/latest/mouse.html#mouse-clicks for how to implement it.
    You can interface it very easily it whatever you are working with.
    Other popular programming languages also provide similar libraries, thought it may not be as simple as doing it with python.
    Hope this helped.


  3. LesHay 7,126 Reputation points
    2023-02-25T19:55:30.6666667+00:00

    Hi

    This code can locate the Mouse Pointer to a particular place on screen and perform a Left Click (by a mouse button down followed by a mouse button up). In this case, I directed the pointer to the location of the Break All tiny icon in the VB edit window and performed a Click, this terminated the application - just as it would with a manual mouse left click.

    Try this code and see if you can adapt it for your use - how you get screen positions is nigh on impossible without getting the particular item (Select All) screen location and that would vary according to page zoom and other considerations.

    Anyway, here it is: (I see Castorix is present - he is the one to do this kind of thing :) )

    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2
      Const MOUSEEVENTF_LEFTUP As UInteger = &H4
    
      Declare Sub mouse_event Lib "user32" Alias "mouse_event" (dwFlags As UInteger, dx As UInteger, dy As UInteger, dwData As UInteger, dwExtraInfo As Integer)
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim mp As Point = New Point(548, 204)
        Cursor.Position = mp
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
      End Sub
    End Class
    
    '<Flags()> Public Enum MouseEventFlags As UInteger
    '  MOUSEEVENTF_ABSOLUTE = &H8000
    '  MOUSEEVENTF_LEFTDOWN = &H2
    '  MOUSEEVENTF_LEFTUP = &H4
    '  MOUSEEVENTF_MIDDLEDOWN = &H20
    '  MOUSEEVENTF_MIDDLEUP = &H40
    '  MOUSEEVENTF_MOVE = &H1
    '  MOUSEEVENTF_RIGHTDOWN = &H8
    '  MOUSEEVENTF_RIGHTUP = &H10
    '  MOUSEEVENTF_XDOWN = &H80
    '  MOUSEEVENTF_XUP = &H100
    '  MOUSEEVENTF_WHEEL = &H800
    '  MOUSEEVENTF_HWHEEL = &H1000
    'End Enum
    

  4. Daniel McElhinney 120 Reputation points
    2023-02-27T13:04:31.5666667+00:00

    That got rid of a lot of errors but there is still one remaining:

    1>C:\Users\user\source\repos\PowerBall\PowerBall\frmDB.vb(59,53): error BC30451: 'DecompressionMethods' is not declared. It may be inaccessible due to its protection level.

    1>C:\Users\user\source\repos\PowerBall\PowerBall\frmDB.vb(59,82): error BC30451: 'DecompressionMethods' is not declared. It may be inaccessible due to its protection level.

    Could that have anything to do with the Version of NewtonSoft.Json?

    0 comments No comments