View Full Version : how to manage the keyboard

03-18-2008, 03:34 AM
May I ask how to get the key I press and manage them?

Thanks for help

03-18-2008, 04:24 AM
OpenGL is a graphics API and has nothing to do with keyboard or mouse. It solely depends on the window library you are using. If you tell us which one you are using (GLUT, WinAPI, X11 etc.), someone will surely give you a link to a solution or o a correct forum.

03-18-2008, 09:13 AM
Try GLFW, it is great lib for game-like apps, and cross-platform.

03-25-2008, 02:54 AM
Using Glut it would be simple to add a keyboard handler function as follows, take notice of the bold areas.

#pragma warning(disable: 4068) //ignore warning given by MSVis about some opengl settings

#include <windows.h>
#include <math.h>
#include "gl.h"
#include "glu.h"
#include "glut.h"
#include "csc3406.h"

//used for glut init
//needed but don't really do anything!
int argc = 1;
char *argv = "";

FileReadWriter fileHandler;
WindowSetup settings;

void myInit(void)

glClearColor(1.0,1.0,1.0,0.0); // set white background color
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

//setup world coordinates
gluOrtho2D(settings.WORLD_LEFT, settings.WORLD_RIGHT,
settings.WORLD_BOTTOM, settings.WORLD_TOP);

//setup window coordinates
glViewport(settings.VIEW_LEFT, settings.VIEW_BOTTOM,
settings.VIEW_RIGHT - settings.VIEW_LEFT,
settings.VIEW_TOP - settings.VIEW_BOTTOM);

//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

glFlush(); //push the drawing out of the display buffer

void myKeyboard(unsigned char key, int mouseX, int mouseY) //The keyboard handler function
{ //you can use whatever mathod you like to react to the value of key I will use the fullscreen, window switch as an example
if (key=='f' || key=='F') // If press f
glutFullScreen(); // Fullscreen
if (key=='w' || key=='W') // If press w
glutReshapeWindow(640,480); // Window Mode
void myReshape(GLsizei W, GLsizei H)
if(settings.ASPECT_RATIO > W/H) //use global window aspect ratio
glViewport(0, 0, W, W/settings.ASPECT_RATIO);
glViewport(0, 0, H*settings.ASPECT_RATIO, H);

int main()
glutInit(&argc, &argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode

//load window setups from ini file
fileHandler.loadSetupFile("setup.ini", &settings);

glutInitWindowSize(settings.SCREENWIDTH, settings.SCREENHEIGHT); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("My First OpenGL Program"); // open the screen window and set the name
glutDisplayFunc(myDisplay); // register redraw function
glutReshapeFunc(myReshape); // register reshape function
glutKeyboardFunc(myKeyboard); // register keyboard function
glutMainLoop(); // go into a perpetual loop
return 1;

03-26-2008, 10:53 AM
I wonder if any one can tell me how to write the above program in c++, like what are the changes i should do in above sample code. Like I have class and i called
glutKeyboardFunc(classname::myKeyboard); // register keyboard function. Does this works? Any help is helpfull.