9

I am trying to get the Robot class to right click on a image on the screen so that I can save it. The problem is though that I cannot seem to get the Robot to successfully simulate a right click. Here is some sample code of what I am doing.

It seems that BUTTON2_DOWN_MASK is the mask for the scroll wheel. Whenever I execute this code it first just regularly clicks at the location but then the scroll wheel super fast move circle pops up and tells me I can now scroll at light speed, but I wanted a right click..

CODE:

// This is the function I use to simulate a full click at location x,y on the screen
// Rob is my Robot
public void click(int x, int y, int mask)
{
    rob.mouseMove(x, y);
    sleepy(1000);
    rob.mousePress(mask);
    rob.mouseRelease(mask);
}

// This is the few lines of code that call this function
// sleepy just calls the Thread.sleep function.

sleepy(1000); // Wait one second
click(705, 390, InputEvent.BUTTON1_DOWN_MASK);
sleepy(1000);
click(705, 390, InputEvent.BUTTON2_DOWN_MASK);
sleepy(1000);

1 Answers1

18

use InputEvent.BUTTON3_DOWN_MASK for a right click. BUTTON2 is as you noticed correctly the mouse wheele and the left click is BUTTON1.

see this example.

Simulant
  • 19,190
  • 8
  • 63
  • 98
  • Basically since Java does a blunder here. Usually the right mouse button is considered the second mouse button and the middle one is the third. The fourth button is usually located at your thumb. – Martin Kersten Nov 23 '15 at 12:36