Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Make The Program Click The Left Mouse Button 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I need my program to click the left mouse button every 30 seconds.

Any ideas?

It's a college project due in soon, so please any help would be good.
 
Use the mouse_event api:

declare:
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetMessageExtraInfo Lib "user32" Alias "GetMessageExtraInfo" () As Long
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up


to call:
dim extra as long

extra = GetMessageExtraInfo
mouse_event MOUSEEVENTF_LEFTUP ,0,0,0,extra
 
It doesnt seem to work, . this text is highlighted in red ""Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

to call:""


Any ideas? where do i put this code? I am not sure what I am doing wrong
 
Matt, I'm not sure if chrisrf is still checking this thread
so I will try to answer.

The trouble is that you included some text in your code
that chris intended as instructions, not as part of the code.

The API function and any constants have to be put into
a separate module.
Do this by clicking on the Project menu and selecting
'Add module'
copy and paste this code into the new module:

Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetMessageExtraInfo Lib "user32" Alias "GetMessageExtraInfo" () As Long
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

You should put the other lines of code
in a timer event or something to make it happen every
30 seconds.
Here is the rest of the code that you should include:

dim extra as long

extra = GetMessageExtraInfo
mouse_event MOUSEEVENTF_LEFTUP ,0,0,0,extra



right, try that. Do some reading on this API function.
This web site has some info on the mouse event api

It's not wise to use something if you're not sure what
it is doing.

hope this makes things clearer
good luck
:)
 
nonguru , I dont get any error messages but it doesnt do anything!!.

Here's what I am doing:

I created a module and put the following code into it:

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetMessageExtraInfo Lib "user32" () As Long
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

then I create a timer with an interval and put this code into it:


Dim extra As Long

extra = GetMessageExtraInfo
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, extra


Any ideas?

Thanks
 
Put this code in a module:

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetMessageExtraInfo Lib "user32" () As Long
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up




Put this code in a Form:

Private Sub Form_Load()
Dim extra As Long

extra = GetMessageExtraInfo
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, extra
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox "It Works!", vbInformation, "School"
End Sub


When you run it,it shows a msgbox on form load.It works!
 
Hey Matt, did the code from War23 help?

Obviously you won't see any effect from the mouse click unless you hold the mouse pointer over a menu or a command
button.

But if you use War23's code, it will pop a message box.

Do you mind telling me why you wanted the mouse to click
every 30 seconds? Just curious.

Let me know if you need any more help

;-)
 
Sorry guys but I cant get it to work, I dont know what I am doing wrong here is a step by step to what I am doing!!.
Firstly I Add a Module and put the following code in there:

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetMessageExtraInfo Lib "user32" () As Long
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

then I double click on my form and add this code:

Private Sub Form_Load()
Dim extra As Long

extra = GetMessageExtraInfo
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, extra
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox "It Works!", vbInformation, "School"
End Sub

when I run the program it doesnt do anything, even when I put a command button!!.

Basically what I want my program to do is click the left mouse button every 30 seconds so If i leave my program running it would click for me.!!

I designing a program for college. I cant say what it is just in case someone from my college uses tek-tips, just in casse my idea is stolen!!.
 
Do I have to call up the Module somehow?

Thanks a lot for all your help!!
 
Hey Matt, yes you are right, the problem is that you have
to call the mouse_event procedure every 30 seconds.

As your program is at the moment, mouse_event is only called once just as the program is starting, because you only call it in the form_load procedure.

Probably the easiest way to call mouse_event every 30 seconds is to use a timer.

Just put a timer control on your form - doesn't matter
where you put it, as it's invisible when the program
is run.

When using a timer you have to set the timer interval.
and enable the timer when you want it to start.
If you want to start clicking as soon as your program
starts, enable the timer in the form_load procedure.

Private Sub Form_Load()
Timer1.Interval = 30000
Timer1.Enabled = True
End Sub

Setting Timer1.Interval = 30000 means that every 30000 milliseconds (every 30 seconds) a procedure called
Timer1.Timer will be called.
So this procedure is where you want to call the mouse_event.

Private Sub Timer1_Timer()
mouse_event MOUSEEVENTF_LEFTDOWN,0,0,0,0,
mouse_event MOUSEEVENTF_LEFTUP,0,0,0,0
End Sub

I wouldn't worry about the getextrainfo procedure
I don't think you need it.
And you will need to add this constant to your module

Public Const MOUSEEVENTF_LEFTDOWN = &H2


So all that should be in your module is this:

Option Explicit
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetMessageExtraInfo Lib "user32" () As Long


Ok, hope this helps
It's important not to use code you don't understand,
so feel free to ask for further explanation :-Q
 
Sorry, all that you need in the module is this...

Option Explicit
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
[/color
I don't feel that you need the GetMessageExtraInfo function.
 
Nonguru, I am not sure why it;s not working! I am not a fool and I think if you start from the begining and explain to me a step by step it I should get it working. Your other explaination was good but I was confused as to what to put in my form so I would appreciate if you could start from scratch and tell me what to do.

Thanks for all your help
also here is a line of code where the problem is (i guess)
as its in red!!

mouse_event MOUSEEVENTF_LEFTDOWN,0,0,0,0,

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top