2014-08-30 64 views
1
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" 

上述輸出是所有顯示,儘管許多點擊。我不確定我做錯了什麼。我不知道如何避免在輪詢鼠標按鈕狀態時阻止控制檯氾濫,而無需進行某種控制檯打印控制。有沒有更有效的方法來做到這一點?或者這是足夠的,只有一些被忽視的錯誤?

+0

我已閱讀它。多次,錯誤在哪裏? – 2014-08-30 07:03:35

+0

啊,比較語句中的賦值語句。錯誤的操作員。我剛剛發現了這一點。 – 2014-08-30 07:21:05

回答

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