views:

51

answers:

1

The following Java code when run on Windows XP will lock the desktop, however when run on Vista or Windows 7 it doesn't lock the desktop. I'd appreciate it if someone could show me how to get this to work in Vista and Windows 7 using Java only.

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();
  }
 }
}
+1  A: 

Sounds like Vista's UAC is preventing the JVM from feeding events into the OS event queue. Since you didn't mention it, I assume you aren't seeing an exception. Can you try running this with Administrator privileges?

Jim Garrison
Hi Jim, you assumed correctly i do not see an exception. But when I set the javaw.exe file to run with admin privileges I now get the following exception: "Exception occurred executing command line. Cannot run program "C:\Program Files\Java\jre6\bin\javaw.exe" (in directory "C:\Users\sja26\Documents\Eclipse Workspace\Page"): CreateProcess error=740, The requested operation requires elevation
sja26
Is "Exception" a Java Exception? How did you "set [it] to run with admin privileges"?
Jim Garrison