LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: CuppoJava on April 02, 2005, 07:30:22

Title: Mouse.setGrabbed creates button presses for Mouse.next
Post by: CuppoJava on April 02, 2005, 07:30:22
Hi,
I'm doing a simple input manager for my game, and I just noticed that whenever I initialize a window or call Mouse.setGrabbed(true), I get a whole whackload of reported mouse button presses even though I didn't click the mouse button. Here's the code I use to check for mouse presses:


while(Mouse.next())
{
 if(Mouse.getEventButton()>=0)
   System.out.println("mouse pressed");
}


Its kind of annoying, as I bound mouse presses to a save function. Now whenever the mouse is setGrabbed(true) it saves.
Title: More information
Post by: CuppoJava on April 02, 2005, 18:53:43
Okay. I thought it would have been an easy workaround but I apparently not. I'm really stuck now.

I replaced my Mouse.next() routine with just calling
Mouse.isButtonDown(int button) but the method is giving me some strange replies.

Whenever I ungrab the mouse, Mouse.isButtonDown(0) always returns true. I don't know where the problem is...my code does not directly interfere with the Mouse, it just reads whether the buttons are down are not. Why does it report that the Left button is down when I'm not holding it down?

Thx for any help. I'll keep on fiddling around.
Title: AHA!
Post by: CuppoJava on April 02, 2005, 19:08:05
   boolean f1;
   boolean m;
   private void mainloop() {
     if(Display.isCloseRequested()) {                     // Exit if window is closed
         done = true;
     }
     if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) {  
       Mouse.setGrabbed(false);                           //On F1 press - release the mouse
         f1 = true;                                    
     }
     if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) {        
         f1 = false;
     }

     if(Mouse.isButtonDown(0))
     {
       if(!m)
       {
         Mouse.setGrabbed(true);           //On mouse press - grab the mouse
         m = true;
       }
     }
     else
       m = false;
   }


Ok I forced out a simple example. This is a modified mainloop method from anyone of Nehe's tutorials. I want it to grab the mouse when I left-click. and Release the mouse when I press F1.

The workaround is : THE MOUSE SHOULD NOT BE GRABBED BETWEEN THE BUTTON PRESS AND THE BUTTON RELEASE. The program should always wait for all mouse buttons to be released before grabbing the mouse.

Ok - I think that was the key. I hope this is of some use to someone in fixing this.