Use Java to lock a screen

17,046

Once, I wrote something in Java, out of which you can't escape. Really impossible. It is for Windows. Here you go:

import java.awt.Robot;
import javax.swing.JFrame;


public class WindowsSecurity implements Runnable 
{
  private JFrame frame;
  private boolean running;

  public WindowsSecurity(JFrame yourFrame)
  {
    this.frame = yourFrame;
    new Thread(this).start();
  }

  public void stop()
  {
     this.running = false;
  }

  public void run() {
    try {
      this.terminal.getParentFrame().setAlwaysOnTop(true);
      this.terminal.getParentFrame().setDefaultCloseOperation(0);
      kill("explorer.exe"); // Kill explorer
      Robot robot = new Robot();
      int i = 0;
      while (running) {
         sleep(30L);
         focus();
         releaseKeys(robot);
         sleep(15L);
         focus();
         if (i++ % 10 == 0) {
             kill("taskmgr.exe");
         }
         focus();
         releaseKeys(robot);
      }
      Runtime.getRuntime().exec("explorer.exe"); // Restart explorer
    } catch (Exception e) {

    }
  }

  private void releaseKeys(Robot robot) {
    robot.keyRelease(17);
    robot.keyRelease(18);
    robot.keyRelease(127);
    robot.keyRelease(524);
    robot.keyRelease(9);
  }

  private void sleep(long millis) {
    try {
      Thread.sleep(millis);
    } catch (Exception e) {

    }
  }

  private void kill(String string) {
    try {
      Runtime.getRuntime().exec("taskkill /F /IM " + string).waitFor();
    } catch (Exception e) {
    }
  }

  private void focus() {
    this.frame.grabFocus();
    this.frame.requestFocus();
  }
}

Background of this code: I wrote some kind of fake terminal in which I could let appear everything what I wanted. Eg, the stupid Windows messages like: "Keyboard not found, press any key to continue". I used it as a joke on school. Fire up this app and leave the room, it would log out the Windows account automatically after one minute. But I hadn't thought that there would be a teacher that used Ctrl-Alt-Del to kill my application. He left a message in my personal documents in which he wrote what he did and ended his message with "I've beaten you, haha I'm evil". So, I wanted to go in competition with him. And then wrote this code. He tried more than five times in school to escape from the app, but he couldn't (I can't as well :D). Of course he could turn off the machine or log out. But the target was to access my personal documents.

Share:
17,046
Samuele Mattiuzzo
Author by

Samuele Mattiuzzo

Bristol, UK Coder @ Simpleweb LTD Call me Sam.

Updated on June 04, 2022

Comments

  • Samuele Mattiuzzo
    Samuele Mattiuzzo almost 2 years

    Basically i just need to create an application (with sort of user access) which first screen is a fullscreen window that cannot be minimized or closed without entering valid username and password. Something like windows screensaver. Can it be done? What libraries should i look into? This is all i need, if my question is incomplete or unclear, feel free to ask!

  • Samuele Mattiuzzo
    Samuele Mattiuzzo over 12 years
    yeah, but since i want to do a portable application, i think there must be a way to spawn a window fullscreen. By doing some other searches, i found this: download.oracle.com/javase/tutorial/extra/fullscreen/index.h‌​tml
  • Tim Jansen
    Tim Jansen over 12 years
    I think that will only work if your "not minimizing or closing" requirement does not include key combinations like alt-tab or ctrl-alt-del.
  • Samuele Mattiuzzo
    Samuele Mattiuzzo over 12 years
    sorry i almost forgot to accept the answer! and kudos for your competition :P
  • Martijn Courteaux
    Martijn Courteaux over 12 years
    Strange, that you were able to accept my answer. Did you use stop() after a Thread.sleep()`? Or did you restart your machine :D ?
  • Samuele Mattiuzzo
    Samuele Mattiuzzo over 12 years
    i restarted :P i accepted your answer since it's a good basis to start from :)
  • Dreamspace President
    Dreamspace President over 3 years
    Good call on killing explorer.exe but I've just played around with regularly (100 ms) killing Taskmgr.exe which is a must. Works if the command prompt is elevated (aka admin) from which you start java -jar myjar.jar! Else CTRL+ALT+DEL or just CTRL+SHIFT+ESC can call the Task Manager, and you can use it to start explorer.exe again. Btw., a WindowFocusListener calling requestFocus and toFront also helps messing with the TaskMgr, but it's not enough to entirely prevent its use (RMB still works).