GLUT Modifier Keys

Started by
4 comments, last by Drew_Benton 18 years, 10 months ago
Is there any way to get Glut to call a function when a modifier key is pressed? I know of glutGetModifiers(), but that can only be called from within the key-handling functions themselves. I can't find any way of testing for them unless another key is pressed. More directly, in a game where the arrow keys move the player, I want the player to slow down when the user holds down shift. If I were to hold shift and then hold an arrow key, the arrow key would trigger the key-handler and I could check for the shift button. However, if I were to hold left and then hold down shift, I have no way of checking for the shift button until another key is pressed (such as the user pressing another arrow key). Any ideas?
Advertisement
Have a look at this page and see if it has what you need. Just a quick excerpt:
Quote:
CTRL, ALT and SHIFT

Sometimes we may want to know if one modifier key, i.e. CTRL, ALT or SHIFT is being pressed. GLUT provides a function that detects if any modifier is being pressed. This function should only be called inside the functions that process keyboard or mouse input events. The syntax for this function is:

int glutGetModifiers(void);
The return value for this function is either one of three predefined constants (in glut.h), or any bitwise OR combination of them. The constants are:

# GLUT_ACTIVE_SHIFT - Set if either you press the SHIFT key, or Caps Lock is on. Note that if they are both on then the constant is not set.
# GLUT_ACTIVE_CTRL - Set if you press the CTRL key.
# GLUT_ACTIVE_ALT - Set if you press the ALT key.

Beware that the windows system may intercept some modifiers, no callback is generated in these cases. So lets extent our processNormalKeys a little bit to see how to handle these modifier keys. Suppose that you want the variable red to be 0.0 when the user presses r, and 1.0 when the user presses ALT + r. The following piece of code will do the trick:


Happy reading! If it's not what you are looking for, perhaps another explanation to what you want to do might be needed [wink]. Good luck!
Quote:Original post by Drew_Benton
Have a look at this page and see if it has what you need.


Yup, I've come across that page quite bit while googling for this.

Quote:This function should only be called inside the functions that process keyboard or mouse input events.


That's the main problem. That "should" is actually a "can". Glut won't allow me to check for a modifier key until a key is pressed. I need it to be able to detect a modifier key without any other key being pressed.

Thanks for the reply!
Ahh, ok, I see what you mean now. I just gave it a try out and you are right, it will not work at all. I guess you will have to either find another way to accomplish what you are doing, or switch to another OpenGL framework that is more suited for games. Those include SDL, GLFW, and the OGLWFW. Good luck!
Ah, got it working using SDL. I overestimated what Glut actually does, so it was easier to port my code than I thought it would be.
Great to hear! Yea GLUT is more for demo apps and such, as I was told [wink]

4000th post! [smile]

This topic is closed to new replies.

Advertisement