Skip to content

Commit

Permalink
added mouse support
Browse files Browse the repository at this point in the history
  • Loading branch information
claypigeon123 committed Feb 13, 2022
1 parent eee3306 commit 6c0b0f4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.cp.tools</groupId>
<artifactId>anti-afk</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.3-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/cp/tools/antiafk/config/Configurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Configuration {
Expand All @@ -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;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/cp/tools/antiafk/config/model/MouseButton.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/cp/tools/antiafk/logic/AntiAfkProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand All @@ -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);

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/cp/tools/antiafk/util/SystemInputProcessor.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 6c0b0f4

Please sign in to comment.