DarkKnight
DarkKnight

Reputation: 243

How to simulate SHIFT + Mouse key press in java

I want to move the mouse pointer to a particular location and perform SHIFT + right mouse button click. I am able to move the mouse to a location but not able to simulate mouse click.

Robot r = new Robot();
r.mouseMove(x1,y1);

What should i do to simulate the expected mouse click?

Upvotes: 5

Views: 5300

Answers (3)

Alex Fischer
Alex Fischer

Reputation: 545

Pressing a key with the robot class is simple:

r.keyPress(KeyEvent.VK_SHIFT); //hold down shift
r.mousePress(InputEvent.BUTTON3_MASK); //perform a right click
r.mouseRelease(InputEvent.BUTTON3_MASK); //release right click
r.keyRelease(KeyEvent.VK_SHIFT); //release shift

InputEvent and KeyEvent are in java.awt.event.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347314

I think you will need just a little extra information for Robot to complete successfully, try

r.keyPress(KeyEvent.VK_SHIFT);
r.mousePress(KeyEvent.BUTTON3_MASK);
r.mouseRelease(KeyEvent.BUTTON3_MASK);
r.keyRelease(KeyEvent.VK_SHIFT);

Upvotes: 7

Ossie7
Ossie7

Reputation: 370

this should do the trick:

r.mousePress(InputEvent.BUTTON3_MASK);
r.mouseRelease(InputEvent.BUTTON3_MASK);

Important here is not to forget to press and release it, since those are 2 diffrent events.

Upvotes: 1

Related Questions