鼠标侦听器防洪仅报告首次点击

时间:2014-08-30 06:58:06

标签: java events mouse

public void get_Mouse_press()
{
    boolean leftDown = Mouse.isButtonDown(0);
    boolean rightDown = Mouse.isButtonDown(1);
    validate_Mouse_press(leftDown, rightDown);

}

public void validate_Mouse_press(boolean left, boolean right)
{
    if (left == true)
    {
        if(repeatLeftPressed == false)
        {
            System.out.println("Left mouse button down");
            repeatLeftPressed = true;   
            repeatLeftReleased = false;
        }
        if(repeatLeftPressed == true)
        {   
            repeatLeftReleased = false;
        }
    }
    if (right == true)
    {
        if(repeatRightPressed == false)
        {
            System.out.println("Right mouse button down");
            repeatRightPressed = true;
            repeatRightReleased = false;
        }
        if(repeatRightPressed == true)
        {   
            repeatRightReleased = false;
        }
    }
    if (left == false)
    {
        if(repeatLeftReleased = false)
        {
            System.out.println("Left mouse button released");
            repeatLeftReleased = true;
            repeatLeftPressed = false;
        }
        if(repeatLeftReleased == true)
        {   
            repeatLeftPressed = false;
        }
    }
    if (right == false)
    {
        if(repeatRightReleased = false)
        {
            System.out.println("Right mouse button released");
            repeatRightReleased = true;
            repeatRightPressed = false;
        }
        if(repeatRightReleased == true)
        {   
            repeatRightPressed = false;
        }
    }

}

当我运行程序时,它无法正常运行。我希望程序在每次按下鼠标键时,每次释放一个鼠标键时,都会在屏幕上打印相应的语句。这是为了防止快速fps泛滥。 但是,它仅在我第一次单击左或右键时显示​​。

输出

"Left mouse button down"
"Right mouse button down"

尽管点击次数很多,但仍会显示以上输出。我不确定我做错了什么。我不知道在轮询鼠标按钮状态时如何防止控制台泛滥,没有某种控制台打印控制。有没有更有效的方法来做到这一点?或者这是否足够,只有一些被忽视的错误?

1 个答案:

答案 0 :(得分:0)

问题是repeatLeftReleased = false因为它是一个赋值(评估为false)并且不是是一个相等的。同样,右侧发布的代码也有这个缺陷。逻辑很好(至少在我使用Auto Clicker赢得所有级别之前;-),尽管有重复和NOP代码。

为防止将来出现此问题,请不要使用== true/== false,而应直接使用布尔变量/表达式值;

if (!left)
{
    if(!repeatLeftReleased) // no chance for `=` now!
    {
        System.out.println("Left mouse button released");
        repeatLeftReleased = true;
        repeatLeftPressed = false;
    }
    if(repeatLeftReleased) // this is always true here
    {   
        repeatLeftPressed = false;
    }
}