Mouse, just 1 click

Started by Kroko309, January 02, 2015, 14:27:16

Previous topic - Next topic

Kroko309

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

Cornix

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.

SHC

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.

Kroko309

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.