Reading keys from the user

Hi,

I am just a beginner. I want to draw an object that moves according to the pressed arrow key.

How can I read the key pressed by the user?

Here’s an example of code featuring keyboard interaction.
I recently posted it in response to another quiestion.


//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
 
#include <windows.h>
#include <stdio.h>
#include <gl.h>         // The GL Header File
#include <glut.h>       // The GL Utility Toolkit (Glut) Header
 
 
float  xs = 0.0, ys = 0.0, tip = -10.0, trn = 0.0,  zum = 1.0,
	   rquad1 = 0.0, rquad2 = 0.0, rquad3 = 0.0;
 
int cube[8][3] = {{-1,-1,+1}, {+1,-1,+1}, {+1,+1,+1}, {-1,+1,+1}, 
                  {-1,-1,-1}, {+1,-1,-1}, {+1,+1,-1}, {-1,+1,-1}};
 
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//----------------------------------------   Arrow_Keys   ------------------------------------------
 
void Arrow_Keys (int s_keys, int x, int y)
{
    switch (s_keys)  {
 
       case  GLUT_KEY_RIGHT:  xs += 0.01;  break;
       case  GLUT_KEY_LEFT :  xs -= 0.01;  break;
       case  GLUT_KEY_UP   :  ys += 0.01;  break;
       case  GLUT_KEY_DOWN :  ys -= 0.01;  break;
 
       default:  printf ("   Keyboard -> key = %d, key = %c
", s_keys, s_keys);   break;
    }
 
    glutPostRedisplay ();
}
 
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//---------------------------------------   Rotate_Cubes   -----------------------------------------
 
// Animate rotation of cubes.
 
void Rotate_Cubes (void)
{
    static float  rot1 = 3.0, rot2 = 5.0, rot3 = 8.0;
 
    rquad1 += rot1;
    rquad2 -= rot2;
    rquad3 += rot3;
 
    glutPostRedisplay ();
}
 
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//-----------------------------------------   Keyboard   -------------------------------------------
 
void Keyboard (unsigned char key, int x, int y)
{
    switch (key)  {
 
       case 'a':  glutIdleFunc (Rotate_Cubes);  break;
       case 'z':  glutIdleFunc (    NULL    );  break;
 
       case 'x':  tip += 3.0;  break;
       case 'X':  tip -= 3.0;  break;
       case 'y':  trn += 3.0;  break;
       case 'Y':  trn -= 3.0;  break;
       case 's':  zum *= 1.1;  break;
       case 'S':  zum *= 0.9;  break;
 
       case 'r':  tip = 0.0; trn = 0.0; zum = 1.0;  break;
 
       case  27:  exit(0);  break;      // Quit program with 'esc' key.
 
       default :  printf ("   Keyboard -> key = %d, key = %c
", key, key);   break;
    }
 
    glutPostRedisplay ();
}
 
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//-----------------------------------------   One_Cube   ------------------------------------------
 
void One_Cube (void)
{
    glBegin (GL_QUADS);
       glColor3f (1.0, 0.6, 0.3);
       glVertex3iv (cube[0]); glVertex3iv (cube[1]); glVertex3iv (cube[2]); glVertex3iv (cube[3]); // front
       glColor3f (0.3, 1.0, 0.6);
       glVertex3iv (cube[1]); glVertex3iv (cube[5]); glVertex3iv (cube[6]); glVertex3iv (cube[2]); // right
       glColor3f (0.3, 0.6, 1.0);
       glVertex3iv (cube[6]); glVertex3iv (cube[5]); glVertex3iv (cube[4]); glVertex3iv (cube[7]); // back
       glColor3f (0.6, 0.3, 1.0);
       glVertex3iv (cube[7]); glVertex3iv (cube[4]); glVertex3iv (cube[0]); glVertex3iv (cube[3]); // left
       glColor3f (0.8, 0.8, 0.2);
       glVertex3iv (cube[3]); glVertex3iv (cube[2]); glVertex3iv (cube[6]); glVertex3iv (cube[7]); // top
       glVertex3iv (cube[5]); glVertex3iv (cube[1]); glVertex3iv (cube[0]); glVertex3iv (cube[4]); // bottom
    glEnd ();
}
 
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//----------------------------------------   Draw_Cubes   ------------------------------------------
 
void Draw_Cubes (void)
{
	glPushMatrix ();
	   glRotatef (rquad1, 0,1,0);
	   One_Cube ();
	glPopMatrix ();
 
	glPushMatrix ();
	   glTranslatef (5.0, 0.0, 0.0);
	   glRotatef    (rquad2, 0,1,0);
	   One_Cube ();
	glPopMatrix ();
 
	glPushMatrix ();
	   glTranslatef (-5.0, 0.0, 0.0);
	   glRotatef    (rquad3, 0,1,0 );
	   One_Cube ();
	glPopMatrix ();
}
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//-----------------------------------------   Display   --------------------------------------------
 
 
void Display (void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    glMatrixMode (GL_MODELVIEW);   // Perform operations on the Modeling matrix.
    glLoadIdentity ();             // Clears Model transformation stack to the identity matrix.
 
 
	// Modeling transformations used here to change view of teapot and axes.
 
    glTranslatef (xs, ys, 0 );    // Use arrow keys to move scene up-down, right-left.
    glRotatef (tip, 1, 0, 0 );    // Use 'x' key to rotate around X axis (tipping)
    glRotatef (trn, 0, 1, 0 );    // Use 'y' key to rotate around Y axis (turning)
    glScalef  (zum, zum, zum);    // Use 's' key to scale objects in scene.
 
    glScalef (0.1, 0.1, 0.1);
 
    Draw_Cubes ();
 
    glutSwapBuffers ();
}
 
 
//---+----4----+----3----+----2----+----1----+---/\---+----1----+----2----+----3----+----4----+---\\
//-------------------------------------------   main   ---------------------------------------------
 
void main (int argc, char** argv) 
{
    glutInit (&argc, argv);
 
    glutInitDisplayMode    (GLUT_RGB | GLUT_DOUBLE); 
    glutInitWindowSize     (800, 800); 
    glutInitWindowPosition (300, 150);
    glutCreateWindow       ("Spinning Cubes");
 
    glutKeyboardFunc ( Keyboard );
    glutDisplayFunc  ( Display  );
    glutSpecialFunc  (Arrow_Keys);
 
    glClearColor (0.0, 0.1, 0.2, 0.0);    // Clear screen to dark blue.
    glEnable (GL_DEPTH_TEST);
 
    glutMainLoop ();
}