mousePressed() not updating mouse position

Hello oF people.

I’m having a bit of a funny issue with mouse presses and focus. I’m working on OSX and if my application is not in the foreground I obviously cannot interact with it (as it should be). When I click on the app window it brings my app to the fore-ground but does not register an ofMousePressed() event in the app (as intended I believe). The funny thing that’s happening is that if I continue clicking in the same location again and again, it does not update the mouses position but it does register the mousePressed() event but with the wrong x and y values, ie the values that it had when I last lost focus on the app. If I move my mouse and get a mouseMoved() event then it updates to the correct x and y positions. I think I can see why, if I follow the call-stack I get back to ofAppGLFWWindow.cpp with the following two methods:

void ofAppGLFWWindow::mouse_cb(GLFWwindow* windowP_, int button, int state, int mods) {
	ofAppGLFWWindow * instance = setCurrent(windowP_);
    //we do this as unlike glut, glfw doesn't report right click for ctrl click or middle click for alt click 
    if( instance->events().getKeyPressed(OF_KEY_CONTROL) && button == GLFW_MOUSE_BUTTON_LEFT){
        button = GLFW_MOUSE_BUTTON_RIGHT; 
    if( instance->events().getKeyPressed(OF_KEY_ALT) && button == GLFW_MOUSE_BUTTON_LEFT){
        button = GLFW_MOUSE_BUTTON_MIDDLE; 
	if (state == GLFW_PRESS) {
		instance->events().notifyMousePressed(instance->events().getMouseX(), instance->events().getMouseY(), button);
	} else if (state == GLFW_RELEASE) {
		instance->events().notifyMouseReleased(instance->events().getMouseX(), instance->events().getMouseY(), button);
	instance->buttonInUse = button;
void ofAppGLFWWindow::motion_cb(GLFWwindow* windowP_, double x, double y) {
	ofAppGLFWWindow * instance = setCurrent(windowP_);
	rotateMouseXY(instance->orientation, instance->getWidth(), instance->getHeight(), x, y);
		instance->events().notifyMouseMoved(x*instance->pixelScreenCoordScale, y*instance->pixelScreenCoordScale);
		instance->events().notifyMouseDragged(x*instance->pixelScreenCoordScale, y*instance->pixelScreenCoordScale, instance->buttonInUse);

This makes a lot of sense as the motion_cb function takes an x and y value but the mouse_cb function does not and the notifyMosueReleased and notifyMousePressed calls take the previously saved mouseX and mouseY. Now I know that the system knows where the mouse is, so I’ve very much like to send that information to mousePressed directly instead of sending it the last saved mouseMoved mouse position.

The reason I need this is that I use it on a touchscreen so if the app loses focus I need people to be able to click on it without dragging their finger around the screen first.

Any help on this would be greatly appreciated!

Wow, it’s 2020, and this reported behavior is exactly what I’m seeing. Thanks for the post – it really saved time thinking it’s got to be something I’m doing wrong.

After you click to gain focus, any mouse move seems to set mouseX, mouseY to the new location, but until then, mouseX and mouseY are wrong, and incorrect values are passed to the mousePressed callback.

1 Like