[1.1.0] Mouse interrupt action after mouse was moved externally by user or other process

Asked by Leif

Hello! I'm using sikuli together with another program. So far everythings been working out perfectly. I do have a question regarding mouse control.

This second program performs action from time to time; by hovering the mouse to a location and clicking. The operation it performs is abit more complicated but not relevant.

This seconday program has a higher priority then sikuli. The action is performed at random times so It's not possible to program this with time. I'm therfore curious. Is there any way for sikuli to take pause everytime the mouse is moved/clicked by? Say that sikuli stops all it's action for 3 seconds after last mouse movement is performed.

Have a fantastic summer!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

With latest builds of version 1.1.0 you can do the following:

# somwhere before your main workflow starts:

def handler(e):
  wait(3)

Mouse.setMouseMovedCallback(handler)

With each mouse action done by SikuliX, it is checked, wether the mouse is still in the place, where it was left with the last moose action before.
If the mouse is found somewhere else, it is supposed, that the mouse was moved by some Sikuli-external process or the user.

Among other options you can define a handler callback), that is called in this case.

In your case you want to pause any Sikuli activities for 3 seconds, which is done by the wait(3).

Revision history for this message
Leif (leifhaa) said :
#2

Brilliant really! Works very well! I've got a followup question if you dont mind. As you mention, sikuli recognized if the mouse is still in place or have been moved from where sikuli left it. My secondary program will override the cursor controll anywhere from 3 seconds up to 15. If it uses 3 seconds, It's all good. If it uses 6 example, sikuli will gain controll for a second before letting go again.

I'm curious if sikuli could during the wait(3) could update the cursors location and see if It's moved. if it have, then wait another 3 seconds, create a new keypoint; if moved; wait another 3 seconds and so on.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

def handler(e):
  loc = Mouse.at() # store current location
  wait(3)
  for i in range(5):
    if loc.equals(Mouse.at()): # if current location == stored loc, the leave loop (not moved during the last 3 seconds)
      break;
    loc = Mouse.at()
    wait(3)

Revision history for this message
Leif (leifhaa) said :
#4

Once again thanks! Works exactly as intended. I feel horrible to nag you with all these follow-up questions; hope you dont mind! My secondary software is, like sikuli, a program that reads the screen and is sensitive to changes, popups, animations etc.

As the script above is performed, another sikuli process is started (lasting less then 1 second) creating two red squares (1 smaller, 1 bigger) to indicate where the mouse is. Would it be possible to disable these two red square animations as they could easily disturb my secondary script.

Sorry to be such a nag about the details!

Revision history for this message
RaiMan (raimund-hocke) said :
#5

ok, I will make that gimmick optional ;-)

watch out for the fix on nightly build (maybe Monday) that says:

added: Mouse.setMouseMovedHighlight(true/false), where true will be the default.

You would then have to say:

Mouse.setMouseMovedCallback(handler)
Mouse.setMouseMovedHighlight(false)

Revision history for this message
RaiMan (raimund-hocke) said :
#6

By the way:
I feel horrible to nag you with all these follow-up questions; hope you dont mind!

... not "needed" in any case.
If you have questions or suggestions, just post them, without any bad feelings.
Especially suggestions at no time are something like torture for me. I am happy, that people are using SikuliX, are talking about their use cases and make suggestions or ask questions.

Revision history for this message
Leif (leifhaa) said :
#7

Thanks RaiMan, that solved my question.