iDevGames Forums
Left mouse button causes moueup event despite button still being pressed. - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Left mouse button causes moueup event despite button still being pressed. (/thread-2569.html)

Pages: 1 2


Left mouse button causes moueup event despite button still being pressed. - QuestingCordiial - Jun 18, 2008 12:33 AM

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. : )


Left mouse button causes moueup event despite button still being pressed. - ThemsAllTook - Jun 18, 2008 05:16 AM

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?


Left mouse button causes moueup event despite button still being pressed. - AnotherJake - Jun 18, 2008 06:33 AM

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.


Left mouse button causes moueup event despite button still being pressed. - QuestingCordiial - Jun 18, 2008 05:25 PM

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.


Left mouse button causes moueup event despite button still being pressed. - AnotherJake - Jun 18, 2008 08:44 PM

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.


Left mouse button causes moueup event despite button still being pressed. - QuestingCordiial - Jun 18, 2008 11:01 PM

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


Left mouse button causes moueup event despite button still being pressed. - QuestingCordiial - Jun 18, 2008 11:03 PM

Another thing, some of the mouseDown events are not detected at all. But this occurs randomly, once in about 5 clicks.


Left mouse button causes moueup event despite button still being pressed. - QuestingCordiial - Jun 18, 2008 11:09 PM

Restarting with the different mouse made no difference.


Left mouse button causes moueup event despite button still being pressed. - QuestingCordiial - Jun 18, 2008 11:23 PM

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?


Left mouse button causes moueup event despite button still being pressed. - AnotherJake - Jun 19, 2008 08:17 AM

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.


Left mouse button causes moueup event despite button still being pressed. - TomorrowPlusX - Jun 19, 2008 02:06 PM

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.


Left mouse button causes moueup event despite button still being pressed. - reubert - Jun 19, 2008 02:22 PM

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?


Left mouse button causes moueup event despite button still being pressed. - AnotherJake - Jun 19, 2008 03:09 PM

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


Left mouse button causes moueup event despite button still being pressed. - reubert - Jun 19, 2008 03:11 PM

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.


Left mouse button causes moueup event despite button still being pressed. - wyrmmage - Jun 19, 2008 05:27 PM

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