编辑 - Java while循环意味着不调用事件

时间:2017-01-20 14:30:11

标签: java click mouse

所以我在Cruncher的帮助下更新了我的代码,现在点击器看起来效果更好了。然而,当while(按下)循环运行时,没有其他事件被调用&所以它一直在运行。

public class函数实现NativeMouseListener {

private Robot robot;

private boolean pressed = false;

private boolean skip = false;

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

private void repeatMouse()
{
    skip = true;
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    while (pressed)
    {
        System.out.println("pressed while loop " + pressed);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void nativeMouseClicked(NativeMouseEvent nativeMouseEvent) {

}

@Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
    System.out.println("GG");
    if (!(nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)) {
        System.out.println("Returned.");
        return;
    }
    if (!Native.get().getData().getEnabled())
    {
        System.out.println("Isn't enabled.");
        return;
    }

    pressed = true;
    repeatMouse();
}

@Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
    System.out.println("released");
    if (!(nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)) {
        System.out.println("Returned 2");
        return;
    }
    if (!skip)
    {
        System.out.println("pressed " + pressed);
        pressed = false;
        System.out.println("pressed " + pressed);
    } else {
        skip = false;
    }

}

}

知道为什么while循环会阻止事件被调用吗?我是否需要使用多线程或某些爵士乐?

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您的主要方法不包含在您的代码中,但我认为它包含以下行(或类似):

new Project()

try {
    bot = new Robot();
} catch (AWTException e) {
    e.printStackTrace();
}

while (pressed) {
    bot.mousePress(InputEvent.BUTTON1_MASK);
    //bot.mouseRelease(InputEvent.BUTTON1_MASK);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

现在让我们来看看这段代码。当它运行时,在开始时按下将是假的(可能),它将退出并且不会在稍后的点击中运行。

您要做的是在注册点击时启动循环。让我们把它转移到另一种方法

private void repeatMouse() {
    bot.mouseRelease(InputEvent.BUTTON1_MASK);
    while (pressed) {
        bot.mousePress(InputEvent.BUTTON1_MASK);
        bot.mouseRelease(InputEvent.BUTTON1_MASK);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

现在让我们用鼠标按下原生钩子

@Override
public void nativeMousePressed(NativeMouseEvent nativeMouseEvent) {
    if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
    {
        pressed = true;
        System.out.println(pressed);
        repeatMouse();
    }
}

编辑:
看来你的另一个问题是,在第一次mouseRelease之后,将从本机库调用该处理程序。我有一个潜在的解决方案。

首先在您定义pressed变量的位置旁边,定义新的skipRelease

boolean skipRelease = false;

然后在每次调用mouseRelease之前,首先将skipRelease设置为true。然后将mouseRelease处理程序更改为以下

@Override
public void nativeMouseReleased(NativeMouseEvent nativeMouseEvent) {
    if (nativeMouseEvent.getButton() == NativeMouseEvent.BUTTON1)
    {
        if(skipRelease) {
            skipRelease = false;
            return;
        }
        pressed = false;
        System.out.println(pressed);
    }
}