how does observeEvent work exactly ?

Asked by ONG YI CHONG on 2018-12-07

So ive been reading the sikuliX api on observing events and Im not able to understand certain events that happen when i try coding it out myself.

I wrote this function that clicks a region, and after the click will observe that region for any change in pixels.

def observe_and_click(phone_region, pixel_region, handler, observe_time): # make pixel click on region
    phone_region.onChange(50,handler) # observe for changes in region
    phone_region.observe(observe_time) # how long to observe for

this is the handler function for the above.

def handler(event):
    global i
    r = event.getRegion() # get the observing region for this event
    path = capture(r,"/Users/ongyichong/SikuliX/Scripts",str(i))
    i += 1

So every click of an event would trigger the handler function once since at the end of the handler function i make the observation stop. However my message log prints something like this:
[log] CLICK on L(55,109)@S(0)[0,0 1440x900] (109 msec)


[log] CLICK on L(75,109)@S(0)[0,0 1440x900] (101 msec)


where i get like 3 - 4 print statements for every click i make ? shouldnt Stopobserver() cause the handler to be called once per observe_and_click function call ? I have already tried reading the documentation but i cant seem to find any information that could explain this phenomenon.

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
ONG YI CHONG (calveeen) said : #1

Btw i am running the function Observe_and_click inside a loop and there are certain transition effects when the onChange() is called, for example a menu bar popping up on the phone when it is clicked. Could that explain the multiple handler function being called ?

RaiMan (raimund-hocke) said : #2

I have to check.

In fact it should work like you thought it should work ;-)

Is the phone_region always the same Region OBJECT?

try to define the phone_region once outside the loop together with the
phone_region.onChange(50,handler) # observe for changes in region

so your function should only be:
def observe_and_click(phone_region, pixel_region, handler, observe_time): # make pixel click on region
    phone_region.observe(observe_time) # how long to observe for

ONG YI CHONG (calveeen) said : #3

Yes, phone region is always the same region object. Hey your suggestion worked and it solved my problem ! Thank you so much !!!!

RaiMan (raimund-hocke) said : #4

Thanks for feedback.

Daily business ;-)