GLUT Tutorial Handling Keyboard Events

GLUT Tutorial – Handling Keyboard Events

Almost all of the games use Keyboard to control the objects i.e. to move, jump, turn, slide etc.  Keyboard events is essential part of game programming. All game development libraries support Keyboard Handling. GLUT also has nice and simple functions that provide the facility of Keyboard controlling. GLUT provides different functions to handle NORMAL key events like 1 , 2 , A, B, ESC (having ASCII code)  and SPECIAL key events like UP, DOWN, F1 etc.

The first one,glutKeyboardFunc, is used to tell the windows system which function we want to process the “normal” key events. By “normal” keys, we mean letters, numbers, anything that has an ASCII code. The syntax for this function is as follows:

void glutKeyboardFunc(void (*func) (unsigned char key, int x, int y));

where, func – The name of the function that will process the “normal” keyboard events. Passing NULL as an argument causes GLUT to ignore “normal” keys.The function used as an argument to glutKeyboardFunc needs to have three arguments. The first indicates the ASCII code of the key pressed, the remaining two arguments provide the mouse position when the key is pressed. The mouse position is relative to the top left corner of the client area of the window.
The second one, glutSpecialFunc, is used to tell the windows system which function we want to process the “special” key events. The syntax for this is as follows.
void glutSpecialFunc(void (*func) (int key, int x, int y));                                                                  
where, func – The name of the function that will process the special keyboard events. Passing NULL as an argument causes GLUT to ignore the special keys.

The GLUT_KEY_* are predefined constants in glut.h. The full set of constants is presented next:

GLUT_KEY_F1  F1 function key
GLUT_KEY_F2 F2 function key
GLUT_KEY_F3 F3 function key
GLUT_KEY_F4 F4 function key
GLUT_KEY_F5 F5 function key
GLUT_KEY_F6 F6 function key
GLUT_KEY_F7 F7 function key
GLUT_KEY_F8 F8 function key
GLUT_KEY_F9 F9 function key
GLUT_KEY_F10 F10 function key
GLUT_KEY_F11 F11 function key
GLUT_KEY_F12 F12 function key
GLUT_KEY_LEFT Left function key
GLUT_KEY_RIGHT Right function key
GLUT_KEY_UP Up function key
GLUT_KEY_DOWN Down function key
GLUT_KEY_PAGE_UP Page Up function key
GLUT_KEY_PAGE_DOWN Page Down function key
GLUT_KEY_HOME Home function key
GLUT_KEY_END End function key
GLUT_KEY_INSERT Insert function key