Java防锁屏小程序代码实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java防锁屏小程序代码实例

  2021-04-02 我要评论

为防止系统桌面自动锁屏,只需打成jar包,写个批处理程序start.bat,双击执行保持dos窗口执行即可,无其他影响。

程序设计为每30秒动一次鼠标,可根据需要调整。

附代码:

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();
          }
        }
      }
    }
  }
}

将这个Main类打成jar包,此处jar包名为MouseMove.jar;与jar包同目录位置写个.bat类型文件,文件内容如下:

@echo off
java -jar MouseMove.jar

双击执行即可。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们