Left mouse button causes moueup event despite button still being pressed.

Apprentice
Posts: 9
Joined: 2008.06
Post: #1
Hello everyone Smile

I am working on mouse input for a game, I am having trouble with the left mouse button, the right button works perfectly.

I am using GLUT.

Here is my mouse event handling code.

I am using integers to keep track of the mouse button states. when the mouse button is released, this is represented by a continually decreasing negative value in iMouseLeftButton and iMouseRightButton.

When the button is depressed, it is represented by a continually increasing positive value.
Code:
void HandleMousePress (int button, int state, int x, int y)
{
    if ( button == GLUT_LEFT_BUTTON )
    {
        if ( state == GLUT_DOWN )
        {
            if ( iMouseLeftButton < 0 )
            {
                iMouseLeftButton = 1;
            }
        }
        else if ( state == GLUT_UP )
        {
            if ( iMouseLeftButton > 0 )
            {
                iMouseLeftButton = -1;
            }
        }
    }
    else if ( button == GLUT_RIGHT_BUTTON )
    {
        if ( state == GLUT_DOWN )
        {
            if ( iMouseRightButton < 0 )
            {
                iMouseRightButton = 1;
            }
        }
        else if ( state == GLUT_UP )
        {
            if ( iMouseRightButton > 0 )
            {
                iMouseRightButton = -1;
            }
        }
    }
}

The right mouse button portion works perfectly, but the left mouse button not so well.

When I press the left button, the code works fine for one cycle, after that it thinks that the left button has been released, even though i am still holding it.

I have tried switching the code around, substituting GL_LEFT_BUTTON with GL_RIGHT_BUTTON, just to see if I had written it incorrectly. GL_RIGHT_BUTTON works perfectly.

Any ideas?

Thanks in advance. : )
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Strange. GLUT mouseUps have been working for me. What OS version are you on? How soon after pressing the button do you get a GLUT_UP? Does the left button on your mouse generally work as expected in other applications?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #3
It should work. My guess is that your iMouseLeftButton is being unintentionally modified incorrectly somewhere else in your program.

It is strange to me that you are continually increasing or decreasing those values, which suggests to me that maybe you are doing so to use them as some sort of timing values for animation of some sort. If that's the case, then maybe (wild guess) you're doing a iMouseLeftButton += 1; where you meant to be doing a iMouseLeftButton -= 1; instead.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.06
Post: #4
Thanks heaps for the replies Smile

I am using Leopard, and the mouse works fine for all other applications.

The mouseUp event is instantaneous, it occurs seemingly at the same time as i press the mouse down.

I have checked that the only place in the code which sets iMouseLeftButton to a negative value is that in the code that I've put up here. This is what makes me think that random GLUT_UP events are responsible, because the only code that changes iMouseLeftButton to a negative value is inside that IF statement.

And I've also checked that the code responsible for decreasing/increasing iMouseLeftButton is correct. It is exactly the same as that for iMouseRightButton. (except with the variable switched)

Once again thanks for the replies. Smile

I'll post up some of the other code when I can.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #5
QuestingCordiial Wrote:I'll post up some of the other code when I can.
That'd be helpful.

Just as a little sanity check, I tested out glutMouseFunc, using your code snippet, and it works just fine on my system (10.5.3) -- no unexpected mouse ups.

What brand of mouse are you using? I'm using a Wacom mouse (with the tablet) and I've had many issues with it over the last couple years because of the drivers, but have managed to work around them in my own software and report bugs which have been subsequently been fixed in others' software.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.06
Post: #6
Here is the other section of code.

Code:
void ManageKeyTimers()
{

    if ( iMouseLeftButton < 0 ) iMouseLeftButton--;
    else if ( iMouseLeftButton > 0 ) iMouseLeftButton++;
    
    if ( iMouseRightButton < 0 ) iMouseRightButton--;
    else if ( iMouseRightButton > 0 ) iMouseRightButton++;    
    
}

Each of the variables is initialized as -1.

I've done a search through all the code and these two bits of code are the only parts that modify the two variables in any way.

I have tried using two different mouses, one windows one and the other the mighty mouse which came with the computer. I don't think the fault is in the mouse though, because both mice work perfectly with all other programs.

I am also using 10.5.3

What I haven't tried is restarting the computer with the different mouse plugged in. I'll try that now.

Thanks
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.06
Post: #7
Another thing, some of the mouseDown events are not detected at all. But this occurs randomly, once in about 5 clicks.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.06
Post: #8
Restarting with the different mouse made no difference.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2008.06
Post: #9
RIGHT!

I've got the cause!

I am making a call to glutWarpPointer every cycle in order for mouse look to function correctly, and when i comment this out, the left mouse button functions perfectly!

But this is a very necessary function to call. I tried doing this:

int temp = iMouseLeftButton;
glutWarpPointer(200, 200);
iMouseLeftButton = temp;

But it made no difference, so glutWarpPointer must be sending GLUT_UP messages.

Smile

Do you know how to stop it?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #10
This looks like a bug in GLUT. GLUT wasn't designed to be very robust, but that feature should work.

You can use something like this instead of glutWarpPointer:

CGPoint pos;
pos.x = 200;
pos.y = 200;
CGWarpMouseCursorPosition(pos);

You'll need to add the ApplicationServices framework to your project.

BTW, you *are* aware that ints have bounding limits right? That is, you need to clamp the value to less than INT_MAX and greater than INT_MIN to get expected behavior. Otherwise, if you let your application sit too long, your mouse button values will eventually grow (or shrink) outside of the limits of an int and unintended values will result.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #11
Can you describe what you're attempting to actually do? The code you've shown is very odd looking; it may very well be that there is a different and more robust approach -- one which will fit better within the functionality GLUT offers.
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #12
have you stuck a printf at the top level of the HandleMousePress function to see what GLUT is giving you? It seems more likely that the bug is to do with your (odd looking) integer incrementing. Normally a boolean would be used to store the button state on or off, with a separate counter if you need to know how long it's been down for.

Otherwise, a fix when changing unrelated code can be a sign that you're corrupting memory somewhere. Make sure you're not overrunning any arrays, and that everything is allocated correctly etc.

And those ints are ints and not unsigned ints right?

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #13
FWIW, I tested glutWarpPointer on my machine and I can confirm that it screws up values given to glutMouseFunc (i.e. this does indeed look like a genuine GLUT bug). The good news is that inserting the code I listed above does fix the problem if you want to do mouse warping in GLUT.

... And I agree, we normally use Booleans for the inputs and apply animation *based* on those, not actually *to* them. Wink
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #14
AnotherJake Wrote:FWIW, I tested glutWarpPointer on my machine and I can confirm that it screws up values given to glutMouseFunc (i.e. this does indeed look like a genuine GLUT bug).

How about that. Lucky, then Smile Nothing worse than trying to track down a memory trasher.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #15
I don't know if this is at all relevant, but every time you call glutWarpPointer(), mouseMovement (or whatever the name is) function is called, so if you're doing anything in there involving key presses, you'll want to be careful.
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  customize an action of iphone home button to submit score in gamecenter sefiroths 7 7,345 Nov 30, 2011 01:59 AM
Last Post: sefiroths
  only one error left! Please help... Kingbry 10 4,899 Sep 11, 2007 01:38 AM
Last Post: OneSadCookie
  Mouse button enabler, like GamePad Companion setera 4 4,396 Apr 30, 2006 06:32 PM
Last Post: setera
  Event Handling (Keyboard and Mouse) bwalters 6 7,394 Mar 12, 2006 08:17 PM
Last Post: OneSadCookie
  Configurable keys - describe key that was pressed MattDiamond 13 7,119 Oct 27, 2003 11:23 PM
Last Post: OneSadCookie