gpt4 book ai didi

java - 鼠标监听器洪水保护仅报告第一次点击

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:55 25 4
gpt4 key购买 nike

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"

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

最佳答案

问题是repeatLeftReleased = false,因为它是一个赋值(计算结果为false)并且不是等式。同样,正确版本的代码也存在此缺陷。逻辑很好(至少在我使用自动点击器赢得所有关卡之前是这样;-),尽管存在重复和 NOP 代码。

为防止将来出现此问题,请勿使用 == true/== false 而是直接使用 boolean 变量/表达式值;

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;
}
}

关于java - 鼠标监听器洪水保护仅报告第一次点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com