Jump to content
Sign in to follow this  

Screen Searching

Recommended Posts


I've been looking for a good way to search the entire screen for specific coordinates based on what is actually out there. This is what I came up with. If anyone has optimization ideas, I'd love to hear them. It usually takes my system a full minute to search from the top of the screen to the bottom (depending on how unique the key area is).

[edit] To clarify, this script first uses the mouse location to identify a unique 10x10 pixel area of the screen and sticks that information into a string. It then searchs the entire screen for that unique information[/edit]

In application I will have the specific search string hard coded.

P.S. Forgive the exessive commentation, It helps me think things through.


HotKeySet("{ESC}", "Terminate")
HotKeySet("{F8}", "SetKey")
HotKeySet("{F7}", "FindKey")

Global $KeyStr="",$KeyShort=""
Global $KeyColor, $KeyCoord, $Target, $found=0

; Main loop (waiting for commands)
While 1

; Creates strings for identifying unique screen information
; ---------------------------------------------------------
; $KeyCoord holds the upper left coords for our 10X10 pixel unique location
; $KeyColor holds the color value of the first pixel of the string
; $KeyShort holds the first 10 pixels of the string
; $KeyString holds the entire string value
Func SetKey()
    $KeyCoord = MouseGetPos()
    $KeyColor = PixelGetColor($KeyCoord[0],$KeyCoord[1])
    $KeyShort = FillShort($KeyCoord[0],$KeyCoord[1])
    $KeyStr = FillStr($KeyCoord[0],$KeyCoord[1])

; Searches the entire screen for the unique string stored earlier
; ---------------------------------------------------------------
; $found is a flag finding the location
; nested loops walk through the screen and calls for a closer look
; if it finds a pixel of the right color as the first of the unique string
; As it goes I have it move the mouse along the left side to show progress
; If it ever finds that the match has been made it moves the mouse to the
; found location
Func FindKey()
    $found = 0
    For $y = 0 to @DesktopHeight
        If $found = 1 then ExitLoop
        For $x = 0 to @DesktopWidth
            If $found = 1 then ExitLoop
            $color = PixelGetColor($x,$y)
            If $color = $KeyColor Then $Target = Compare($x,$y)
 ; this sleep line reduces CPU load on my system by 20% or so
    If $found = 1 then MouseMove($Target[0],$Target[1])
; Creates a single line 10 pixel wide string for quicker comparison
Func FillShort($x,$y)
    $tmpStr = ""
    For $ix = 0 To 9 
        $color = PixelGetColor($x+$ix, $y)
        $tmpStr = $tmpStr & $color
    Return $tmpStr

; Creates the 10x10 unique string for comparison
Func FillStr($x,$y)
    $tmpStr = ""
    For $iy = 0 To 9
        For $ix = 0 To 9 
            $color = PixelGetColor($x+$ix, $y+$iy)
            $tmpStr = $tmpStr & $color
    Return $tmpStr

; First performs a quick compare then if successful performs the long
; Returns the golden coords if correct
Func Compare($x,$y)
    dim $coords[2]
    $tmpStr = FillShort($x,$y)
    If $tmpStr = $KeyShort Then
        $tmpStr = FillStr($x,$y)
        If $tmpStr = $KeyStr Then
            $found = 1
            return $coords

Func Terminate()
Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

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