问题
I just recently found out about the awt.Robot Library and I'm quite excited to get to use it. I thought I'd play a little prank on my friend and have the robot press control,alt,delete press the lock the computer button but I can't get the program to bring up the control alt delete screen.
Here's my code:
import java.awt.*;
import java.awt.event.KeyEvent;
public class Bot {
public static void main(String[] args)
{
try {
Robot bot = new Robot();
bot.delay(4000);
bot.keyPress(KeyEvent.VK_CONTROL);
bot.delay(100);
bot.keyPress(KeyEvent.VK_ALT);
bot.delay(100);
bot.keyPress(KeyEvent.VK_DELETE);
bot.delay(500);
bot.keyRelease(KeyEvent.VK_CONTROL);
bot.keyRelease(KeyEvent.VK_ALT);
bot.keyRelease(KeyEvent.VK_DELETE);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void pressEnter(Robot bot)
{
bot.keyPress(KeyEvent.VK_ENTER);
bot.delay(40);
bot.keyRelease(KeyEvent.VK_ENTER);
}
}
回答1:
This cannot be done in Windows XP1 (+ patches?) onward with simulated key events.
From a comment here on an old article showing how this used to be able to be simulated:
For secure reasons on Vista we can not broadcast hotkey message to simulate CTRL ALT DEL. To do this on VISTA you need to use a special library "SASLIB" not provided by default...
Anyway, if you use the Win32 (or whatever OS) API directly, you probably have access to the appropriate API to perform a task. For instance, see LockWorkStation:
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.
See Is there a Java library to access the native Windows API? for leads on how the native (Windows) API can be accessed.
1 From the description of the operation I'm assuming the target is Windows.
回答2:
I looked up how to access Ctrl+Alt+Del through the command line and unfortunately it is not possible.
However, it is possible to lock the computer through the command line using this code:
try {
Runtime.getRuntime().exec("rundll32 user32.dll,LockWorkStation");
} catch (IOException ex) {
Logger.getLogger(TimeFrame.class.getName()).log(Level.SEVERE, null, ex);
}
来源:https://stackoverflow.com/questions/19782781/java-awt-robot-ctrlaltdel-does-not-bring-up-desired-screen