mousePressed equivalent of toggle button

Hi sorry for the amateur hour question but I haven’t worked much with the mouse before I am trying to make a simple toggle box.

To toggle a bool with keyboard I would right this:


void ofApp::keyPressed(int key){
if(key == ‘a’){
clicked = !clicked;
}

So I wrote this and can’t figure out why it doesn’t work:

void ofApp::mousePressed(int x, int y, int button){
if(button == 0){
clicked = !clicked;
}

}
Thanks in advance.

This is working fine for me when pressing the left mouse button.
void ofApp::mousePressed(int x, int y, int button){
if (button == 0) {
clicked = !clicked;
cout << clicked << endl;
}
}

Maybe something else is going wrong? Could you post more code concerning your issue?

Thanks for the reply. I didn’t have the cout local but it returns now a 1 with a 0 right after it. Two values at once and thats without releasing. I am on 8.4 on xcode 7.1.1 El Capitan.

void ofApp::setup(){
ofBackground(0);
w = ofGetWidth();
h = ofGetHeight();
size = 20;
clicked = false;
}
//----------------------------------------------------
void ofApp::draw(){
ofSetColor(255);
if((ofDist(w/2, h/2, mouseX, mouseY)<size)&& clicked == true){
   ofFill();
 } else {
   ofNoFill();
  }
ofCircle(w/2,h/2,size,size);
}
//--------------------------------
void ofApp::keyPressed(int key){
//WORKS 
if(key == 'a'){
clicked = !clicked;
}
//----------------------------------
void ofApp::mousePressed(int x, int y, int button){
//doesn't work
if(button == 0){
     clicked = !clicked;
}
cout<<clicked<<endl;
}

Also it should be noted that on that version the mouse wouldn’t show up on the screen but their was a fix that I found on here. Possibly that is screwing something up? I really don’t want to upgrade because I’m working a lot with the raspberry pi and a handful of addons. Thanks for ensuring me that I wasn’t loosing my mind. However if anyone has a solution it would be much appreciated. I definitely think its an error now because the toggle box on the gui also did the same thing. It was a test at the time so I didn’t think much of it.

Edit: However just so you know:

void ofApp::mousePressed(int x, int y, int button){

  if(button == 0){
       clicked = true;
     }

Does return true and just one value. So the pressed function seems to be working.

I think your mousePressed function is executed twice, maybe in the fix you found? Because this code seems to be fine, it worked fine for me on an empty project :smile: So it should be somewhere else I guess

thanks yea i screwed it up and just realized that 0.9 is for rpi now soo…i might upgrade