From 6c0b0f45371de235c62a30d88a2e16f92acd69b9 Mon Sep 17 00:00:00 2001 From: claypigeon123 Date: Sun, 13 Feb 2022 21:44:30 +0100 Subject: [PATCH] added mouse support --- pom.xml | 2 +- .../cp/tools/antiafk/config/Configurer.java | 16 ++++++--- .../antiafk/config/model/Configuration.java | 17 +++++++++- .../antiafk/config/model/MouseButton.java | 19 +++++++++++ .../tools/antiafk/logic/AntiAfkProcess.java | 15 ++++++++- .../antiafk/util/SystemInputProcessor.java | 33 +++++++++++++++++++ 6 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/cp/tools/antiafk/config/model/MouseButton.java diff --git a/pom.xml b/pom.xml index 17a4355..4e560db 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.cp.tools anti-afk - 1.0.2-SNAPSHOT + 1.0.3-SNAPSHOT UTF-8 diff --git a/src/main/java/com/cp/tools/antiafk/config/Configurer.java b/src/main/java/com/cp/tools/antiafk/config/Configurer.java index 1b4966e..c8e5227 100644 --- a/src/main/java/com/cp/tools/antiafk/config/Configurer.java +++ b/src/main/java/com/cp/tools/antiafk/config/Configurer.java @@ -2,6 +2,7 @@ import com.cp.tools.antiafk.config.model.Configuration; import com.cp.tools.antiafk.config.model.KeyboardButton; +import com.cp.tools.antiafk.config.model.MouseButton; import com.cp.tools.antiafk.util.SystemInputProcessor; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; @@ -31,13 +32,20 @@ private static Configuration setupConfig() throws JAXBException { int minTime = processor.nextPositiveInt("Enter the MINIMUM amount of time to wait between executions (in seconds): "); int maxTime = processor.nextPositiveIntLargerThanOrEqualTo("Enter the MAXIMUM amount of time to wait between executions (in seconds): ", minTime); - KeyboardButton key = processor.nextKeyboardButton("Type out the key to press (valid options are SPACE, BACKSPACE, ENTER, I, U): "); - processor.close(); + boolean useMouse = processor.nextYesNoDecision("Use mouse instead of keyboard (yes/no): "); - Configuration config = new Configuration(minTime, maxTime, key); - writeConfig(config); + Configuration config; + if (useMouse) { + MouseButton key = processor.nextMouseButton("Type out the mouse button to press (valid options are RMB, LMB, MMB): "); + config = new Configuration(minTime, maxTime, key); + } else { + KeyboardButton key = processor.nextKeyboardButton("Type out the key to press (valid options are SPACE, BACKSPACE, ENTER, I, U): "); + config = new Configuration(minTime, maxTime, key); + } + processor.close(); + writeConfig(config); return config; } diff --git a/src/main/java/com/cp/tools/antiafk/config/model/Configuration.java b/src/main/java/com/cp/tools/antiafk/config/model/Configuration.java index 90ed164..bb15f88 100644 --- a/src/main/java/com/cp/tools/antiafk/config/model/Configuration.java +++ b/src/main/java/com/cp/tools/antiafk/config/model/Configuration.java @@ -7,7 +7,6 @@ @Data @NoArgsConstructor -@AllArgsConstructor @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Configuration { @@ -17,4 +16,20 @@ public class Configuration { private long maximumTimeBetweenExecutions; @XmlElement(name = "key-to-press") private KeyboardButton keyToPress; + @XmlElement(name = "mouse-button-to-press") + private MouseButton mouseButtonToPress; + + public Configuration(long minimumTimeBetweenExecutions, long maximumTimeBetweenExecutions, KeyboardButton keyToPress) { + this.minimumTimeBetweenExecutions = minimumTimeBetweenExecutions; + this.maximumTimeBetweenExecutions = maximumTimeBetweenExecutions; + this.keyToPress = keyToPress; + this.mouseButtonToPress = null; + } + + public Configuration(long minimumTimeBetweenExecutions, long maximumTimeBetweenExecutions, MouseButton mouseButtonToPress) { + this.minimumTimeBetweenExecutions = minimumTimeBetweenExecutions; + this.maximumTimeBetweenExecutions = maximumTimeBetweenExecutions; + this.keyToPress = null; + this.mouseButtonToPress = mouseButtonToPress; + } } diff --git a/src/main/java/com/cp/tools/antiafk/config/model/MouseButton.java b/src/main/java/com/cp/tools/antiafk/config/model/MouseButton.java new file mode 100644 index 0000000..4199a74 --- /dev/null +++ b/src/main/java/com/cp/tools/antiafk/config/model/MouseButton.java @@ -0,0 +1,19 @@ +package com.cp.tools.antiafk.config.model; + +import java.awt.event.MouseEvent; + +public enum MouseButton { + LMB(MouseEvent.BUTTON1), + RMB(MouseEvent.BUTTON2), + MMB(MouseEvent.BUTTON3); + + private final int keyCode; + + MouseButton(int keyCode) { + this.keyCode = keyCode; + } + + public int getKeyCode() { + return keyCode; + } +} diff --git a/src/main/java/com/cp/tools/antiafk/logic/AntiAfkProcess.java b/src/main/java/com/cp/tools/antiafk/logic/AntiAfkProcess.java index cdce661..670d6e2 100644 --- a/src/main/java/com/cp/tools/antiafk/logic/AntiAfkProcess.java +++ b/src/main/java/com/cp/tools/antiafk/logic/AntiAfkProcess.java @@ -3,12 +3,14 @@ import com.cp.tools.antiafk.config.Configurer; import com.cp.tools.antiafk.config.model.Configuration; import com.cp.tools.antiafk.config.model.KeyboardButton; +import com.cp.tools.antiafk.config.model.MouseButton; import com.github.javafaker.Faker; import jakarta.xml.bind.JAXBException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; +import java.awt.event.InputEvent; public class AntiAfkProcess { private static final Logger log = LoggerFactory.getLogger(AntiAfkProcess.class); @@ -26,7 +28,8 @@ public AntiAfkProcess() throws JAXBException, AWTException { private void configure() throws JAXBException { this.config = Configurer.configure(); - log.info("Configuration loaded"); + boolean isMouse = config.getMouseButtonToPress() != null; + log.info("Configuration loaded - using {}", isMouse ? "MOUSE" : "KEYBOARD"); } public void start() { @@ -49,6 +52,16 @@ private void sleep() { } private void press() { + if (config.getMouseButtonToPress() != null) { + MouseButton mb = config.getMouseButtonToPress(); + log.info("Pressing [{}] mouse button now", mb); + + robot.mousePress(InputEvent.getMaskForButton(mb.getKeyCode())); + robot.delay(faker.number().numberBetween(10, 101)); + robot.mouseRelease(InputEvent.getMaskForButton(mb.getKeyCode())); + return; + } + KeyboardButton key = config.getKeyToPress(); log.info("Pressing [{}] key now", key); diff --git a/src/main/java/com/cp/tools/antiafk/util/SystemInputProcessor.java b/src/main/java/com/cp/tools/antiafk/util/SystemInputProcessor.java index cf8f563..e5988b0 100644 --- a/src/main/java/com/cp/tools/antiafk/util/SystemInputProcessor.java +++ b/src/main/java/com/cp/tools/antiafk/util/SystemInputProcessor.java @@ -1,6 +1,7 @@ package com.cp.tools.antiafk.util; import com.cp.tools.antiafk.config.model.KeyboardButton; +import com.cp.tools.antiafk.config.model.MouseButton; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,6 +80,38 @@ public KeyboardButton nextKeyboardButton(String message) { } } + public MouseButton nextMouseButton(String message) { + while (true) { + String input = nextString(message); + + MouseButton btn; + try { + btn = MouseButton.valueOf(input.toUpperCase()); + } catch (IllegalArgumentException iae) { + log.error("Not a valid option. Try again."); + continue; + } + + return btn; + } + } + + public boolean nextYesNoDecision(String message) { + while (true) { + String input = nextString(message); + + switch (input.toLowerCase()) { + case "yes": + return true; + case "no": + return false; + default: + log.error("Not a valid option. Try again."); + break; + } + } + } + public void close() { scan.close(); }