Command + F , how to handle it


#1

Until 0.9.8 this piece of code would work

	if (key == 'f') {
		if (ofGetKeyPressed(OF_KEY_COMMAND)) {
			ofToggleFullscreen();
		}
	}

but now in 0.10.0 it doesn’t fire event.
I’ve tried to use

void keyPressed(ofKeyEventArgs & key);

and check for modifier but I think I’m just doing it wrong.

Any ideas of how to handle COMMAND + any key?
Thank you


#2

Hi!
Just a quick thought, since I’ve done something similar once.
Create a bool for the CMD-button-state, and simply toggle it anytime you press/release command.
Then you check the bool when ‘f’ is pressed.

switch(key){
        case OF_KEY_COMMAND:
            bCommandPressed = true;
            break;
        case 'f':
            if(bCommandPressed)
                ofToggleFullscreen();
            break;
    }

Hope this helps :slight_smile:


#3

Heyy thank you I’ll test it right now.
I was trying something like this:

	if (key.key == 'f' && key.hasModifier(OF_KEY_COMMAND)) {
		ofToggleFullscreen();
	}

but I think modifier changes the value of the ‘key.key’ when command is pressed.
Thank you


#4

Ah! Didn’t realize this is different than the basic keyPressed(int key) function.
I’ve tried it this way, and with std::cout << key.key << std::endl I saw it modified the value, when cmd is pressed. (f = 102, cmd+f = 6)
So another way could be

switch(key.key){
    case 6:
        ofToggleFullscreen();
        break;
}

Assuming no other key-combinations result in 6… Cheers!


#5

Yeah I’m arrived at the same conclusion here. I’m afraid if this 6 can appear in other key sequences.

I’m kinda confused because even using void ofApp::keyPressed(int key) the key value is modified by command key in 0.10.0 (different thing in 0.9.8)

@arturo if you have the time bring us some light (command + light) to the issue


#6

Maybe this is related, I assumed I had found a bug with modifier keys in the current release.
Putting this code in keypressed() prints both couts when I hit the command key. Something changed for sure.

keyPressed(int key){
    if (key == OF_KEY_COMMAND) {
        cout<<"got the command key" << endl;
    }
    if (key != OF_KEY_COMMAND) {
        cout<<"got another key" << endl;
    }
}