LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Kroko309 on January 02, 2015, 14:27:16

Title: Mouse, just 1 click
Post by: Kroko309 on January 02, 2015, 14:27:16
Hello everyone,
I have a problem with the mouse. I use in my program this code:
public void pollInput() {
         
        if (Mouse.isButtonDown(0)) {
        int x = Mouse.getX();
        int y = Mouse.getY();
             
        System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
    }

It is from LWJGL tutorial imput.

Everything works great, but when i click i get the value of the mouse 3 times. I need to get only one entry in one click. No matter how long the click will be. Is ther a option of keyrelease?

Sorry for my English i am form Slowakia.
Thanks for Help. ;D
Title: Re: Mouse, just 1 click
Post by: Cornix on January 02, 2015, 15:04:02
You can do this yourself by adding a boolean variable that you toggle.
When the mouse is pressed you set the variable to true, if the mouse is released you set the variable to false.
You only process the event if the boolean variable was toggled.
Title: Re: Mouse, just 1 click
Post by: SHC on January 02, 2015, 15:09:25
More to add what Cornix said, I will show some code.


private boolean mouseButton1 = false;

public void poll()
{
    if (Mouse.isButtonDown(0) && !mouseButton1)
    {
        // Mouse was clicked only in this frame
        int x = Mouse.getX();
        int y = Mouse.getY();
             
        System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
    }

    // Poll the input state here
    mouseButton1 = Mouse.isButtonDown(0);
}

Hope this helps you.
Title: Re: Mouse, just 1 click
Post by: Kroko309 on January 02, 2015, 16:17:42
Thanx  ;)

while (Mouse.next()){
    if (Mouse.getEventButtonState()) {
        if (Mouse.getEventButton() == 0) {
            System.out.println("Left button pressed");
        }
    }else {
        if (Mouse.getEventButton() == 0) {
            System.out.println("Left button released");
        }
    }
}


I found such a solution, but your your solution I like more so i use your's.