Jump to content

mousemove, variables and loops


Recommended Posts

Could anyone please tell me what I doing wrong with the following script?

It's merely a test of mine while I'm getting used to the program. I wish to start the mouse pointer at X = 100, Y = 100 inside a local window, then automatically move the mouse pointer along to the right by 1 pixel each time until X = 300, Y =100. The script assumes that the Calculator window is already loaded on screen.

Winactivate("Calculator")

WinWaitActive("Calculator")

AutoItSetOption ("MouseCoordMode", 0)

$X = 100

$Y = 100

if $X < 300 Then

MouseMove ($X, $Y, 5)

$X = $X + 1

Else

MsgBox(0, "Result:", "$X equals 300, $Y equals 100")

EndIf

The mouse pointer does initially go to 100,100, but it doesn't start moving to the right as I wanted.

Thanks

Edited by emog
Link to comment
Share on other sites

It should look like this:

AutoItSetOption ("MouseCoordMode", 0)
$X = 100
$Y = 100
while $X < 300
MouseMove ($X, $Y, 5)
$X = $X + 1
WEnd
MsgBox(0, "Result", "$X = " & $X & @CRLF & "$Y = " & $Y)

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

Many thanks, Cyber! How would you also nest a loop inside a loop, such as something like below (where it completes a line, then moves down a pixel and does the next line, etc., until it's completed five lines)?

AutoItSetOption ("MouseCoordMode", 0)

$X = 100

$Y = 100

while $Y < 105

while $X < 300

MouseMove ($X, $Y, 0)

$X = $X + 1

WEnd

$Y = $Y + 1

WEnd

MsgBox(0, "Result", "$X = " & $X & @CRLF & "$Y = " & $Y)

Thanks :whistle:

Link to comment
Share on other sites

g2g, hopefully someone will help you out, be back in 3hrs

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

I'm back, did maxilla's reply help you?

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

Thanks Cyber + Maxilla

I'm back, did maxilla's reply help you?

I'd already done something similar to Maxilla's suggestion, using differently named/type of loops to blag my way through, but wondered as to the theory of having any number of loops within loops, if needed.

I thought it'd be a case of nesting loops in the same way as you would use brackets in a complex maths equation, such as shown in my original attempt using multiple While/WEnds (which, of course, didn't work), so it appears you can't have more than one loop using the same type of loop nested directly within itself.

It's become a moot point now as I've used another method (similar to Maxilla's) to hack it to achieve the same goal, but it intrigued me nonetheless.

Cheers :whistle:

Link to comment
Share on other sites

Thanks Cyber + Maxilla

I'd already done something similar to Maxilla's suggestion, using differently named/type of loops to blag my way through, but wondered as to the theory of having any number of loops within loops, if needed.

I thought it'd be a case of nesting loops in the same way as you would use brackets in a complex maths equation, such as shown in my original attempt using multiple While/WEnds (which, of course, didn't work), so it appears you can't have more than one loop using the same type of loop nested directly within itself.

It's become a moot point now as I've used another method (similar to Maxilla's) to hack it to achieve the same goal, but it intrigued me nonetheless.

Cheers :whistle:

yeah the while within the while didn't seem to work, I dono, the for loops work well though

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Link to comment
Share on other sites

yeah the while within the while didn't seem to work, I dono, the for loops work well though

Easy to see why the suggested while within while (or actually while within do-loop) did not work. It works when you change it as follows: (read my comments!)

AutoItSetOption("MouseCoordMode", 0)
$X = 100
$Y = 100
Do
    While $X < 300
        MouseMove($X, $Y, 5)
        $X = $X + 1
    WEnd
    $Y = $Y + 1
    $X = 100; <-- RESET THE $X! If you don't reset the $X, it stays on 300 after adding 1 to $Y and the 'While $X < 300' condition is never met anymore
Until $Y >= 105
MsgBox(0, "Result", "$X = " & $X & @CRLF & "$Y = " & $Y)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

But still, you can do it WAY easier than using the while loops:

AutoItSetOption("MouseCoordMode", 0)
For $y=100 To 105
    For $x=100 To 300 Step 20
        MouseMove($X, $Y, 5)
    Next
Next
MsgBox(0, "Result", "$X = " & $X & @CRLF & "$Y = " & $Y)

(Added the 'Step 20' to make it run faster and you dont have do wait so long for your test to finish :whistle:)

Note that For-Next is good to use if the amount of loops is known beforehand, and While-WEnd and Do-Until loops are better if you don't know the amount of loops (for instance in an app that has a main loop waiting for a user action). Typically you want to use While when you want to do something as long as a condition is met, and Do when you want to do something until a condition is met.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Marvellous, that makes great sense. I'm still new to all this, so am learning as I go!

Much appreciated. :whistle:

Just to be complete: you can nest loops to any depth you want to. (Also you can mix different kinds of loops or use the same ones. (Whatever suits your needs.) (Like you said, just like in math expressions you can nest as deep as you want to. (The only things you need to make sure is that no nesting levels overlap (just like in math))) That's about it. :P)

Maybe a nice excercise in programming philosophy and logic would be the following example/thought experiment (that I wrote for my girlfriend some years ago when trying to explain these same concepts of nested loops to her; had to look it up and translate it from Dutch into English though, but hey, everything to inspire others!):

You see, if you think this through far enough, this question will eventually appear: "what if I want to nest a loop to a level that I don't know before, for instance if the depth of the nest depends on a user action or a value that is determined while running the program? That is impossible to hardcode!"

Well, it is impossible to hardcode directly, but for that concept you can use recursion, meaning a function with a loop that calls itself, making it call itself, making it call itself ....etc........ until some point is reached (called an escape condition) and then returning some value with which the previous nesting level can do something, which makes it return a value with which the previous nesting level can do something, .......etc.......

If you're wondering about the use of this concept: imagine a simple 2D maze-solving algorithm that hugs one side of the corridoor until it can't go any further, then turns around (and hugs the other side - this is a form of depth-first search algorithm with backtracking). Now imagine the program not knowing the amount of crossings to "remember" for backtrackig beforehand, for instance because you let a user create a custom 2D maze for the algorithm to solve.

Now here, you can easily imagine (and almost as easily create :D) a recursive wall-hugging algorithm that goes on forever and EVENTUALLY it will have found an exit out of the maze, or have covered the whole maze if there wasn't an exit. But what if the user created an infinitely big maze, then the program would take infinite time. If you don't want that, the 'recursion escape condition' might be that either it finds an exit, or after X minutes or X crossings, the recursion breaks (escapes) and tells the user: 'your maze is too big to finish for me within X minutes'.

This leaves some loose ends though if you think it through even further... But, end of lecture! :lmao:

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...