How to act on multiple changes in a region

Asked by Mathieu Létourneau on 2019-04-09

Here is what I'm trying to do :

* This needs to be a continuous background process that allows mouse and keyboard interaction
* There are multiple regions being observed at once, but images only appear in one region at the time

This isn't what the script is actually for, but it gives a good representation of what I'm trying to do :

1. I roll a die, which can appear on 3 different regions of the screen
2. Script is idle and waiting for the image of the die to appear in any of these 3 regions
3. Then, perform a specific task based on which region the die appeared in, and what result it gave.

I hope this is clear enough! I'm completely new to programming/scripting and despite reading the documentation, I couldn't figure it out.

I'm using Sikuli 1.1.4

Question information

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

--- continuous background process that allows mouse and keyboard interaction
SikuliX cannot run in background, since it relates on the screen content and uses rel mouse and keybord
So interractions must either be programmed or usually mangle with the workflow and hence disturb it.

--- multiple regions to observe
your case fits best with the observation feature.
region.onChange(imagesize, handler)
where region is the outer region of the subregions.

--- I'm completely new to programming/scripting
That clearly said is a problem with your rather complex case.
You have to find a way, to start with small pieces and then enhance yourself.

Looking for examples in the net might help.

Hi RaiMan,

Thanks for answering so quickly!

By background, I meant that it's running while I'm performing other tasks manually. I'm expecting the script to take over at a precise moment, and control the mouse.

Let me show you the "logic" I have come up with so far :

1. Set three observers in parallel, one for each region
2. Register 6 "Region.onAppear()" events per observer, each with their own handler.

region1 = someRegion
region2 = someRegion
region3 = someRegion

def roll1():

region1.onAppear("roll1.png", roll1)

This code seems to work on a basic level, but I can't make it work on a larger scale for my project. It feels I'm somewhat close, but perhaps it's more complicated than I thought?

I did manage to make a working version for one region using region.findBest(), but it seemed like a convoluted solution when trying to observe multiple regions at once.

Best RaiMan (raimund-hocke) said : #3

ok, the principle approach for one region:

region1 = someRegion
def roll1(event):
        event.repeat(10) # onAppear needs to know, wether it should continue and with what delay

region1.onAppear("roll1.png", roll1)
region1.r.observeInBackground() # observes forever in background

... and now simply the same for each region

Thanks RaiMan, that solved my question.