使用 Java 锁定屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6744535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:06:45  来源:igfitidea点击:

Use Java to lock a screen

javalockingfullscreen

提问by Samuele Mattiuzzo

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!

基本上我只需要创建一个应用程序(具有某种用户访问权限),它的第一个屏幕是一个全屏窗口,如果不输入有效的用户名和密码,就不能最小化或关闭它。类似于 Windows 屏幕保护程序的东西。可以做到吗?我应该研究哪些图书馆?这就是我所需要的,如果我的问题不完整或不清楚,请随时提问!

回答by Martijn Courteaux

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

有一次,我用 Java 写了一些东西,你无法逃脱。真的不可能。它适用于 Windows。干得好:

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-Delto 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.

这段代码的背景:我写了一种假终端,我可以在其中显示我想要的一切。例如,愚蠢的 Windows 消息,如:“找不到键盘,按任意键继续”。我用它作为学校的笑话。启动这个应用程序离开房间,它会在一分钟后自动注销Windows帐户。但我没想到,会有老师认为使用Ctrl- Alt-Del杀死我的应用程序。他在我的个人文件中留下了一条信息,写下了他的所作所为,并以“我打败了你,哈哈,我是邪恶的”结束了他的信息。所以,我想和他竞争。然后写了这段代码。他在学校里尝试了五次以上从应用程序中逃脱,但他做不到(我也做不到:D)。当然,他可以关闭机器或注销。但目标是访问我的个人文件。

回答by SJuan76

In any half-decent OS, the OS will be the one in charge of assigning an area or window to each process.

在任何半体面的操作系统中,操作系统将负责为每个进程分配一个区域或窗口。

You will need to check each OS method of doing that, and implement it in native code (probably C or C++) using JNI.

您需要检查每个操作系统的方法,并使用 JNI 在本机代码(可能是 C 或 C++)中实现它。