我制作了一个AutoClicker,它可以正常工作,但是我想添加一个KeyListener,以便使用它的人可以停止该autoclicker,它应该与按钮q配合使用,但是不能! 这是代码: Main.java:
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.util.*;
public class Main{
private static Robot roboter = null;
public static void main(String[]args){
JFrame jf = new JFrame();
jf.setSize(1920, 1080);
jf.addKeyListener(new KeyHandler());
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.requestFocus();
Scanner SekAbstand = new Scanner(System.in);
int SekundenAbstand;
int EndlosSchleife = 1;
System.out.println("Um das Programm zu stoppen druecke Q!!!");
System.out.println("Wie lange soll der Abstand zwischen den Klicks
sein(in Millisekunden)?");
SekundenAbstand = SekAbstand.nextInt();
System.out.println("Du hast 10 Sekunden um die Maus zur richtigen
Position zu bringen!");
try {
roboter = new Robot();
} catch(AWTException e) {
e.printStackTrace();
}
roboter.delay(10000);
while(EndlosSchleife == 1) {
roboter.delay(SekundenAbstand);
roboter.mousePress(MouseEvent.BUTTON1_MASK);
roboter.mouseRelease(MouseEvent.BUTTON1_MASK);
}
}
}
KeyHandler.java:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_Q){
System.exit(0);
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
谢谢。