5

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);
}
}
user2864740
  • 60,010
  • 15
  • 145
  • 220
Bobby Strickland
  • 175
  • 4
  • 14

2 Answers2

5

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.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

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);
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
JFreeman
  • 974
  • 1
  • 10
  • 26