CTRL, ALT and SHIFT key in GLUT

Hi all,

I don’t know how to detect and handle event while the key CTRL, ALT or SHIFT key is pressed by using GLUT.
Anyone can help? Sample code given would be greatly appreciated.
Thx.

You can retrieve modifier key states with glutGetModifiers(). Check out man-page for more info. The following return values apply:

GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT.

If you need to find out left or right shift, you got to turn to native window API.

Ctrl plus characters are directly handled with the Key-function. Ctrl-A yield the key-code 1, etc.

/ plg

Really thx for your help. Could u help me more?
Now, I can detect the CTRL-A for key code 1 and the following, but I still can’t find the key code for ALT-A and SHIFT-A …

Thx again.

You can do this by setting a variable if ATL, CTRL or SHIFT is pressed, then when a key is pressed, you just check if either of the three keys is pressed.

Some pseudocode…

if ALT is pressed, alt=1, else alt=0
if ALT is pressed, crtl=1, else crtl=0

if ‘a’ is pressed and alt=1, alt_a is pressed
if ‘a’ is pressed and ctrl=1, alt_a is pressed
.
.
.
and so on…

In your key-function or mouse-function, get the modifier states with glutGetModifiers(). Then go on in a way described by Bob, or build tables so you can map it to a code. A lot of ifs isn’t the best way, switch is better.

e.g.

states = glutGetModifiers();
action = lookupTable[(states << 8) | key];

switch (action) {
case A_ROLL:


default:
error();
break;
}