How to temporarily stop listening to mouse events

If I have a very simple app like this:

void ofApp::setup(){}

void ofApp::update(){}

void ofApp::draw(){}

void ofApp::mousePressed(ofMouseEventArgs &mouse) {
    cout << "mouse pressed" << endl;
}

void ofApp::mouseMoved(ofMouseEventArgs &mouse) {}
void ofApp::mouseReleased(ofMouseEventArgs &mouse) {}
void ofApp::mouseDragged(ofMouseEventArgs &mouse) {}

void ofApp::keyPressed(int key){
    cout << "key pressed" << endl;
    ofUnregisterMouseEvents(this);
}

Even after pressing a key and seeing the key pressed message, I keep getting mouse pressed messages whenever I click the mouse. What am I missing here? Thanks!

P.S: I would like to be able to do something like this, so that I could enable and disable the mouse events as I wish in a different app.

You could also create a boolean value in you header file, for example mouseEventsEnabled.
Then toggle it on keypress and only do actions on mouse press when true.

void ofApp::keyPressed(int key){
     cout << "key pressed" << endl;
     mouseEventsEnabled = mouseEventsEnabled ? false : true;
}

void ofApp::mousePressed(ofMouseEventArgs &mouse) {
     if (mouseEventsEnabled) {
          cout << "mouse pressed" << endl;
     }
}

But what i think is going wrong with the ofUnregisterMouseEvents(), that your doing now, is that you are pointing it to the wrong listener. But i don’t know how to point it to the right one thoug. It also looks fine to me, but then again i never had to use it before :wink:

Thanks so much for your answer Bramdeleeuw! What you mention are my thoughts exactly. I don’t think I am unregistering the right class, but also not sure how to do it. I’ve went with the boolean approach, though the point of my question was exactly to avoid it as in to be able to think in a events way, that for some reason seems cleaner to me, rather than keeping state with a boolean value.