Cannot press Window+L using robot in Java

独自空忆成欢 提交于 2019-12-29 01:35:09

问题


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

回答1:


Try this instead:

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



回答2:


Try:

private void pressKey(){
   Robot r=new Robot();
   robot.keyPress(KeyEvent.VK_WINDOWS);
   robot.keyPress(KeyEvent.VK_L);
   robot.keyRelease(KeyEvent.VK_L);
   robot.keyRelease(KeyEvent.VK_WINDOWS);
}



回答3:


Try this:

robot.keyPress(KeyEvent.VK_WINDOWS);
robot.delay(100);
robot.keyPress(KeyEvent.VK_L);
robot.delay(200);
robot.keyRelease(KeyEvent.VK_L);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_WINDOWS);


来源:https://stackoverflow.com/questions/13193293/cannot-press-windowl-using-robot-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!