I am trying to make it so after I press the start button, it does the clicks when left mouse button is being held down, but right now its not clicking at all when I hold left mouse button.
package GUI;
import Engine.AutoClicker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class EventHandler implements ActionListener {
private UI ui;
private AutoClicker autoClicker;
public EventHandler(UI ui, AutoClicker autoClicker) {
this.ui = ui;
this.autoClicker = autoClicker;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource().equals(ui.getStartButton())) {
try {
int minCPS = Integer.parseInt(ui.getMinCPSFieldText());
int maxCPS = Integer.parseInt(ui.getMaxCPSFieldText());
autoClicker.setMinCPS(minCPS);
autoClicker.setMaxCPS(maxCPS);
} catch (NumberFormatException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(ui, "Please enter valid integers for CPS values.");
return;
}
autoClicker.startClicking();
System.out.println("start");
} else if (actionEvent.getSource().equals(ui.getStopButton())) {
autoClicker.stopClicking();
System.out.println("stop");
}
}
}
package Engine;
import javax.swing.*;
import GUI.UI;
public class Engine {
private Thread UIthread, clickerThread;
private UI ui;
private AutoClicker autoClicker;
public Engine(int width, int height) {
autoClicker = new AutoClicker();
ui = new UI(width, height, autoClicker);
ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ui.setVisible(true);
// UI class already adds the AutoClicker as a MouseListener
UIthread = new Thread(ui);
UIthread.start();
clickerThread = new Thread(autoClicker);
clickerThread.start();
}
public static void main(String[] args) {
new Engine(500, 300);
}
}
package GUI;
import javax.swing.*;
import Engine.AutoClicker;
public class UI extends JFrame implements Runnable {
public int width, height;
private JButton start, stop;
private JTextField minCPSField, maxCPSField;
private AutoClicker autoClicker;
public UI(int width, int height, AutoClicker autoClicker) {
this.width = width;
this.height = height;
this.autoClicker = autoClicker;
this.setLayout(null);
this.setSize(width, height);
// Add AutoClicker as a MouseListener
this.addMouseListener(autoClicker);
}
public void init() {
start = new JButton("Start");
start.setBounds(100, 100, 100, 30);
add(start);
stop = new JButton("Stop");
stop.setBounds(320, 100, 100, 30);
add(stop);
JLabel minCPSLabel = new JLabel("Min CPS:");
minCPSLabel.setBounds(30, 50, 100, 30);
add(minCPSLabel);
minCPSField = new JTextField(String.valueOf(autoClicker.getMinCPS()));
minCPSField.setBounds(100, 50, 100, 30);
add(minCPSField);
JLabel maxCPSLabel = new JLabel("Max CPS:");
maxCPSLabel.setBounds(250, 50, 100, 30);
add(maxCPSLabel);
maxCPSField = new JTextField(String.valueOf(autoClicker.getMaxCPS()));
maxCPSField.setBounds(320, 50, 100, 30);
add(maxCPSField);
EventHandler eHandler = new EventHandler(this, autoClicker);
start.addActionListener(eHandler);
stop.addActionListener(eHandler);
minCPSField.addActionListener(eHandler);
maxCPSField.addActionListener(eHandler);
}
@Override
public void run() {
init();
}
public String getMinCPSFieldText() {
return minCPSField.getText();
}
public String getMaxCPSFieldText() {
return maxCPSField.getText();
}
public JButton getStartButton() {
return start;
}
public JButton getStopButton() {
return stop;
}
}
package Engine;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
public class AutoClicker extends MouseAdapter implements Runnable {
private Robot robot;
private boolean running = false;
private boolean leftMouseButtonPressed = false; // Add this flag
private int minCPS = 10;
private int maxCPS = 20;
private Random random = new Random();
public AutoClicker() {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
if (running && leftMouseButtonPressed) {
clickMouse();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void clickMouse() {
try {
int delay = getRandomCPS();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(delay);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(delay);
} catch (Exception e) {
e.printStackTrace();
}
}
private int getRandomCPS() {
return 1000 / (minCPS + random.nextInt(maxCPS - minCPS + 1));
}
public void startClicking() {
running = true;
}
public void stopClicking() {
running = false;
}
public void setMinCPS(int minCPS) {
this.minCPS = minCPS;
}
public void setMaxCPS(int maxCPS) {
this.maxCPS = maxCPS;
}
public int getMinCPS() {
return minCPS;
}
public int getMaxCPS() {
return maxCPS;
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
leftMouseButtonPressed = true;
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
leftMouseButtonPressed = false;
}
}
}
I tried adding the things in the AutoClicker like MouseEvent but I am not familiar with this stuff and had no luck, I am failing to change the value of leftMouseButtonPressed = true by holding down left click.
user25629679 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.