Control + Click or Command + Click

Hi all!

Does anyone know if it is possible to capture de Control key or Command Key?
I need to ejecute some code on the mouse pressed event, when the command key or the control key is pressed.

Thanks!

Hi there,

Yes it is! :slight_smile:

You can even know if it is left or right. Either you do ofGetKeyPressed() or you use the ofApp::keyPressed(int key) event, with the OF_KEY constants. For exemple:

if (ofGetKeyPressed(OF_KEY_LEFT_COMMAND)) {
        // Do something cool
}

Or:

void ofApp::keyPressed(int key){
    
    if (key == OF_KEY_RIGHT_CONTROL) {
        // Do something cooler
    }

}

You can get the list key constants list in libs/openFrameworks/utils/ofConstants.h, or if your IDE as Code Completion, just type OF_KEY_ to check the full list.


To combine the two, you can do:

void ofApp::mousePressed(int x, int y, int button){
    
    if (ofGetKeyPressed(OF_KEY_LEFT_COMMAND)) {
       // Do something even cooler
    }

}

Thanks Hubris!

Unfortunately I already tried with what you wrote. I find it really wierd that I set a breakpoint in the ofApp::keyPressed function, and nothing happens when I press Shift, Alt, Fn, Control or Command. Is like i didn’t even press those keys.

Im using OSx 10.10.5 and xCode 6.4

Hum… Since only the modifiers don’t work, do you use an external keyboard or maybe a different/less common layout?

What do you mean with different layout?
I use don’t use external keyboard, just the one that comes with mac :smile:

I think It has something to do with an addon i’m using: ofxComposer.
I created a blank project and it worked there. Then I created a new project with ofxComposer, and it didn’t work.