robot.mousepress“Auto Clicker”如何让它停止?

时间:2013-12-16 16:13:10

标签: java eclipse

我找到了制作自动点击器以供娱乐使用的代码。

我会用什么代码让点击器停止?我希望使用类似A Key的监听器来阻止它。喜欢按空格键并输入以使其开始和停止。

以下是代码:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class AutoClicker {

public static int rate = 0;

public static void main (String[] args) {

    while (rate == 0){
        try{
            System.out.println("Speed of the auto-clicker (in miliseconds):");
             BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
             try {
                rate = Integer.parseInt(in.readLine());
                if (rate < 500) {
                    rate = 0;
                    System.out.println("Must be at least 500 miliseconds.");
             }
        } catch (NumberFormatException ex) {
            System.out.println("Error - please try again.");
        }
    } catch (IOException e) {}

}
try {
    Robot robot = new Robot();
    while (true) {
        try {
            Thread.sleep(rate);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            } catch (InterruptedException ex) {}
        } 
    }catch (AWTException e) {}
}

}

1 个答案:

答案 0 :(得分:5)

而不是

While(true)

使用

While(//keyevent here// != true)

因此,在您按下指定的按钮之前,它将继续循环,一旦keyevent等于true,自动闪光灯将停止。