2012-11-02 20 views
8

我正在使用Robot類來模擬Java中的按鍵。但我無法按Window鍵+ L,儘管我可以單獨按下它們。這裏是我的代碼:無法在Java中使用機器人按Window + L

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

@ chaitanya10的方法是標準[' java.awt.robot'](http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html) 方法。 – Baz

+0

@Baz ohh ..我不知道awt:P ..謝謝反正:) – PermGenError

+0

是的,他們是在課堂上自己指定的 – Madeyedexter

回答

12

試試這個:

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

令人印象深刻。非常感謝。工作很好,服務我的目的。 – Madeyedexter

+2

我只想補充說,這隻適用於Windows。 – Simeon

+0

這條線如何解決實際問題?你能描述一下這條線嗎? –

0

嘗試:

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

這就是@wilddev所說的。但它不起作用。 – Madeyedexter

+0

你使用的是什麼windows版本? –

+0

我正在使用Windows 8 RTM – Madeyedexter

0

試試這個:

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