Java Robot Class XP vs Vista Win7

The following Java code will lock the desktop when launched on Windows XP, but it will not lock the desktop when launched on Vista or Windows 7. I would appreciate it if someone could show me how to make this work in Vista and Windows 7 using only Java.

import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; public class Roboto { //Lock windows desktop using "WinKey + L" public static void main(String[] args) { try { Robot r = new Robot(); r.keyPress(KeyEvent.VK_WINDOWS); r.keyPress(KeyEvent.VK_L); r.keyRelease(KeyEvent.VK_L); r.keyRelease(KeyEvent.VK_WINDOWS); } catch (AWTException e) { e.printStackTrace(); } } } 
+4
source share
1 answer

Vista UAC seems to be stopping the JVM from posting events to the OS event queue. Since you did not mention this, I assume that you do not see an exception. Can you try to run this with administrator privileges?

+1
source

All Articles