Java机器人点击是每秒70次点击无法弄清楚数学/出了什么问题

时间:2017-01-24 20:53:12

标签: 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值之间的随机数,来自差异类),然后从中点击然后再睡眠线程为1 / randomNum(以秒为单位),以便每秒多次点击。

出于某种原因,它以200cps的速度点击并将计算机拉出来。有没有人看到我的代码有任何问题?

感谢。

点击新代码;

    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变量发生变化时不会被调用。

1 个答案:

答案 0 :(得分:0)

如果您打算经常点击它,那么它会创建大量线程,所有线程都会点击

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()并在某处调用它,例如在构造函数中。