none
How to make the mouse click? RRS feed

  • Question

  • Hey,

    Long time noob, Needing assistance, I just need the code to make the mouse double left click, When a timer is on.

    Thank you,

    BluePlum
    Tuesday, November 25, 2008 8:36 AM

Answers

  • Hi BluePlums,

    You can use this code:

    Option Explicit On

    Imports System.Runtime.InteropServices

    Public Class Form1

    Private Declare Function SendInput Lib "user32" (ByVal cInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer

    Private Structure INPUT

    Public dwType As Integer

    Public mi As MOUSEINPUT

    End Structure

    <StructLayout(LayoutKind.Sequential)> _

    Private Structure MOUSEINPUT

    Public dx As Integer

    Public dy As Integer

    Public mouseData As UInteger

    Public dwFlags As UInteger

    Public time As UInteger

    Public dwExtraInfo As IntPtr

    End Structure

    Const INPUT_MOUSE As Integer = 0

    Const MOUSEEVENTF_LEFTDOWN As Integer = &H2

    Const MOUSEEVENTF_LEFTUP As Integer = &H4

    Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20

    Const MOUSEEVENTF_MIDDLEUP As Integer = &H40

    Const MOUSEEVENTF_MOVE As Integer = &H1

    Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000

    Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8

    Const MOUSEEVENTF_RIGHTUP As Integer = &H10

    Sub ClickMouse(ByVal MouseButton As Integer)

    Dim mi As New MOUSEINPUT

    mi.dwFlags = MouseButton

    Dim i As New INPUT

    i.dwType = INPUT_MOUSE

    i.mi = mi

    Dim cbSize As Integer = Marshal.SizeOf(i)

    Dim result As Integer = SendInput(1, i, cbSize)

    If result = 0 Then

    Throw New System.ComponentModel.Win32Exception

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Start()

    Timer1.Interval = 1000 'It will make the mouse click every 1000 miliseconds.

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Cursor.Position = New Point(10, Screen.PrimaryScreen.Bounds.Height - 10)

    Windows.Forms.Application.DoEvents()

    ClickMouse(MOUSEEVENTF_LEFTDOWN) 'left click

    ClickMouse(MOUSEEVENTF_LEFTUP) 'release left click

    ClickMouse(MOUSEEVENTF_LEFTDOWN) 'left click

    ClickMouse(MOUSEEVENTF_LEFTUP) 'release left click
    'left click + left click = double click

    End
    Sub

    End Class




    Bill

    • Proposed as answer by HrBill32 Tuesday, November 25, 2008 10:11 PM
    • Edited by HrBill32 Wednesday, November 26, 2008 12:52 PM changes
    • Marked as answer by Martin Xie - MSFT Friday, November 28, 2008 7:38 AM
    Tuesday, November 25, 2008 10:08 PM
  • BluePlums said:

    Sorry, But this Isn't what Im looking for.

    I just need the mouse to double click in it's current position



    Hi BluePlums,

    You didn't say that you want the mouse click in it's current position, so i posted a code which allow you to set the cordinates of where you want the mouse to click.
    To achieve what you want just remove this line from my code:
    Cursor.Position = New Point(10, Screen.PrimaryScreen.Bounds.Height - 10)
    (You can use this in case you want to set the cordinates of where you want the mouse to click.)

    I also edited my previous post to make you understand some things because you may want to make some changes.



    Bill
    Wednesday, November 26, 2008 12:47 PM

All replies

  • Call the Form1_MouseDoubleClick event if you want to click on your form, SendInput if you want to click outside your form.
    Tuesday, November 25, 2008 9:12 AM
  • Hi BluePlums,

    You can use this code:

    Option Explicit On

    Imports System.Runtime.InteropServices

    Public Class Form1

    Private Declare Function SendInput Lib "user32" (ByVal cInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer

    Private Structure INPUT

    Public dwType As Integer

    Public mi As MOUSEINPUT

    End Structure

    <StructLayout(LayoutKind.Sequential)> _

    Private Structure MOUSEINPUT

    Public dx As Integer

    Public dy As Integer

    Public mouseData As UInteger

    Public dwFlags As UInteger

    Public time As UInteger

    Public dwExtraInfo As IntPtr

    End Structure

    Const INPUT_MOUSE As Integer = 0

    Const MOUSEEVENTF_LEFTDOWN As Integer = &H2

    Const MOUSEEVENTF_LEFTUP As Integer = &H4

    Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20

    Const MOUSEEVENTF_MIDDLEUP As Integer = &H40

    Const MOUSEEVENTF_MOVE As Integer = &H1

    Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000

    Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8

    Const MOUSEEVENTF_RIGHTUP As Integer = &H10

    Sub ClickMouse(ByVal MouseButton As Integer)

    Dim mi As New MOUSEINPUT

    mi.dwFlags = MouseButton

    Dim i As New INPUT

    i.dwType = INPUT_MOUSE

    i.mi = mi

    Dim cbSize As Integer = Marshal.SizeOf(i)

    Dim result As Integer = SendInput(1, i, cbSize)

    If result = 0 Then

    Throw New System.ComponentModel.Win32Exception

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Start()

    Timer1.Interval = 1000 'It will make the mouse click every 1000 miliseconds.

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Cursor.Position = New Point(10, Screen.PrimaryScreen.Bounds.Height - 10)

    Windows.Forms.Application.DoEvents()

    ClickMouse(MOUSEEVENTF_LEFTDOWN) 'left click

    ClickMouse(MOUSEEVENTF_LEFTUP) 'release left click

    ClickMouse(MOUSEEVENTF_LEFTDOWN) 'left click

    ClickMouse(MOUSEEVENTF_LEFTUP) 'release left click
    'left click + left click = double click

    End
    Sub

    End Class




    Bill

    • Proposed as answer by HrBill32 Tuesday, November 25, 2008 10:11 PM
    • Edited by HrBill32 Wednesday, November 26, 2008 12:52 PM changes
    • Marked as answer by Martin Xie - MSFT Friday, November 28, 2008 7:38 AM
    Tuesday, November 25, 2008 10:08 PM
  • Sorry, But this Isn't what Im looking for.

    I just need the mouse to double click in it's current position
    Wednesday, November 26, 2008 1:55 AM
  • It would be a monumental task to modify the posted code to do that.  Try spending a couple of days searching for the line of code to remove.
    Wednesday, November 26, 2008 5:47 AM
  • BluePlums said:

    Sorry, But this Isn't what Im looking for.

    I just need the mouse to double click in it's current position



    Hi BluePlums,

    You didn't say that you want the mouse click in it's current position, so i posted a code which allow you to set the cordinates of where you want the mouse to click.
    To achieve what you want just remove this line from my code:
    Cursor.Position = New Point(10, Screen.PrimaryScreen.Bounds.Height - 10)
    (You can use this in case you want to set the cordinates of where you want the mouse to click.)

    I also edited my previous post to make you understand some things because you may want to make some changes.



    Bill
    Wednesday, November 26, 2008 12:47 PM