Skip to content

Instantly share code, notes, and snippets.

@mageddo
Created September 30, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageddo/ba7ac24e5ea6e6fd894aac9113b37f34 to your computer and use it in GitHub Desktop.
Save mageddo/ba7ac24e5ea6e6fd894aac9113b37f34 to your computer and use it in GitHub Desktop.
ScreenSaver Prevent
import java.awt.*;
import java.time.Duration;
/**
* Move the mouse by one pixel every 3 minutes to prevent screen saver to start,
* useful when you dont have the right permissions to change that
* https://stackoverflow.com/questions/4231458/moving-the-cursor-in-java
* https://stackoverflow.com/questions/1439022/get-mouse-position
*/
public class ScreenSaverPreventMain {
public static final long MOUSE_MOVE_INTERVAL = Duration
.ofMinutes(3)
.toMillis();
public static void main(String[] args) {
try {
final Robot r = new Robot();
while (true) {
moveMouseOnePixel(r);
Thread.sleep(MOUSE_MOVE_INTERVAL);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void moveMouseOnePixel(Robot robot) {
final Point location = MouseInfo.getPointerInfo()
.getLocation();
final int x = (int) (location.getX() + 1);
final int y = (int) (location.getY() + 1);
robot.mouseMove(x, y);
System.out.printf("status=preventing-screen-saver, action=mouse-moved, y=%d, y=%d%n", x, y);
}
static void moveMouseToTheCenter(Robot robot) throws AWTException {
final int y = (int) Toolkit.getDefaultToolkit()
.getScreenSize()
.getHeight() / 2;
final int x = (int) Toolkit.getDefaultToolkit()
.getScreenSize()
.getWidth() / 2;
robot.mouseMove(x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment