场景
桌面管理软件会在10分钟内无操作时,自动锁屏,但由于某些特殊情况,需要防止锁屏。此时可以写一个工具,定时按下SCROLL LOCK
键,此方式适用于台式机。
实现方式
代码非常简单,直接上代码
Java
import java.awt.*;
import java.awt.event.KeyEvent;
public class NotLockRunner implements Runnable {
private final Robot robot;
private final int duration;
public NotLockRunner(int duration) throws AWTException {
this(new Robot(), duration);
}
public NotLockRunner(final Robot robot, int duration) {
this.robot = robot;
this.duration = duration;
System.out.printf("[防锁屏线程] 初始化完成,时间间隔为 %d 秒%n", duration);
}
@Override
public void run() {
while (true) {
try {
if (robot == null) break;
Thread.sleep(duration * 1000L);
robot.keyPress(KeyEvent.VK_SCROLL_LOCK);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_SCROLL_LOCK);
System.out.println("[防锁屏线程] 机器人按下 VK_SCROLL_LOCK 键以防止锁屏...");
} catch (Exception e) {
System.err.println("[防锁屏线程] 模拟按键出错:"+e.getMessage());
}
}
}
public static void main(String[] args) throws AWTException {
new Thread(new NotLockRunner(9*60)).start();
}
}
Node.js
Node 本身没有相关的功能,可以用 robot.js、robot-cmd 、rubick-native等第三方库