View Full Version : Keyboard Input in GLUT

09-12-2001, 01:02 PM
I have just learnt to write simple OpenGL programs with GLUT. I wonder if in my glutDisplayFunc I can use C functions to wait for a keyboard input. I used some functions like getch(), ... between my OpenGL function calls but none of them function, although the scene showed up as I had written the code for.

09-12-2001, 02:11 PM
In main(), you have to register a callback to the keyboard function, the prototype for which is

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

Then write your own function keyboard, let's say,

keyboard(unsigned char key, int x, int y)
switch(key) {
case 'a':
// call a function
case 'b':
// call a function

glutPostRedisplay(); /* this redraws the scene without
waiting for the display callback so that any changes appear
instantly */

Hope this helps.