Sign in to follow this  
UglyJoe

GLUT Modifier Keys

Recommended Posts

UglyJoe    122
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?

Share this post


Link to post
Share on other sites
Drew_Benton    1861
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!

Share this post


Link to post
Share on other sites
UglyJoe    122
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!

Share this post


Link to post
Share on other sites
Drew_Benton    1861
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!

Share this post


Link to post
Share on other sites
UglyJoe    122
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this