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

双击执行即可。

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...