Sign in to follow this  
Followers 0
user101

a little help for a beginner please

13 posts in this topic

While 1

$var = PixelGetColor (300,100)

If $var = 0xFF0404 Then

MouseClick("left", 300, 100, 1, 5)

else

MouseClick("left")

EndIf

WEnd

ok basically I have read the help file but I just cant figure out how to do this, I have underlined the position value of the pixelgetcolor and mouseclick code also the color of the variable.. What I would like to know is how do I input these manually without having to include them in the code, basically kinda like what the autoitinfowindow does, I wish I could decode it but its too complicated for me as of now.. when you point your mouse at a position it gets its x,y coordinates and also its color hex code, what I want to do is have those value's inputed into the place of the underlined parts of the example code above once I click a certain spot on the screen, can anyone give me some advice?

Share this post


Link to post
Share on other sites



Global $xyPos

HotKeySet("{ESC}", "Terminate")

HotKeySet("z","xyPos")

Func Terminate()

Exit

ToolTip("")

EndFunc

Func xyPos()

$xyPos = MouseGetPos()

EndFunc

While 1

$var3 = PixelGetColor($xyPos[0], $xyPos[1])

$closeit2 = PixelGetColor(849, 369)

If $var3 = $var3 Then

MouseClick("left", $xyPos[0], $xyPos[1], 1, 5)

Sleep(500)

WEnd

here is the code ive tried out so far but I keep getting errors i deleted all the other parts and left the main parts to focus on

Share this post


Link to post
Share on other sites

Func _MouseGetColor()
    Local $pos = MouseGetPos(), $aReturn[3] = [$pos[0], $pos[1], Hex(PixelGetColor($pos[0],$pos[1]), 6)]
    Return $aReturn
EndFunc

Not really sure what you're trying to do, but that function will return an array with [x, y, hex color].

Share this post


Link to post
Share on other sites

okei thanks for the code, but what I want to do is implement the position of the mouse coordinates I click into var3 and also into

mouseclick("left", x, y, 1, 5) so this is what will happen, I will make a message box saying please click the item, then the user will click the item on his screen, and then the x,y position of that click will be inputed into the x and y coordinates here mouseclick("left", x, y, 1, 5) and here

$var = PixelGetColor (x,y)

Share this post


Link to post
Share on other sites

does no one know how to do this? 70+ views but no replies, im trying here but I cant seem to figure it out on my own..

Ive started with this so far, this is what I want this code to do but doesn't seem to work

HotKeySet("z","getmousepos")

Local $a[2]

Local $b

Func getmousepos()

MsgBox(0,"wow","hello please click a pos",2)

if MouseClick("left") then $a = MouseGetPos()

$b=1

EndFunc

While 1=1

WEnd

if $b=1 then

MsgBox(0, "Mouse x,y:", $a[0] & "," & $a[1])

$b=0

EndIf

I click z to activate the function, a msgbox pops up telling me to click anywhere on my screen, I then click a any place on my screen and then a message box will pop up that tells the x,y position of that click I made.. can anyone see whats wrong with my code?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I'm not even going to bother point out all the things wrong with that code. Though I am going to question this line

if MouseClick("left") then $a = MouseGetPos()

Have you read the help file?

MouseClick

Perform a mouse click operation.

[...]

Success: Returns 1.

Failure: Returns 0, the button is not in the list or invalid parameter as x without y.

What this means, is the MouseClick function MAKES your mouse click, not tests if you have clicked it.

With that said, I am not familiar with any functions that test if you have clicked your mouse. I haven't researched into it, so there may be something out there (don't expect anyone to do this for you). It may just be easier to settle with a hotkey press to capture the X,Y.

Learn to use the help file. It is your friend.

EDIT: You're lucky. I got bored.

HotKeySet("z","getPos")
Local $getPos = FALSE

MsgBox(0, "Get color at X,Y", "Hover mouse over color, press Z to capture.")

While 1
    Sleep(100)
    If $getPos = TRUE Then
        $getPos = FALSE
        Local $colorAtXY = _MouseGetColor()
        MsgBox(0, "$colorAtXY", "X: " & $colorAtXY[0] & @CRLF & "Y: " & $colorAtXY[1] & @CRLF & "Color: " & $colorAtXY[2])
    EndIf
WEnd

Func getPos()
    $getPos = TRUE
EndFunc

Func _MouseGetColor()
    Local $pos = MouseGetPos(), $aReturn[3] = [$pos[0], $pos[1], Hex(PixelGetColor($pos[0],$pos[1]), 6)]
    Return $aReturn
EndFunc
Edited by JRouleau

Share this post


Link to post
Share on other sites

I'm not even going to bother point out all the things wrong with that code. Though I am going to question this line

if MouseClick("left") then $a = MouseGetPos()

Have you read the help file?

What this means, is the MouseClick function MAKES your mouse click, not tests if you have clicked it.

With that said, I am not familiar with any functions that test if you have clicked your mouse. I haven't researched into it, so there may be something out there (don't expect anyone to do this for you). It may just be easier to settle with a hotkey press to capture the X,Y.

Learn to use the help file. It is your friend.

EDIT: You're lucky. I got bored.

HotKeySet("z","getPos")
Local $getPos = FALSE

MsgBox(0, "Get color at X,Y", "Hover mouse over color, press Z to capture.")

While 1
    Sleep(100)
    If $getPos = TRUE Then
        $getPos = FALSE
        Local $colorAtXY = _MouseGetColor()
        MsgBox(0, "$colorAtXY", "X: " & $colorAtXY[0] & @CRLF & "Y: " & $colorAtXY[1] & @CRLF & "Color: " & $colorAtXY[2])
    EndIf
WEnd

Func getPos()
    $getPos = TRUE
EndFunc

Func _MouseGetColor()
    Local $pos = MouseGetPos(), $aReturn[3] = [$pos[0], $pos[1], Hex(PixelGetColor($pos[0],$pos[1]), 6)]
    Return $aReturn
EndFunc

shortly after Ive figured how to do this you post the answer T_T but thank you so much still for this, its much better than what I wrote and ive discovered the use of functions being used in codes by dissecting this, with the tut I thought functions can only be used thru hotkeys hahaha, Im sorry I dont expect anyone to write the program for me I want to do it myself all I need is help like what codes I need to be looking at, about the mouseclicking thing it can be done because that is exactly what au3recorder tool does it saves the inputed clicks you make, if only there was a way to take a look at the code in the au3recorder it would be great and probably help solve my answers.. by the way since your more experienced than me this is the code I just completed going on your advice earlier, Im pretty confused about using the return function and it kept on giving errors so I took it out here is what I wrote

HotKeySet("x","_as")

Local $a[3]

while 1=1

Local $v

while $v=0

MsgBox(0, "Mouse x,y:", $a[0] & "," & $a[1] & " color ," & $a[2])

$v=$v+1

WEnd

WEnd

Func _as()

Local $as = MouseGetPos(), $aReturn[3] = [$as[0], $as[1], Hex(PixelGetColor($as[0],$as[1]), 6)]

$a[0]=$as[0]

$a[1]=$as[1]

$a[2]=$as[2]

$v=0

EndFunc

it's supposed to do exactly what your code (that you just posted) does, and it works for mouse position but when I try to add in the color at $a[2]=$as[2] this is my error Array variable has incorrect number of subscripts or subscript dimension range exceeded.: what did I do wrong?

Share this post


Link to post
Share on other sites

Your $as variable is a 2-element array. It holds the X,Y from MouseGetPos()

On this line you are referencing a non-existing element in the $as variable.

$a[2]=$as[2]

Either remove the $aReturn variable (as it's not really needed in your code)

Func _as()
    Local $as = MouseGetPos()
    $a[0]=$as[0]
    $a[1]=$as[1]
    $a[2]=Hex(PixelGetColor($as[0],$as[1]), 6)
    $v=0
EndFunc

Or use it to assign the $a variable.

Func _as()
    Local $as = MouseGetPos(), $aReturn[3] = [$as[0], $as[1], Hex(PixelGetColor($as[0],$as[1]), 6)]
    $a[0]=$aReturn[0]
    $a[1]=$aReturn[1]
    $a[2]=$aReturn[2]
    $v=0
EndFunc

Share this post


Link to post
Share on other sites

Your $as variable is a 2-element array. It holds the X,Y from MouseGetPos()

On this line you are referencing a non-existing element in the $as variable.

$a[2]=$as[2]

Either remove the $aReturn variable (as it's not really needed in your code)

Func _as()
    Local $as = MouseGetPos()
    $a[0]=$as[0]
    $a[1]=$as[1]
    $a[2]=Hex(PixelGetColor($as[0],$as[1]), 6)
    $v=0
EndFunc

Or use it to assign the $a variable.

Func _as()
    Local $as = MouseGetPos(), $aReturn[3] = [$as[0], $as[1], Hex(PixelGetColor($as[0],$as[1]), 6)]
    $a[0]=$aReturn[0]
    $a[1]=$aReturn[1]
    $a[2]=$aReturn[2]
    $v=0
EndFunc

hi thank you for your help :D I appreciate it, ive done a little experementing with what you told me and removed the aReturn variable completely and fixed it just a little bit, so this is my current code after editing it all, and it works for me, but is there any way to make it shorter? I know about multidimentional arrays but when I try to use it I end up with a lot of errors everywhere, I was hoping to shorten my variable's to just 2 multiDimentional arrays, and also im kind of confused with switch and case but some say its better than the if statement, I was kind of hoping for something that proceeds thru the if's by steps, like find do the first if until it is found before moving to the next if statement and so on.. here so far is the code I completed

HotKeySet("{ESC}","close")
HotKeySet("1","c1")
HotKeySet("2","c2")
HotKeySet("3","c3")
HotKeySet("4","c4")
HotKeySet("5","c5")
HotKeySet("6","c6")
HotKeySet("7","c7")
HotKeySet("8","c8")
HotKeySet("9","c9")
HotKeySet("0","c10")
HotKeySet("q","c11")
HotKeySet("z","start")

;mouse positions
 Local $a1[2], $a2[2], $a3[2], $a4[2], $a5[2]
 Local $a6[2], $a7[2], $a8[2], $a9[2], $a10[2], $a11[2]
;colors
 Local $b1, $b2, $b3, $b4, $b5
 Local $b6, $b7, $b8, $b9, $b10, $b11


 Local $v

while 1=1
    While $v=True

If $b1=pixelGetColor($a1[0],$a1[1]) Then MouseClick("left", $a1[0], $a1[1], 1, 5)
    Sleep(50)
If $b2=pixelGetColor($a2[0],$a2[1]) Then MouseClick("left", $a2[0], $a2[1], 1, 5)
    Sleep(50)
If $b3=pixelGetColor($a3[0],$a3[1]) Then MouseClick("left", $a3[0], $a3[1], 1, 5)
    Sleep(50)
If $b4=pixelGetColor($a4[0],$a4[1]) Then MouseClick("left", $a4[0], $a4[1], 1, 5)
    Sleep(50)
If $b5=pixelGetColor($a5[0],$a5[1]) Then MouseClick("left", $a5[0], $a5[1], 1, 5)
    Sleep(50)
If $b6=pixelGetColor($a6[0],$a6[1]) Then MouseClick("left", $a6[0], $a6[1], 1, 5)
    Sleep(50)
If $b7=pixelGetColor($a7[0],$a7[1]) Then MouseClick("left", $a7[0], $a7[1], 1, 5)
    Sleep(50)
If $b8=pixelGetColor($a8[0],$a8[1]) Then MouseClick("left", $a8[0], $a8[1], 1, 5)
    Sleep(50)
If $b9=pixelGetColor($a9[0],$a9[1]) Then MouseClick("left", $a9[0], $a9[1], 1, 5)
    Sleep(50)
If $b10=pixelGetColor($a10[0],$a10[1]) Then MouseMove($a1[0],$a1[1], 5)
    Sleep(50)
If $b11=pixelGetColor($a11[0],$a11[1]) Then MouseMove($a2[0],$a2[1], 5)
    Sleep(50)

    WEnd
WEnd


Func start()
if $v = False then
$v = True
MsgBox(0,"","STARTED")
Else
$v=False
MsgBox(0,"","STOPED")
EndIf
EndFunc

Func c1()
$a1 = MouseGetPos()
$b1 = PixelGetColor($a1[0],$a1[1])
$b1 = "0x" & hex($b1, "6")
EndFunc

Func c2()
$a2 = MouseGetPos()
$b2 = PixelGetColor($a2[0],$a2[1])
$b2 = "0x" & hex($b2, "6")
EndFunc

Func c3()
$a3 = MouseGetPos()
$b3 = PixelGetColor($a3[0],$a3[1])
$b3 = "0x" & hex($b3, "6")
EndFunc

Func c4()
$a4 = MouseGetPos()
$b4 = PixelGetColor($a4[0],$a4[1])
$b4 = "0x" & hex($b4, "6")
EndFunc

Func c5()
$a5 = MouseGetPos()
$b5 = PixelGetColor($a5[0],$a5[1])
$b5 = "0x" & hex($b5, "6")
EndFunc

Func c6()
$a6 = MouseGetPos()
$b6 = PixelGetColor($a6[0],$a6[1])
$b6 = "0x" & hex($b6, "6")
EndFunc

Func c7()
$a7 = MouseGetPos()
$b7 = PixelGetColor($a7[0],$a7[1])
$b7 = "0x" & hex($b7, "6")
EndFunc

Func c8()
$a8 = MouseGetPos()
$b8 = PixelGetColor($a8[0],$a8[1])
$b8 = "0x" & hex($b8, "6")
EndFunc

Func c9()
$a9 = MouseGetPos()
$b9 = PixelGetColor($a9[0],$a9[1])
$b9 = "0x" & hex($b9, "6")
EndFunc

Func c10()
$a10 = MouseGetPos()
$b10 = PixelGetColor($a10[0],$a10[1])
$b10 = "0x" & hex($b10, "6")
EndFunc

Func c11()
$a11 = MouseGetPos()
$b11 = PixelGetColor($a11[0],$a11[1])
$b11 = "0x" & hex($b11, "6")
EndFunc




Func close()
    Exit
EndFunc

it does the following pressing the hotkey's from the numbers 0-9 sets the pixel searcher and mouseposition locator of each corresponding if statement, then when z is hit it will execute, I know its really messy but I organized it as simply as possible, im really new to programming all I have to go with really was the help file and a few tutorial videos on youtube haha but I hope you can understand it

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

#include <Array.au3>

HotKeySet("{ESC}","close")
HotKeySet("z","start")
Global Const $hotKeys[11] = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q"]
For $i = 0 To UBound($hotKeys) - 1      ;   Shortened code by using loop to assign HotKeys.
    HotKeySet($hotKeys[$i],"getColor")  ; Using UBound makes the amount of HotKeys 
Next                                    ; easily changable by updating the $hotKeys var

Global $colorAtXY[UBound($hotKeys)][4], $i = 0, $active = FALSE ;Define variables

;   Whenever using a continous loop, make sure there is a sleep function in there.
; This will prevent the script from maxing out your CPU.
; I condensed all of the if statements into the following code. Uses $i to cycle through
; the different positions and will not continue unless the current one completes successfully.
; Also take note of the Switch..Case..EndSwitch. 
; Side note: When ever you need to repeat a block of code, odds are you can use some sort of
; loop, recursion or function to reduce the size of it.
While 1
    Sleep(100)
    While $active
        Sleep(50)
        If $colorAtXY[$i][2] = PixelGetColor($colorAtXY[$i][0],$colorAtXY[$i][1]) Then
            Switch $i
            Case 0 To 8
                MouseClick("Primary", $colorAtXY[$i][0], $colorAtXY[$i][1], 1, 5)
            Case 9 To 10
                MouseMove($colorAtXY[$i][0], $colorAtXY[$i][1], 5)
            EndSwitch
            $i += 1
            If $i = UBound($hotKeys) - 1 Then $i = 0
        EndIf
    WEnd
WEnd

;   Condensed all of the c#() functions into one. Using _ArraySearch and @HotKeyPressed
; to determine which one was pressed. [x, y, color, hex color] all stored in one array.
; NOTE: Local $i variable does NOT affect the one defined above. I used $i again here
; to show an example of variable scopes. When defined as Local within a function, it
; will then create a new variable (even though it's the same name) for that function.
; If it were defined as Dim $i, then it WOULD affect to other $i variable. Read up
; on Local, Dim, Global for more information.
Func getColor()
    Local $i = _ArraySearch($hotKeys, @HotKeyPressed), $pos = MouseGetPos()
    $colorAtXY[$i][0] = $pos[0]
    $colorAtXY[$i][1] = $pos[1]
    $colorAtXY[$i][2] = PixelGetColor($colorAtXY[$i][0],$colorAtXY[$i][1])
    $colorAtXY[$i][3] = "0x" & Hex($colorAtXY[$i][2], 6)
EndFunc

Func start()
    If Not $active Then
        $active = TRUE
        MsgBox(0,"","STARTED")
    Else
        $active = FALSE
        MsgBox(0,"","STOPPED")
    EndIf
EndFunc

Func close()
    Exit
EndFunc

Also, I stumbled into this while researching something for my own project: _MouseSetOnEvent().

Edited by JRouleau

Share this post


Link to post
Share on other sites

#include <Array.au3>

HotKeySet("{ESC}","close")
HotKeySet("z","start")
Global Const $hotKeys[11] = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q"]
For $i = 0 To UBound($hotKeys) - 1      ;   Shortened code by using loop to assign HotKeys.
    HotKeySet($hotKeys[$i],"getColor")  ; Using UBound makes the amount of HotKeys 
Next                                    ; easily changable by updating the $hotKeys var

Global $colorAtXY[UBound($hotKeys)][4], $i = 0, $active = FALSE ;Define variables

;   Whenever using a continous loop, make sure there is a sleep function in there.
; This will prevent the script from maxing out your CPU.
; I condensed all of the if statements into the following code. Uses $i to cycle through
; the different positions and will not continue unless the current one completes successfully.
; Also take note of the Switch..Case..EndSwitch. 
; Side note: When ever you need to repeat a block of code, odds are you can use some sort of
; loop, recursion or function to reduce the size of it.
While 1
    Sleep(100)
    While $active
        Sleep(50)
        If $colorAtXY[$i][2] = PixelGetColor($colorAtXY[$i][0],$colorAtXY[$i][1]) Then
            Switch $i
            Case 0 To 8
                MouseClick("Primary", $colorAtXY[$i][0], $colorAtXY[$i][1], 1, 5)
            Case 9 To 10
                MouseMove($colorAtXY[$i][0], $colorAtXY[$i][1], 5)
            EndSwitch
            $i += 1
            If $i = UBound($hotKeys) - 1 Then $i = 0
        EndIf
    WEnd
WEnd

;   Condensed all of the c#() functions into one. Using _ArraySearch and @HotKeyPressed
; to determine which one was pressed. [x, y, color, hex color] all stored in one array.
; NOTE: Local $i variable does NOT affect the one defined above. I used $i again here
; to show an example of variable scopes. When defined as Local within a function, it
; will then create a new variable (even though it's the same name) for that function.
; If it were defined as Dim $i, then it WOULD affect to other $i variable. Read up
; on Local, Dim, Global for more information.
Func getColor()
    Local $i = _ArraySearch($hotKeys, @HotKeyPressed), $pos = MouseGetPos()
    $colorAtXY[$i][0] = $pos[0]
    $colorAtXY[$i][1] = $pos[1]
    $colorAtXY[$i][2] = PixelGetColor($colorAtXY[$i][0],$colorAtXY[$i][1])
    $colorAtXY[$i][3] = "0x" & Hex($colorAtXY[$i][2], 6)
EndFunc

Func start()
    If Not $active Then
        $active = TRUE
        MsgBox(0,"","STARTED")
    Else
        $active = FALSE
        MsgBox(0,"","STOPPED")
    EndIf
EndFunc

Func close()
    Exit
EndFunc

Also, I stumbled into this while researching something for my own project: _MouseSetOnEvent().

wow I can't believe you rewrote the code for me :D, I was just hoping for some advice on what I should do haha, thank you, Ive run thru it so many times now but even with your quotes I have a hard time understanding it haha, there are so many parts that Ive never seen before its like I just got done with addition and then you hand me a quadratic equation rofl, but still thank you so much for actually wasting your own time to help me on this im really grateful : ) I will play around with your script a bit until I get what each and every part does exactly :huggles:, it seems that the step by step part really works but failed in my logic for the program I use it on hahaha but I still need it, all I need to do is take some parts out that will be repeated until the next step is found and then proceed with the step by step procedure :, also about the mousesetonevent thing, O_o I read it haha but it only confuses me even more so I will have to stick with hotkeys for now until I learn more about setting them to gui buttons.. maybe you know about this but can you teach me about the idle thing? like lets say the script cannot find any of the pixels it recorded because the program hung (not the script) and it has been idle for a certain amount of time no mouse movement or clicks have been performed for about 1min, how do I get it to press f5 after the idle time is up? Ive really thought this thru haha I tried searching idle on the help topic but find so many random codes that dont apply to what I need it to do... do you know of anything?

Share this post


Link to post
Share on other sites

About the idle time, use TimerInit() and TimerDiff(). For example

[...]
    While $active
        Sleep(50)
        If $colorAtXY[$i][2] = PixelGetColor($colorAtXY[$i][0],$colorAtXY[$i][1]) Then
            Switch $i
            Case 0 To 8
                MouseClick("Primary", $colorAtXY[$i][0], $colorAtXY[$i][1], 1, 5)
            Case 9 To 10
                MouseMove($colorAtXY[$i][0], $colorAtXY[$i][1], 5)
            EndSwitch
            $i += 1
            If $i = UBound($hotKeys) - 1 Then $i = 0
            $lastTime = TimerInit()             ; <---
        EndIf
        If TimerDiff($lastTime) >= 60000 Then   ; <---
            Send("{F5}")                        ;   --
            Sleep(5000)                         ;   --
            $i = 0                              ;   --
        EndIf                                   ; <---
    WEnd
[...]

Share this post


Link to post
Share on other sites

About the idle time, use TimerInit() and TimerDiff(). For example

[...]
    While $active
        Sleep(50)
        If $colorAtXY[$i][2] = PixelGetColor($colorAtXY[$i][0],$colorAtXY[$i][1]) Then
            Switch $i
            Case 0 To 8
                MouseClick("Primary", $colorAtXY[$i][0], $colorAtXY[$i][1], 1, 5)
            Case 9 To 10
                MouseMove($colorAtXY[$i][0], $colorAtXY[$i][1], 5)
            EndSwitch
            $i += 1
            If $i = UBound($hotKeys) - 1 Then $i = 0
            $lastTime = TimerInit()             ; <---
        EndIf
        If TimerDiff($lastTime) >= 60000 Then   ; <---
            Send("{F5}")                        ;   --
            Sleep(5000)                         ;   --
            $i = 0                              ;   --
        EndIf                                   ; <---
    WEnd
[...]

thanks it works, I tried it on a short script finally its working the way I wanted it to, now im reading in to the mouse on event thing but im thinking how will I make it into an array like you did since each click will be the same unlike hotkeys each one has a different assigned key..

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