Java防锁屏小程序

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

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

附代码:

 1 package main;
 2 
 3 import java.awt.AWTException;
 4 import java.awt.Dimension;
 5 import java.awt.MouseInfo;
 6 import java.awt.Point;
 7 import java.awt.PointerInfo;
 8 import java.awt.Robot;
 9 import java.awt.Toolkit;
10 
11 public class Main {
12     public static void main(String[] args) {
13         Robot robot = null;
14         try {
15             robot = new Robot();
16         } catch (AWTException e1) {
17             e1.printStackTrace();
18         }
19         Point pos = MouseInfo.getPointerInfo().getLocation();
20 
21         int last_x = pos.x;
22         int last_y = pos.y;
23 
24         int mov = 1;
25 
26         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
27 
28         System.out.println("Screen size: " + screenSize.getWidth() + "*" + screenSize.getHeight());
29         while (true) {
30             System.out.println(pos.x + " " + pos.y);
31             PointerInfo pos_info = MouseInfo.getPointerInfo();
32             if (pos_info == null) {
33                 System.out.println("Get location fail!");
34                 try {
35                     Thread.sleep(30000L);
36                 } catch (InterruptedException e) {
37                     e.printStackTrace();
38                 }
39 
40             } else {
41                 pos = pos_info.getLocation();
42 
43                 if ((pos.x == last_x) && (pos.y == last_y)) {
44                     System.out.println("moving!");
45 
46                     if (pos.y <= 0) {
47                         mov = 1;
48                     }
49                     if (pos.y > 0) {
50                         mov = -1;
51                     }
52                     robot.mouseMove(pos.x, pos.y + mov);
53 
54                     robot.mouseMove(pos.x, pos.y);
55                 }
56                 pos_info = MouseInfo.getPointerInfo();
57                 if (pos_info == null) {
58                     System.out.println("Get location fail!");
59                     try {
60                         Thread.sleep(30000L);
61                     } catch (InterruptedException e) {
62                         e.printStackTrace();
63                     }
64 
65                 } else {
66                     pos = pos_info.getLocation();
67 
68                     last_x = pos.x;
69                     last_y = pos.y;
70                     try {
71                         Thread.sleep(30000L);
72                     } catch (InterruptedException e) {
73                         e.printStackTrace();
74                     }
75                 }
76             }
77         }
78     }
79 }
View Code

 

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

@echo off
java -jar MouseMove.jar

双击执行即可。

 

posted on 2019-09-24 17:03  IT-风  阅读(1825)  评论(0编辑  收藏  举报

导航