Sign in to follow this  
Followers 0
Kebaan

Simple automated mouseclicker

13 posts in this topic

Well, as the topic describes I'm a newbie :P

To learn scripting in AutoIt I've been trying to make a simple automated mouseclicker which clicks X times on some position.

My problem is that the whole thing isn't working. I hope some of you will be able to help me out as im kinda stuck atm :/

;Logic needed to start the primary script
$Start = False

;I've made the MsgBox to test if the position of the mouse worked, and if my Logic worked.
$GetPosKey = "{ENTER}"
HotKeySet($GetPosKey, '_GetPos')
Func _GetPos()
    $Start = True
    $pos = MouseGetPos()
    MsgBox(0, "Mouse position (x;y) and $Start's value:", $pos[0] & "," & $pos[1] & "," & $Start, 2)
EndFunc

;Script is closed when ESC is pressed
$ExitKey = "{ESC}"
HotKeySet($ExitKey, '_Exit')
Func _Exit()
    Exit
EndFunc

;I think ive done something wrong here?
Do
    While 1
    Sleep(1000)  ; Just idle around 
    WEnd
Until $Start = True

;This is the primary script that should be run when $Start = True
$count = 0
Do
    MouseClick( "primary" , 560, 610 )
    Sleep(1500)
    $count += 1
Until $count = 10

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Welcome to the forums.

See if you can follow my modifications:

Global $pos
Global $Start = False

HotKeySet("{ENTER}", '_GetPos')
HotKeySet("{ESC}", '_Exit')

Do
    Sleep(100) ; Just idle around
Until $Start = True


For $i = 1 To 10
    MouseClick("primary", $pos[0], $pos[1])
    Sleep(1500)
Next

Func _GetPos()
    $Start = True
    $pos = MouseGetPos()
    MsgBox(0, "Mouse position (x;y) and $Start's value:", $pos[0] & ", " & $pos[1] & ", " & $Start, 2)
EndFunc   ;==>_GetPos

Func _Exit()
    Exit
EndFunc   ;==>_Exit
Edit: Global thingy done :-) Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

in my modest opinion you should make $start and $pos global variables

and change

Do
    While 1
    Sleep(1000) ; Just idle around
    WEnd
Until $Start = True

to

Do
    Sleep(1000) ; Just idle around
Until $Start = True

because it loops infinitely

Edited by muhmuuh

I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

Ahh yea, thank you very much :P

Theres so many ways to do things and I can see that your way is way better than mine as it's working :o

But I have some questions! Why didn't my script work, my logic tells me that it should work?

In this part of your script, you wrote global $pos = .... I've tried to delete Global and then nothing worked, why is that? and what do global do? :P

Func _GetPos()
 $Start = True
 Global $pos = MouseGetPos()
 MsgBox(0, "Mouse position (x;y) and $Start's value:", $pos[0] & ", " & $pos[1] & ", " & $Start, 2)
EndFunc  ;==>_GetPos

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

...Why didn't my script work, my logic tells me that it should work?...

The script enters your Do/Until loop and then enters the While/WEnd loop.

Your HotKey changes $Start from False to True...

...but the script never reaches the Until $Start = True

...line of code to evaluate the True/False state.

The script is stuck inside an infinite While/WEnd loop.

Do
    While 1
    Sleep(1000)  ; Just idle around 
    WEnd
Until $Start = True

...and what do global do? :P ...

Ask muhmuuh :-) Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

your script doesn't work mainly because of the

Do
    While 1
    Sleep(1000); Just idle around
    WEnd
Until $Start = True

part. You never get out of the while loop

And the thing that "Global" does that it makes a variable the same for all functions (global variable). If you dont put global you have local variables that 'work' only for the function that they are in. Local variables may have the same name as other local variables but they are completely different. In your case you have one variable $start that is in the main function and another that is in the _Getpos function and they are different.

I hope you understand my messy explanation and I suggest you read the help file for a better one.


I ran. I ran until my muscles burned and my veins pumped battery acid. Then I ran some more.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Duh. That was a pretty stupid mistake :P

Muhmuuh why is it better to make the variables Global? I've tested it and I can't see the difference :S

EDIT: nvm I just saw your answer

Thnaks for the help both of you!

Edited by Kebaan

Share this post


Link to post
Share on other sites

...And the thing that "Global" does...

Thanks - I had to go chase another rabbit.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

One last thing - just to understand it! :P

Why is this:

For $i = 1 To 10
    MouseClick("primary", $pos[0], $pos[1])
    Sleep(1500)
Next

better than:

Do
    Sleep(1000); Just idle around 
Until $Start = True
Edited by Kebaan

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

One last thing - just to understand it! :P

Why is this:

For $i = 1 To 10
    MouseClick("primary", $pos[0], $pos[1])
    Sleep(1500)
Next

better than:

Do
    Sleep(1000); Just idle around 
Until $Start = True
This:
For $i = 1 To 10
    MouseClick("primary", $pos[0], $pos[1])
    Sleep(1500)
NextoÝ÷ Ú·©§vØb±«­¢+ØÀÌØí½Õ¹ÐôÀ)¼(5½ÕÍ
±¥¬ ÅÕ½ÐíÁÉ¥µÉäÅÕ½Ðì°ÔØÀ°ØÄÀ¤(M±À ÄÔÀÀ¤(ÀÌØí½Õ¹Ð¬ôÄ)U¹Ñ¥°ÀÌØí½Õ¹ÐôÄÀ
...because a For/Next loop has an incrementing or decrementing index already built in. There is no need to:

Set $count to zero

Increment $count with each loop

Edit: BTW - it is good to ask such questions

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Thanks for the explanation :P

I must say that I'm surprised by this forum. You often get alot stupid answers on other forums when asking questions :P it's good to get good answers for once :o

Share this post


Link to post
Share on other sites

Thanks for the explanation :P

I must say that I'm surprised by this forum. You often get alot stupid answers on other forums when asking questions :P it's good to get good answers for once :o

This is a great community of forum members. If time allows - you will probably get plenty of help.

You posted the code that you had attempted in your first post of this thread (without us having to ask for you to show some effort). That is a big plus for getting a quick reply.

Hang around - enjoy AutoIt and the forums :-)


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

just finished this litle project. to end this topic i thought i would post my final result :P not that I think that you can use it for anything, but whatever :P

Global $pos
Global $Start = False
Global $Title = "Automated mouse clicker!"

HotKeySet("^g", '_GetPos')
HotKeySet("{ESC}", '_Exit')

MsgBox( 0, $Title, "Welcome to my automated mouse clicker!" )
Global $NumberOfClicks = InputBox( $Title, "Here you can choose how many times you want the clicker to click!" )
MsgBox( 0, $Title, "Press ""CTRL+g"" to choose the position where you want the script to click!" ) 

Do
    Sleep(100);==>Idle
Until $Start = True

For $i = 1 To $NumberOfClicks
    MouseClick("primary", $pos[0], $pos[1])
    Sleep(1500)
Next;==>Click chosen # of times at the chosen position

Func _GetPos()
    $Start = True
    $pos = MouseGetPos()
    MsgBox(0, $Title ,"Mouse position (x;y) = (" & $pos[0] & ", " & $pos[1] & ")" )
EndFunc  ;==> Get the mouse's position

Func _Exit()
    Exit
EndFunc  ;==> Exit the script
Edited by Kebaan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0