Are mouse and keyboard actions thread safe? --- generally no!

Asked by Jaroslav Novotny on 2020-07-17

Hi,
I want to use sikuli mouse and keyboard actions from many concurrent threads in java (using sikulixapi-2.0.4).

This: https://raiman.github.io/SikuliX1/javadocs/org/sikuli/script/Mouse.html leads me to believe at least mouse actions should be thread safe, but couldn't find anything on type() keyboard actions.

There is this old answer: https://answers.launchpad.net/sikuli/+question/225919 stating:
"(...)concurrent usage of mouse and keyboard: this is definitely not coordinated by Sikuli among threads."
But the answer is rather old.

Should I wrap those keyboard and mouse actions in a thread safe class or am I ok?

Thanks for answering

Question information

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

The general statement is still valid and will always be:
... concurrent usage of mouse and keyboard: this is definitely not coordinated by Sikuli among threads.

There is only one exception (but this is not an official feature and not documented):
mouse actions like click are internally guarded, so that the underlying different Java AWT Robot actions (mouseDown, mouseUp, mouseMove and delay), that form such a top level action can be performed and completed, without interaction from other threads.
This was necessary, to allow mouse actions in handlers of observers running in background.
This is not implemented for any other SikuliX feature.

So it is your responsibility, to coordinate the usage of the top level mouse and keyboard actions including clipboard/paste yourself.

BTW: what is your general intention or your use case (... use sikuli mouse and keyboard actions from many concurrent threads)?

Thanks for the quick response and clarification.

My use case is to perform mouse clicks and type in input fields of multiple GUIs, that each has animations. These animations range from 1sec to about 5sec, and it usually goes like:
- click some pattern
- wait for another pattern to show up (1-5s)
- click another pattern or type into a field

So I want to execute code for each GUI in different thread, so when one is waiting for element to show up, another thread can perform actions on the other GUI.

I'll wrap the sikuli calls into a virtualUser class that will have synchronized methods, so only one thread at a time can use the keyboard and mouse. That should do the trick.