How exactly does onChange() work

Asked by ONG YI CHONG on 2018-12-10

Just to confirm for Region.onChange(), is the handler function called as soon as the region detects the min changed pixel size or the observation ends ? What if there are transition effects during the onChange method ? eg. a popup window sliding up/down

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

Internally the observe simply is a loop, that constantly compares the last screenshot with the current screenshot to detect changes.
If changes are detected, the handler is visited as part of the loop workflow.
Hence detection is paused until the return from the handler.

In the first place, the detection rate (loops per second) mainly depends on the region size (complete screen = slow, small region = fast).

On modern systems the scan rate should be 1 - 2 per second for whole screen.
With smaller regions, it might be more than 10 per second.

--- What if there are transition effects during the onChange method
changes happening on the screen while in handler are not seen.
The next check is the screen/region after leaving the handler against the screen/region before entering the handler.

There are features available to influence the timing - see docs.

ONG YI CHONG (calveeen) said : #2

May i check if the handler code is asynchronous ? For example part of sikuli code can be running while the handler function is being called ?

RaiMan (raimund-hocke) said : #3

run the observe in background (see docs).
Mouse actions are synchronised at the Region object level between foreground and background (observing and handler).

So if you want to have mouse actions in the handler, they should be done against an exclusive region object.

ONG YI CHONG (calveeen) said : #4

What do you mean by exclusive region object ?

RaiMan (raimund-hocke) said : #5

A new Region object, that is not referenced elsewhere with mouse actions, only inside the handler.

reg1 = Region(...)
reg2 = reg1

now reg1 and reg2 are the same identical Region object. Changes to reg2 are seen in reg1 also.

reg1 = Region(...)
reg2 = Region(reg1)

now reg2 has the same aspects as reg1, but is a different object. Changes to reg2 are not seen in reg1.

To synchronise mouse actions, the referenced region objects are used, to not interfere a mouse action by another running in parallel (foreground/background)

Hope it helps

ONG YI CHONG (calveeen) said : #6

Can i just check that the observe scan rate attribute influences the number of times the screenshots are being compared ? For example if you set observe time to 1 second, then within that one second x number of screenshots are compared for observeScanRate = x. So if any of the x observations trigger the onChange method, the handler function is called and the observation time is paused. Once the handler function returns, the remaining observation time is resumed ?

ONG YI CHONG (calveeen) said : #7

Hey RaiMan, i was just thinking if i want to detect changes in UI elements completely (with transition effects), would it be better if i coded a function such that at t = 0 (when i click the UI element) i capture the current screenshot, and then at t = 0.1, i capture another screenshot and compare the similarities between the two screenshots.

I was thinking of something like:
take old screenshot
take new screenshot
compare screenshots.

With this function i am assuming that all UI elements transition will complete within 0.1s. Whereas the handler() in the onChange() function could be called between t = 0 and t = 0.1, and during that time the transition effect may not have fully completed ?

RaiMan (raimund-hocke) said : #8

I think with all the features of observe it should be possible to achieve what you want/need.

... but feel free, to implement any other solution with the features of SikuliX.

Can you help with this problem?

Provide an answer of your own, or ask ONG YI CHONG for more information if necessary.

To post a message you must log in.