# Automate mouse clicks with Mathematica

I need to automate mouse clicks with Mathematica. However, a similiar question was closed on this forum, without any answer.

Consider the following steps:

Dynamic[MousePosition[]]

In this first step I dynamically show the mouse position.

DynamicModule[{col = Green}, EventHandler[ Style["text", FontColor ->
Dynamic[col]], {"MouseClicked" :> (col = col /. {Red -> Green, Green -> Red})}]]

In this second step I create a "clickable" text: when you click it, it changes its color between green and red.

My objective here is twofold:

1) automatically move the cursor position - this has already been answered here. So, following the original answer I can write

Needs["JLink"];
ReinstallJava[];
robotclass = JavaNew["java.awt.Robot"];
robotclass@mouseMove[90, 196];

2) automatically click on the clickable text (and, of course, I want to see its color changing). Here is the code I'm trying to use:

Although I believe I'm using the correct functions (which belong to the Java Robot Class), I'm getting the following error message:

Java::argx1: Method named mouseRelease defined in class java.awt.Robot was called with an incorrect number or type of arguments. The argument was InputEvent.BUTTON1_MASK.

Can anybody give me a hint to solve this problem?

You can use the raw integer value 16, but the correct J/Link syntax for the symbolic name is InputEventBUTTON1UMASK, so your code would look like this:

(* Call LoadJavaClass when needing to refer to static members. *)

robotclass@mousePress[InputEventBUTTON1UMASK];