Strange Problem about keyPressed-OF_KEY_SHIFT run two times?

if i wrote this in keyPressed

void ofApp::keyPressed(int key){
    if(OF_KEY_SHIFT){
        cout << "run" << endl;
    }
}

when i press the Shift Key ,it will cout two “run” in the output.The same result if i use “OF_KEY_ALT”,“OF_KEY_CONTROL”

1 Like

yes, this is somewhere between a bug and a feature. you see, on an average keyboard there are two of each of the following keys: SHIFT|ALT|CONTROL|SUPER.

So the first event you receive is the “generic” special key, which matches OF_KEY_ALT, for example.

The second event you receive tells you whether it was actually the right or the left ALT key, i.e. OF_KEY_LEFT_ALT or OF_KEY_RIGHT_ALT.

You can safely ignore any key released event immediately arriving after OF_KEY_CONTROL, OF_KEY_SHIFT, OF_KEY_ALT, OF_KEY_SUPER.

This is far from ideal though, (and currently the code relating to this in ofEvents.h#L317 ff. has a big FIXME), so it might be an idea to post a github issue for it so that it gets a higher priority for getting fixed soon.