This commit is contained in:
Bea 2024-08-05 18:48:35 +02:00
parent acd4d28490
commit e4d429a88f
7 changed files with 412 additions and 0 deletions

35
.gitignore vendored Normal file

@ -0,0 +1,35 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

64
pom.xml Normal file

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wtf.beatrice</groupId>
<artifactId>autosqueal</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>com.github.kwhat</groupId>
<artifactId>jnativehook</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>wtf.beatrice.autosqueal.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,51 @@
package wtf.beatrice.autosqueal;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.*;
import java.util.TimerTask;
public class CursorMoveListener extends TimerTask {
private int oldX, oldY;
private int newX, newY;
private int loops;
private boolean isUserAway;
private static final Logger LOGGER = LogManager.getLogger(CursorMoveListener.class);
public CursorMoveListener() {
oldX = 0;
oldY = 0;
loops = 0;
}
@Override
public void run() {
newX = MouseInfo.getPointerInfo().getLocation().x;
newY = MouseInfo.getPointerInfo().getLocation().y;
if(newX != oldX || newY != oldY) {
// cursor has been moved
loops = 0;
LOGGER.info("User is no longer away!");
} else {
if (loops < 30) {
loops++;
} else {
LOGGER.info("User is away!");
}
}
isUserAway = loops >= 30;
oldX = newX;
oldY = newY;
}
public boolean isUserAway() {
return isUserAway;
}
}

@ -0,0 +1,94 @@
package wtf.beatrice.autosqueal;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.*;
import java.util.TimerTask;
public class CursorMover extends TimerTask {
private static final Logger LOGGER = LogManager.getLogger(CursorMover.class);
final int destX, destY;
final Robot robot;
float currentX, currentY;
float stepX = 1, stepY = 1;
boolean isRunning = true;
public CursorMover(int destinationX, int destinationY) {
currentX = MouseInfo.getPointerInfo().getLocation().x;
currentY = MouseInfo.getPointerInfo().getLocation().y;
destX = destinationX;
destY = destinationY;
int lengthX, lengthY;
lengthX = Math.round(Math.abs(currentX - destX));
lengthY = Math.round(Math.abs(currentY - destY));
if(lengthX > lengthY) {
stepX = lengthX / (float) lengthY;
}
if(lengthY > lengthX) {
stepY = lengthY / (float) lengthX;
}
LOGGER.info("Dest: [{}, {}], Curr: [{}, {}]", destX, destY, currentX, currentY);
LOGGER.info("Len: [{}, {}]", lengthX, lengthY);
LOGGER.info("Step: [{}, {}]", stepX, stepY);
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException(e);
}
}
@Override
public void run() {
if(Math.abs(currentX - destX) < 1) {
stepX = currentX - destX;
}
if(Math.abs(currentY - destY) < 1) {
stepY = currentY - destY;
}
if(destX == Math.round(currentX) || destY == Math.round(currentY)) {
LOGGER.info("Reached destination, stopping mover timer");
LOGGER.info("Dest: [{}, {}], Curr: [{}, {}]", destX, destY, currentX, currentY);
isRunning = false;
this.cancel();
return;
}
if(currentX > destX) {
currentX -= stepX;
}
if(currentY > destY) {
currentY -= stepY;
}
if(currentX < destX) {
currentX += stepX;
}
if(currentY < destY) {
currentY += stepY;
}
robot.mouseMove(Math.round(currentX), Math.round(currentY));
}
public boolean isRunning() {
return isRunning;
}
}

@ -0,0 +1,56 @@
package wtf.beatrice.autosqueal;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
public class KeyPressListener implements NativeKeyListener
{
private static final Logger LOGGER = LogManager.getLogger(KeyPressListener.class);
private final static List<Integer> pressedKeysIds = new ArrayList<>();
public void nativeKeyPressed(NativeKeyEvent e) {
LOGGER.info("Key Pressed: {}", NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException nativeHookException) {
nativeHookException.printStackTrace();
}
}
pressedKeysIds.add(e.getKeyCode());
handlePressedKeys();
}
public void nativeKeyReleased(NativeKeyEvent e) {
LOGGER.info("Key Released: {}", NativeKeyEvent.getKeyText(e.getKeyCode()));
pressedKeysIds.remove((Integer) e.getKeyCode());
handlePressedKeys();
}
private void handlePressedKeys() {
if (pressedKeysIds.contains(NativeKeyEvent.VC_ALT) &&
pressedKeysIds.contains(NativeKeyEvent.VC_CONTROL)) {
StringBuilder keys = new StringBuilder();
pressedKeysIds.forEach(keyCode -> {
keys.append("[").append(NativeKeyEvent.getKeyText(keyCode)).append("]");
});
LOGGER.warn("Received shutdown keystroke: {}", keys);
System.exit(0);
}
}
}

@ -0,0 +1,95 @@
package wtf.beatrice.autosqueal;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.*;
import java.awt.event.InputEvent;
import java.util.Random;
import java.util.Timer;
public class Main {
private static final int LOOPS_BEFORE_CLICK = 5;
private static final int TIME_BETWEEN_MOVES = 5;
private static final Logger LOGGER = LogManager.getLogger(Main.class);
public static void main(String[] args) throws InterruptedException, AWTException {
LOGGER.info("Hello world!");
registerJNativeHook();
Timer timer = new Timer();
CursorMoveListener cursorMoveListener = new CursorMoveListener();
timer.schedule(cursorMoveListener, 0L, 1000L);
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
Random random = new Random();
while (true) {
//if(!cursorMoveListener.isUserAway()) continue;
for(int i = 0; i < LOOPS_BEFORE_CLICK; i++) {
int currentX = MouseInfo.getPointerInfo().getLocation().x;
int currentY = MouseInfo.getPointerInfo().getLocation().y;
LOGGER.info("Starting coordinates: {}, {}", currentX, currentY);
int randomX, randomY;
randomX = random.nextInt(width);
randomY = random.nextInt(height);
LOGGER.info("Destination coordinates: {}, {}", randomX, randomY);
startMover(randomX, randomY);
}
startMover(width - 5, 5);
Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(200);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(500);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(200);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(200);
}
}
private static void registerJNativeHook() {
LOGGER.info("Registering jnativehook library...");
try {
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new KeyPressListener());
LOGGER.info("Successfully registered jnativehook library!");
}
catch (NativeHookException ex) {
LOGGER.error("There was a problem registering the native hook.");
LOGGER.error(ex.getMessage());
System.exit(1);
}
}
private static void startMover(int destX, int destY) throws InterruptedException {
Timer timer = new Timer();
CursorMover cursorMover = new CursorMover(destX, destY);
timer.schedule(cursorMover, 0L, 2L);
while (cursorMover.isRunning()) {
Thread.sleep(200);
}
timer.cancel();
Thread.sleep(TIME_BETWEEN_MOVES * 1000);
}
}

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss.SSS}] [%t] %-4level | %logger{36} - %msg%n" />
</Console>
<File name="FileAppender" fileName="application-${date:yyyyMMdd}.log" immediateFlush="false" append="true">
<PatternLayout pattern="[%d{yyy-MM-dd HH:mm:ss.SSS}] [%t] %-4level | %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>