Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot press Window+L using robot in Java

I am using the Robot class to simulate key press in Java. But i am unable to press Window key+L although i am able to press them individually. Here is my code:

private void pressKey()
{
    Robot r=new Robot();
    robot.keyPress(KeyEvent.VK_WINDOWS);
    robot.keyPress(KeyEvent.VK_L);
    robot.keyRelease(KeyEvent.VK_WINDOWS);
    robot.keyRelease(KeyEvent.VK_L);
}
like image 393
Madeyedexter Avatar asked Nov 02 '12 10:11

Madeyedexter


People also ask

How does Java Robot work?

The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue.


1 Answers

Try this instead:

Runtime.getRuntime().exec("rundll32 user32.dll,LockWorkStation");
like image 94
Boann Avatar answered Oct 16 '22 12:10

Boann