← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #689956]: Trouble using onChange in a for loop

 

Question #689956 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/689956

    Status: Open => Answered

RaiMan proposed the following answer:
The first approach is correct, since the change event does not vary in
the loop.

When observe() is started, a screenshot is taken and then every 1/ObserveScanRate seconds a new screenshot is taken and compared with the one taken before.
If changes are detected, the handler is visited.
If you do not stop the observer, it will go on checking for changes until the given oberserve time elapses.

With your given script, you can only guess, that the handler is visited,
if the next click happens within less than 12 seconds.

I suggest, to restrict the observed region to the area of the observed
map. This avoids false positives (changes that might happen elsewhere on
the screen).

You are setting the observe scan rate to 0.5 (checking every 2 seconds
for changes) - hope it is your intention.

This is my testscript:

def handler(evt):
    clen = len(evt.getChanges())
    print "changed", clen
    cr = evt.getChanges()[0]
    for n in range(1, clen):
        cr = cr.union(evt.getChanges()[n])
    cr.highlight(2)
    evt.stopObserver()

reg = SCREEN
# reg = Region(25,111,548,285) # uncomment to have a restricted area
reg.highlight(2)
reg.onChange(10000, handler) 
for n in range(3):
    print n
    reg.observe(5)

comment on 
reg.onChange(10000, handler) 

if the observed region is not the whole screen, then this works with the defaults:
reg.onChange(handler)

A higher pixel value is only needed if you want to ignore smaller
changes. In case of whole screen, this filters changes happening outside
the region of interest. But as mentioned: it is always better to
restrict the observed region.

In my example I trigger the changes by switching windows on the screen.

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.