Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate SHIFT + Mouse key press in java

Tags:

java

swing

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?

like image 626
DarkKnight Avatar asked Aug 29 '12 23:08

DarkKnight


2 Answers

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);
like image 186
MadProgrammer Avatar answered Oct 31 '22 02:10

MadProgrammer


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.

like image 26
Ossie7 Avatar answered Oct 31 '22 04:10

Ossie7