Control, Shift, Alt keys

Is there a way to read out if Control Shift or Alt is pressed during an keyPressed event? For example Shift + Arrow key.


Yes, you can take care of that with:

if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) {  
   cout << "SHIFT pressed.";  
if (glutGetModifiers() == GLUT_ACTIVE_ALT) {  
   cout << "ALT pressed.";  
if (glutGetModifiers() == GLUT_ACTIVE_CONTROL) {  
   cout << "CONTROL pressed.";  

Hmm, this did not work for me. GLUT_ACTIVE_CONTROL and similar identifiers are not declared ( I am on Xcode, osx 10.5). I also noticed that I get different key codes.

Is this intended behavior that I get a key code of 6 when I press control-f or a key code of 4 when I press control-d?

hmm… curious, I get consistent values with this / xcode 2.5 / 10.5

void keyboard_cb(unsigned char key, int x, int y) {  
	if (key == 27){				// "escape"  
	cout << glutGetModifiers() << endl;  

shift = 1, control = 2, alt = 4…


Yes, for glutGetModifiers(). Not so much for the key parameter.

CTRL-a to CTRL-z is mapped to 1 to 26
control-[ is mapped to 27 and quits the app
numbers stay the same

So effectively for all the letters and some special characters I have to check against different values and glutGetModifiers() is irrelevant.

I can work with this but I am not sure if this is consistent behavior. There is a small chance this has to do with my computer.

What values do you get for CTRL-a to CTRL-z?

Thanks, I use it now at follows:

bool ofKeyAlt() {  
    return (glutGetModifiers() & GLUT_ACTIVE_ALT);  
bool ofKeyShift() {  
    return (glutGetModifiers() & GLUT_ACTIVE_SHIFT);  
bool ofKeyControl() {  
    return (glutGetModifiers() & GLUT_ACTIVE_CTRL);  


I don’t know what happened, may be upgrading to 0.06 but glutGetModifiers outputs warning messages in the console:

GLUT: Warning in openframeworks: glutCurrentModifiers: do not call outside core input callback.  

Has anyone seen this before?


Looking for a solution to this problem, I came across this post. I have found a solution for Windows only:

bool shiftDown = ((GetKeyState( VK_SHIFT ) & 0x80) > 0);  

The low-order bit (0x1) toggles everytime you press Shift, so it should be filtered out. The high-order bit (0x80) contains the actual state of the Shift button. The above code translates this into a boolean.