PDA

View Full Version : Problem with glut key press callback function



p.luo
10-27-2008, 05:23 AM
I registed the keyboard callback function with glutKeyboardFunc and glutSpecialFunc,but I found that: when I pressed a key, sometimes it was normal while other times it didnot response. Can somebody explain why this happened?

I use VS.2008 glut3.7 Windows XP SP2

code:

#include "GL/glut.h"
#include <iostream>
using namespace std;

int i=0;

int f=false;
//the window to be hide
int handle;

void cb_keyboard(unsigned char key,int x,int y){
cout<<"key:"<<key<<" pressed"<<endl;
}

void cb_sk(int key,int x,int y){
switch (key)
{
case GLUT_KEY_F2:
if(!f){
glutSetWindow(handle);
glutHideWindow();
}else{
glutSetWindow(handle);
glutShowWindow();
}
f=!f;
cout<<"F2 pressed"<<endl;
break;
}
}

void renderScene(void) {
cout<<"GL rendering:"<<i++<<endl;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glFlush();
}

int main(int argc, char **argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
//parent window
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
int p=glutCreateWindow("3D Tech- GLUT Tutorial");
glutDisplayFunc(renderScene);
//sub window
handle=glutCreateSubWindow(p,0,0,160,160);
glutDisplayFunc(renderScene);
glutKeyboardFunc(cb_keyboard);
glutSpecialFunc(cb_sk);
glutMainLoop();
return 0;
}