Jump to content

Screen Searching


SpookMeister
 Share

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.

-SpookMeister

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
    sleep(1000)
WEnd

; 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])
EndFunc

; 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)
        Next
 ; this sleep line reduces CPU load on my system by 20% or so
        Sleep(50) 
        MouseMove(0,$y,0)   
    Next
    If $found = 1 then MouseMove($Target[0],$Target[1])
EndFunc
    
; 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
    Next
    Return $tmpStr
EndFunc

; 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
        Next
    Next
    Return $tmpStr
EndFunc

; 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
            $coords[0]=$x
            $coords[1]=$y
            return $coords
        EndIf
    EndIf   
EndFunc

Func Terminate()
    Exit
EndFunc
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]

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...