Java robots.mousepress“自动点击器”如何让它停止?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20615451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:09:23  来源:igfitidea点击:

robot.mousepress "Auto Clicker" How to make it stop?

javaeclipse

提问by Aaron

I have found code to make an Auto-Clicker for recreational use.

我找到了制作用于娱乐用途的自动点击器的代码。

What code would I use to make the clicker stop? I was hoping to use something like A Key listener to make it stop. Like by pressing space and enter to make it start and stop.

我将使用什么代码来使答题器停止?我希望使用像 A Key listener 这样的东西来让它停止。就像按空格并输入以使其开始和停止一样。

Here is the code :

这是代码:

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) {}
}

}

采纳答案by Mitch Zinck

Instead of

代替

While(true)

Use

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

So until you press the button specified by you, it will continue looping, once the keyevent equals true the autoclicker will stop.

因此,直到您按下您指定的按钮,它才会继续循环,一旦 keyevent 等于 true,自动点击器将停止。