简体   繁体   中英

Robot to hold multiple key down using Java

I'm trying to build some robot class in JAVA.
I need to robot hold 3 keys at the same time -> (CTRL + SHIFT + DELETE)
I have to achieve this beacuse this accelerator opens a new windows and saves a lot of time.

Here is my code below:

  Robot robot = new Robot();

            robot.keyPress(InputEvent.CTRL_MASK);
            robot.delay(100); 
            robot.keyPress(InputEvent.SHIFT_MASK); 
            robot.delay(150);
            robot.keyPress(KeyEvent.VK_DELETE);

            robot.keyRelease(KeyEvent.VK_DELETE); 
            robot.keyRelease(InputEvent.SHIFT_MASK); 
            robot.keyRelease(InputEvent.CTRL_MASK);

Basically you need to use KeyEvent constants not InputEvent

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_SHIFT); 
        robot.keyPress(KeyEvent.VK_DELETE);

keypress is working with KeyEvent constants. Input Events do not belong.

It is all in the documentation http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html#keyPress(int)

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