您当前的位置: 首页 >  Java

石头wang

暂无认证

  • 11浏览

    0关注

    295博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

防止电脑睡眠的Java程序

石头wang 发布时间:2022-10-17 09:14:04 ,浏览量:11

背景

如果你可以通过电脑电源管理能设置不睡眠则可以,但是有些电脑可能因为权限无法设置不睡眠,有什么办法?

比如可以启动一个java程序模拟鼠标隔一段时间就动一下

代码

如下代码按照自己的需要调整间隔,下面代码是delay 5000毫秒,即5秒动一下。注意不要设置过于密集,对于cpu会消耗,比如如果你的电脑10分钟不动就会睡眠,其实取一半时间5分钟都没问题。

设置得太频繁会导致你想操作鼠标点击停止按钮的时候会导致鼠标不断被移开,你点都点不了!!

package org.example.test;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;

public class TestMain {

    public static void main(String[] args) throws AWTException {
        Robot robot = new Robot();
        Random random = new Random();
        int x = 0;
        int y = 0;
        while(true) {
            try {
                robot.delay(5000);
                x = Math.abs(random.nextInt(100)) % 100 + 50;
                y = Math.abs(random.nextInt(100)) % 100 + 50;
                change(0, x, y);
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }

    public static void change(int type, int x, int y){
        Point p = MouseInfo.getPointerInfo().getLocation();
        int width = (int) p.getX() + x;
        int heigh = (int) p.getY() + y;
        if(type == 0) {
            width = x;
            heigh = y;
        }
        Robot robot;
        try {
            robot = new Robot();
            robot.mouseMove(width,heigh);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

}
关注
打赏
1663722529
查看更多评论
立即登录/注册

微信扫码登录

0.0378s