How to write an auto clicker in Java
After my kid asked me to download and install an auto-clicker, I saw this as a teachable moment to build one in Java. I could talk about the different concepts Java offers and also explore a new API in the Java API.
Source code:
package org.ea;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class AutoClicker implements KeyListener {
static boolean enabled = false;
public static void main(String[] args) {
AutoClicker ac = new AutoClicker();
try {
JFrame f=new JFrame();
f.addKeyListener(ac);
f.setSize(100, 100);
f.setVisible(true);
Robot r = new Robot();
int button = InputEvent.BUTTON1_DOWN_MASK;
while (true) {
if(enabled) {
System.out.println("Click");
r.mousePress(button);
Thread.sleep(400);
r.mouseRelease(button);
}
Thread.sleep( 2000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_A) {
enabled = !enabled;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}