Last active
June 1, 2021 16:25
-
-
Save avanderw/6dd2a12f7615dc84104f7f57d6e65d50 to your computer and use it in GitHub Desktop.
Moves the mouse every 10 minutes to a random location on the screen. Great for keeping the screen active when you are not active.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.avdw.hangman; | |
import org.jnativehook.GlobalScreen; | |
import org.jnativehook.NativeHookException; | |
import org.jnativehook.keyboard.NativeKeyEvent; | |
import org.jnativehook.keyboard.NativeKeyListener; | |
import java.awt.*; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
import java.util.logging.Level; | |
import java.util.logging.LogManager; | |
import java.util.logging.Logger; | |
public class MouseMover implements Runnable, NativeKeyListener { | |
private static boolean shortcutRegistered = false; | |
private static Thread thread = null; | |
private boolean active; | |
public static void main(String[] args) { | |
MouseMover mouseMover = new MouseMover(); | |
registerGlobalShortcut(mouseMover); | |
mouseMover.start(); | |
} | |
private static void registerGlobalShortcut(MouseMover mouseMover) { | |
if (shortcutRegistered) { | |
System.out.println("Shortcut already registered"); | |
return; | |
} | |
LogManager.getLogManager().reset(); | |
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName()); | |
logger.setLevel(Level.OFF); | |
try { | |
GlobalScreen.registerNativeHook(); | |
} catch (NativeHookException ex) { | |
System.err.println("There was a problem registering the native hook."); | |
System.err.println(ex.getMessage()); | |
} | |
GlobalScreen.addNativeKeyListener(mouseMover); | |
shortcutRegistered = true; | |
System.out.println("Registered native hook, adding key listener"); | |
} | |
private void deregeisterGlobalShortcut() { | |
signalStop(); | |
thread.interrupt(); | |
GlobalScreen.removeNativeKeyListener(this); | |
try { | |
GlobalScreen.unregisterNativeHook(); | |
} catch (NativeHookException e) { | |
System.err.println("There was a problem unregistering the native hook."); | |
System.err.println(e.getMessage()); | |
} | |
System.out.println("Deregistered native hook, removed key listener"); | |
} | |
private void moveMouse() { | |
sendNotification("Started"); | |
active = true; | |
int refresh = 10_000; | |
Random random = new Random(); | |
while (active) { | |
try { | |
// These coordinates are screen coordinates | |
int xCoord = random.nextInt((int) Toolkit.getDefaultToolkit().getScreenSize().getWidth()); | |
int yCoord = random.nextInt((int) Toolkit.getDefaultToolkit().getScreenSize().getHeight()); | |
// Move the cursor | |
Robot robot = new Robot(); | |
robot.mouseMove(xCoord, yCoord); | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} | |
int remaining = 600_000; | |
while (remaining > 0 && active) { | |
System.out.printf("Moving in %2d min %02d sec...%n", | |
TimeUnit.MILLISECONDS.toMinutes(remaining), | |
TimeUnit.MILLISECONDS.toSeconds(remaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(remaining))); | |
try { | |
Thread.sleep(refresh); // convert to use scheduled executor instead (better performance on CPU) | |
} catch (InterruptedException e) { | |
// ignore | |
} | |
remaining -= refresh; | |
} | |
} | |
System.out.println("Killing MouseMover thread"); | |
} | |
@Override | |
public void nativeKeyPressed(final NativeKeyEvent nativeKeyEvent) { | |
// ignore | |
} | |
@Override | |
public void nativeKeyReleased(final NativeKeyEvent nativeKeyEvent) { | |
boolean isShiftPressed = (nativeKeyEvent.getModifiers() & NativeKeyEvent.SHIFT_MASK) != 0; | |
boolean isCtrlPressed = (nativeKeyEvent.getModifiers() & NativeKeyEvent.CTRL_MASK) != 0; | |
if (nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_M && isCtrlPressed && isShiftPressed) { | |
sendNotification("Killed"); | |
deregeisterGlobalShortcut(); | |
} else if (nativeKeyEvent.getKeyCode() == NativeKeyEvent.VC_M && isCtrlPressed) { | |
if (active) { | |
System.out.println("Stopping MouseMover thread"); | |
signalStop(); | |
thread.interrupt(); | |
sendNotification("Paused"); | |
} else { | |
System.out.println("Starting new MouseMover thread"); | |
start(); | |
sendNotification("Resumed"); | |
} | |
} | |
} | |
private void sendNotification(final String message) { | |
SystemTray systemTray = SystemTray.getSystemTray(); | |
Image image = Toolkit.getDefaultToolkit().createImage("some-icon.png"); | |
TrayIcon trayIcon = new TrayIcon(image); | |
trayIcon.setImageAutoSize(true); | |
try { | |
systemTray.add(trayIcon); | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} | |
trayIcon.displayMessage("Mouse Mover", message, TrayIcon.MessageType.INFO); | |
new Thread(() -> { | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException e) { | |
// ignore | |
} | |
systemTray.remove(trayIcon); | |
}).start(); | |
} | |
@Override | |
public void nativeKeyTyped(final NativeKeyEvent nativeKeyEvent) { | |
// ignore | |
} | |
@Override | |
public void run() { | |
moveMouse(); | |
} | |
public synchronized void signalStop() { | |
active = false; | |
} | |
private void start() { | |
thread = new Thread(this); | |
thread.setName("MouseMover"); | |
thread.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment