what is call back function

glutKeyboardFunc and glutSpecialFunc are both callback function, but what is callback function mean, and what are they used for.

Beside, what’s the differenct between glutKeyboardFunc and glutSpecialFunc

A callback function is a function which the library (GLUT) calls when it needs to know how to proccess something.
e.g. when glut gets a key down event it uses the glutKeybourdFunc callback routine to find out what to do with a key press.

This sort of thing is used also for live feedback scrolling (or at least it is on the mac, I don’t know wheather this is the same on Wintel machines), you call TrackControl (or something simmilar) and specify a routine to be called repeatedly to scroll the page.
I hope this answers your question .

I assume that you want to handle keyboard events. You may already have an idea for how to handle these events, for example when the user presses a particular key you want the camera to move a little bit forward.

OK, you must write a function to process the keyboard events, lets say you called it “processMyKeyboadEvents”. The next step is to tell GLUT that it should use that function when there is a keyboard event.

In technical terms you are “registering a callback”, meaning that you want GLUT to call that particular function to whenever a keyboard event occurs.

The glutKeyboardFunc and glutSpecialFunc functions from GLUT do just that, they let you specify which function will be responsible for keyboard events. So for instance if the function “processMyKeyboardEvents” is the one then you must call glutKeyboard with the name of the function as a parameter, i.e.


You’ll usually do this in the initialization of the application unless you want to change the way you handle keyboard events after starting the application.
If processMyKeyboardEvents is not defined then you’ll get an error eventually when a key is pressed.

The difference between glutKeyboardFunc and glutSpecialFunc is that the former deals with events generated by keys which have an ASCII code, for instance ‘a’, ‘1’, or even ’ ', whereas the latter (glutSpecialFunc), deals with the “special keys”, like F1-F12, Home, Up, etc…

You should also check glutGetModifiers which deals with SHIFT, CTRL and ALT keys, although this is not a callback registration function.

If you need more info you can check my GLUT tutorial at www.fatech.com/tech/opengl/glut

It covers both keyboard and mouse, as well as the required initializations. Source code is provided and examples are given for each of the functions above.

Hope this helps,

Antonio www.fatech.com/tech