Storing Mouse Position

Hello,

I’m having trouble detecting when the mouse isn’t moving. I previously had this working fine, however I’m using a Wacom tablet. When I used the app with a mouse it no longer works.

I worked out that ofGetPreviousMouseX doesn’t seem to update to the current position when using a mouse, and so the if statement always returns true, even if it’s still.

I was wondering how I could store the ofGetMouseX of the previous frame myself, so I can compare the two and run the same statement. At least I think that would be the best way about going about it?

Any help would be gratefully appreciated!

Thanks!

if ((ofGetPreviousMouseX() != ofGetMouseX()) || (ofGetPreviousMouseY() != ofGetMouseY())) { curserMoved = true; time = ofGetElapsedTimef(); } else { if (ofGetElapsedTimef()-time>2) { curserMoved = false; } }

1 Like

Another, perhaps easier, way would be to put curserMoved = true; in the mouseMoved and mouseDragged callbacks.

Then on each of your loops, check that cursorMoved variable, do what you need to do, and then set it to false again if needed.

void update()
{
    if(curserMoved)
    {
        // do stuff
        cursorMoved = false;
    }
}

/// ...

void mouseMoved(int x, int y)
{
   cursorMoved = true;
}

void mouseMoved(int x, int y)
{
   cursorDragged = true;
}

1 Like

Ditto to @bakercp

And if it is puzzling why your code didn’t work, it seems as though the value stored by ofGetPreviousMouseX() and ofGetPreviousMouseY() is only updated around a mouse click event. So maybe you didn’t notice any issues when using a wacom tablet because you were constantly triggering mouse clicks? Somehow else more familiar with ofGetPreviousMouseX() could probably comment…