Search code examples
javawindows-8awtrobot

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

Solution

  • Try this instead:

    Runtime.getRuntime().exec("rundll32 user32.dll,LockWorkStation");