Home Basic OpenGL functions Keyboard and mouse
Keyboard and Mouse

Remember how I told you GLUT is an event based framework? If you want to be notified of anything (like a key press) all you need to do is tell glut "call my function when somebody presses a key!".

The name doesn't matter but you have to to declare it with the right signature:

void keyboard(unsigned char key, int x, int y)

{

switch (key) {

case 27: // 27 is the code for the ESC key.
exit(0);
break;
}
}

 

Then before giving control to glut do this call:

glutKeyboardFunc(keyboard)

 

When the user presses a key the function will be called and parameter "key" will contain the ascii code of the key. This works with normal keys that generate only one scan code but for special key you need a different function:

void keyspecial( int key, int x, int y )

void keyspecial( int key, int x, int y )
{

if( key == GLUT_KEY_PAGE_UP) // Page up
{
// ...... do what ever you want to do
glutPostRedisplay(); // redraw everything to reflect the changes

}
if (key == GLUT_KEY_PAGE_DOWN)
{

// ...... do what ever you want to do
glutPostRedisplay();// redraw everything to reflect the changes
}
if (key == GLUT_KEY_HOME)
{
// ...... do what ever you want to do
glutPostRedisplay();// redraw everything to reflect the changes
}
if (key == GLUT_KEY_END)
{
// ...... do what ever you want to do
glutPostRedisplay();// redraw everything to reflect the changes
}

 

}

Hook it up with glut using:

glutSpecialFunc(keyspecial);

 

Mouse handleing is just as easy. For the mouse you have the following callbacks:

glutMouseFunc(processMouse); // this one is to catch mouse clicks and is only called when a click has occured

glutMotionFunc(processMouseActiveMotion); // this
glutPassiveMotionFunc(processMousePassiveMotion);
glutEntryFunc(processMouseEntry);

 

 

OpenGL Example program

 

// if you are not using Visual Studio to compile this then remove stdafx.h

#include <stdafx.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>

void init(void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_SMOOTH);

}


void display(void)
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Loading the Identity matrix means we reset the screen coordinate system to XYZ axis of lenght 1:
The screen starts at z=0, x=-1 to x=1 and y=-1 to y=1 */
glLoadIdentity ();

glTranslatef(0,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd(); // Done Drawing A Triangle

Sleep(5);
glutSwapBuffers();


}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);

}

void keyboard(unsigned char key, int x, int y)
{

switch (key) {
case 27:
exit(0);
break;
}

}



void keyspecial( int key, int x, int y )
{

if( key == GLUT_KEY_PAGE_UP) // Page up
{
// ...... do what ever you want to do
glutPostRedisplay(); // redraw everything to reflect the changes

}
if (key == GLUT_KEY_PAGE_DOWN)
{

// ...... do what ever you want to do
glutPostRedisplay();// redraw everything to reflect the changes
}
if (key == GLUT_KEY_HOME)
{
// ...... do what ever you want to do
glutPostRedisplay();// redraw everything to reflect the changes
}
if (key == GLUT_KEY_END)
{
// ...... do what ever you want to do
glutPostRedisplay();// redraw everything to reflect the changes
}

 

}

int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard); // tell glut to call this function when the user presses a key
glutSpecialFunc(keyspecial); // tell glut to call this function when the user presses a special a key
glutMainLoop();
return 0;

}

 
Copyright © 2010 Tudor Carean. All Rights Reserved.