简体   繁体   中英

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?

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);

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM