Unable to simultaneously listen to global hotkeys and do desktop surveillance

Asked by colonel_claypoo on 2019-08-09

Hi, just getting into SikuliXx 1.1.4,

What I'm trying to achieve is have a global exit hotkey as described under https://sikulix-2014.readthedocs.io/en/latest/interaction.html?highlight=Env.addHotkey#listening-to-global-hotkeys

while at the same time be able to have the script constantly monitor the desktop for images to pop up.

Here's my code thus far:

# hotkey to stop the script
exitScript = False; # global to communicate with main loop
def xHandler(event):
  global exitScript
  exitScript = True # tell main loop that hotkey was pressed
# add the hotkey with its handler
Env.addHotkey("x", KeyModifier.CTRL + KeyModifier.SHIFT, xHandler)

while True:

    if has(Pattern("1.png").similar(0.66)):
        click(Pattern("1.png").similar(0.66))
    if has ("2.png"):
        click("2.png")
        paste("<email address hidden>")
    if has ("3.png"):
        click()

    if (exitScript):
        exit()

The way I have it here works for one iteration.
I can exit the lopp at any time by pressing the defined keyboard shortcut cominbation.

Those three images are successfully found and clicked on once they appear on the screen.
However, soon after that the loop fails because 1.png cannot be found.

Here's the log:

[log] CLICK on L[1130,708]@S(0) (275 msec)
[log] CLICK on L[1025,442]@S(0) (273 msec)
[log] CLICK on L[1272,599]@S(0) (272 msec)
[error] script [ Test ] stopped with error in line 18
[error] FindFailed ( 1.png: (568x851) seen at (846, 283) with 0.75 in R[0,0 1920x1200]@S(0) )
[error] --- Traceback --- error source first
line: module ( function ) statement
18: main ( <module> ) click("1.png")
[error] --- Traceback --- end --------------

So is there a logic to be able to constantly monitor for desktop activity until I press the global exit hotkey?

Thanks for your help and keep up the good work.

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2019-08-15
Last query:
2019-08-15
Last reply:
2019-08-15
Best RaiMan (raimund-hocke) said : #1

I guess a timing problem, because the loop steps are executed too fast:

assuming
- 1.png is found
- 1.png is clicked and starts to vanish
- loop repeats
- has(1.png) still sees 1.png
- but click(1.png) no longer finds it

so my suggestion:

waittime = 1.0

    if has(Pattern("1.png").similar(0.66)):
        click()
        wait(waittime)
    if has ("2.png"):
        click()
        wait(waittime)
        paste("<email address hidden>")
    if has ("3.png"):
        click()
        wait(waittime)
    if (exitScript):
        exit()

you might experiment with the waittime: 0.5 might be sufficient

Hi RaiMan, thanks for your proposal. I will try it out next week and report back.