java - Java Robot单击的速度为每秒70次单击,无法弄清楚数学/出了什么问题

标签 java multithreading

 volatile private boolean mouseDown = false;

private int max = 0;
private int min = 0;
private Robot robot;

public MouseClickListener()
{
    try
    {
        robot = new Robot();
    } catch (AWTException e)
    {
        e.printStackTrace();
    }
}

@Override
public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent)
{

}

@Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent)
{
    if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
    {
        max = Native.get().getFrame().getCps().getValue() + Native.get().getFrame().getDev().getValue();
        min = Native.get().getFrame().getCps().getValue() - Native.get().getFrame().getDev().getValue();
        mouseDown = true;
        initThread();
    }
}

@Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent)
{
    if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
    {
        mouseDown = false;
    }
}

嗨,我基本上是在尝试制作一个自动点击器,该点击器单击JSlider的值(例如,介于9和13之间的随机值)。

因此,在单击鼠标时,将调用initThread方法,并计算出每秒的点击次数(介于JSlider值之间的随机数,该值是来自差异类),然后从中单击,然后将线程 hibernate 1/randomNum(以秒为单位),以便每秒单击多次。

由于某种原因,它的点击速度约为200cps,使我的计算机落后。有人看到我的代码有问题吗?

谢谢。

CLICKER的新代码;
    public class ClickMethod implements Runnable
{
    @Override
    public void run()
    {
        System.out.println("STARTED");
        do
        {
            System.out.println("RUNNING");
            Random r = new Random();
            int random = r.nextInt((max - min) + 1) + min;
            robot.mousePress(BUTTON1_MASK);
            robot.mouseRelease(BUTTON1_MASK);
            try
            {
                Thread.sleep(1000 / random);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        } while (mouseDown);
    }
}

由于某种原因,此命令仅运行一次,然后在mouseDown变量更改时不会被调用。

最佳答案

如果希望您经常单击此按钮,那么它将创建许多线程,所有这些线程都可以单击

public void nativeMousePressed(NativeMouseEvent nativeMouseEvent)
{
    if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
    {
        max = Native.get().getFrame().getCps().getValue() + Native.get().getFrame().getDev().getValue();
        min = Native.get().getFrame().getCps().getValue() - Native.get().getFrame().getDev().getValue();
        mouseDown = true;
        initThread();
    }
}

您需要从那里删除initThread()并在某个地方(例如在构造函数中)调用一次。

关于java - Java Robot单击的速度为每秒70次单击,无法弄清楚数学/出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838790/

相关文章:

ruby - 具有强读取偏差的快速线程安全 Ruby 哈希

java - Jenkins 如何发现构建后插件的 config.jelly?

java - 为什么这个方法不是递归的?

java - 从 DB 检索图像到 imageView

java - 为什么将我的代码拆分到不同的类时会出现堆栈溢出错误?

Python线程

spring - spring executor中java executor framework的invokeAll等价方法

java - java中的多个并行线程不会终止

.net - Akka.Net 和缓存一致性

java - 从数据库中获取数据并将其显示在android应用程序上