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

雙擊執行即可。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM