The reason I write an article that deals with simulating mouse clicks in the programming language "Visual Basic 2010 Express Edition" is pretty simple: I wanted to know how to do something like this, just like you, but it was unbelievably hard to come across a tutorial on the internet about this, really, pretty simple problem. I've written this article so that someone else should not needlessly spend hours searching the internet in the future.
Leanring how to simulate mouse clicks (right mouse button, left mouse button etc.) in Visual Basic 2010 Express Edition is really useful. For a start, this will help you a great deal in creating something like an autoclicker (for legal purposes) or a fully automatic setup program. You're going to have to learn how to do this in your life as a software programmer, and this is how:
- First of all, you obviously need the software we're using here which is "Visual Basic 2010 Express Edition". The code I am about to give you is guaranteed to work on the latter, but it has not been tested on the 2005/2008 versions. Nevertheless, you can always download it here at the official Microsoft website.
- Now create a new project and call it whatever you want (like "cookies", for example)
- Double click on the form and replace all of the code with the code listed beneath:
Public Class Form1
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 Public Const MOUSEEVENTF_LEFTUP = &H4 Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 Public Const MOUSEEVENTF_MIDDLEUP = &H40 Public Const MOUSEEVENTF_RIGHTDOWN = &H8 Public Const MOUSEEVENTF_RIGHTUP = &H10
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Start() Timer1.Interval = 500 End Sub End Class
4. Now add a timer to your form and call it "Timer1".
This will run a script that will press the right mouse button every 500 milliseconds. If you would like it to press the left mouse button you will need to replace the mouse_event by it's left equivalent:
Public Const MOUSEEVENTF_LEFTDOWN = &H2 Public Const MOUSEEVENTF_LEFTUP = &H4
The middle mouse button can also be pressed:
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 Public Const MOUSEEVENTF_MIDDLEUP = &H40
Changing Timer1.Interval = 500 (milliseconds) will result in a faster or longer time between two clicks.
The code you're using has been derived from the code at this site (thanks to XI Samuel IX), it's not the same however, the code Samuel provides uses a checkbox.