Java anti lock screen applet code example

To prevent the system desktop from automatically locking the screen, just type a jar package and write a batch program start Bat, double-click execute to keep the DOS window executing without other effects.

The program is designed to move the mouse every 30 seconds, which can be adjusted as needed.

Attached code:

package main;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.Toolkit;

public class Main {
  public static void main(String[] args) {
    Robot robot = null;
    try {
      robot = new Robot();
    } catch (AWTException e1) {
      e1.printStackTrace();
    }
    Point pos = MouseInfo.getPointerInfo().getLocation();

    int last_x = pos.x;
    int last_y = pos.y;

    int mov = 1;

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    System.out.println("Screen size: " + screenSize.getWidth() + "*" + screenSize.getHeight());
    while (true) {
      System.out.println(pos.x + " " + pos.y);
      PointerInfo pos_info = MouseInfo.getPointerInfo();
      if (pos_info == null) {
        System.out.println("Get location fail!");
        try {
          Thread.sleep(30000L);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

      } else {
        pos = pos_info.getLocation();

        if ((pos.x == last_x) && (pos.y == last_y)) {
          System.out.println("moving!");

          if (pos.y <= 0) {
            mov = 1;
          }
          if (pos.y > 0) {
            mov = -1;
          }
          robot.mouseMove(pos.x,pos.y + mov);

          robot.mouseMove(pos.x,pos.y);
        }
        pos_info = MouseInfo.getPointerInfo();
        if (pos_info == null) {
          System.out.println("Get location fail!");
          try {
            Thread.sleep(30000L);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }

        } else {
          pos = pos_info.getLocation();

          last_x = pos.x;
          last_y = pos.y;
          try {
            Thread.sleep(30000L);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
}

Type the main class into a jar package, where the jar package name is MouseMove jar; Write a file in the same directory as the jar package Bat type file, the file contents are as follows:

@echo off
java -jar MouseMove.jar

Double click to execute.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>