Hello Guest

Mouse, just 1 click

  • 3 Replies
  • 11180 Views
Mouse, just 1 click
« on: January 02, 2015, 14:27:16 »
Hello everyone,
I have a problem with the mouse. I use in my program this code:
Code: [Select]
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

*

Offline Cornix

  • *****
  • 488
Re: Mouse, just 1 click
« Reply #1 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.

*

Offline SHC

  • **
  • 94
    • GoHarsha.com
Re: Mouse, just 1 click
« Reply #2 on: January 02, 2015, 15:09:25 »
More to add what Cornix said, I will show some code.

Code: [Select]
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.

Re: Mouse, just 1 click
« Reply #3 on: January 02, 2015, 16:17:42 »
Thanx  ;)

Code: [Select]
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.