View Full Version : open GL keyboard func
blessman11
04-02-2011, 07:12 PM
How can I listen for the keys for UP, DOWN, LEFT and RIGHT?
When I ran a test on this using the function below, I fount it hard to display the keys and I don't know their ascii values.
Anybody help?
void KeyDown(unsigned char key, int x, int y)
{ cout <<key<<endl;
}
Alfonse Reinheart
04-02-2011, 07:35 PM
1: This is about GLUT, not OpenGL. They are not the same thing.
2: If you want to display the ASCII value of a key, you need to convert it into an integer with a cast operation.
enjoycrf
04-02-2011, 07:38 PM
use glut
im not sure if u know but
there are more than one function for each device sometimes
depending on what they do
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
/* MOUSE */
glutWarpPointer(mid_x, mid_y);
glutSetCursor(GLUT_CURSOR_NONE);
glutMouseFunc(mousePress);
glutMotionFunc(mouseMove);
glutPassiveMotionFunc(mouseMove); //check for mouse movement
/* KEYS */
glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardup);
glutSpecialFunc(specialkeyboard);
glutSpecialUpFunc(specialkeyboardup);
glutMainLoop();
void keyboardup(unsigned char key, int x, int y) {
//glutIgnoreKeyRepeat(1);
/* avoid thrashing this procedure */
usleep(100);
int tex = 1;
switch (key) {
case ESCAPE: // kill everything.
exit(1);
break; // redundant.
case 'a': case 'A': // switch the blending
ambOn = ambOn ? 0 : 1;
printf("amb is now: %d\n", ambOn);
break;
case 'd': case 'D': // switch the blending
difOn = difOn ? 0 : 1;
printf("dif is now: %d\n", difOn);
break;
/*
case 'b': case 'B': // switch the blending
blend = blend ? 0 : 1;
if (blend) {
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
} else {
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
}
printf("Blending is now: %d\n", blend);
break;
*/
case 't': case 'T': // switch the blending
tex = tex ? 0 : 1;
if (!tex) {
glEnable(GL_TEXTURE_2D);
} else {
glDisable(GL_TEXTURE_2D);
}
printf("Textures is now: %d\n", tex);
break;
case 'l': case 'L': // switch the lighting
light = light ? 0 : 1;
if (light) glEnable(GL_LIGHTING);
else glDisable(GL_LIGHTING);
printf("Lighting is now: %d\n", light);
break;
case '-': case '_': // switch the lighting
if (light_intensity <= 0) return;
light_intensity -= 0.1;
break;
case '=': case '+': // switch the lighting
if (light_intensity >= 1) return;
light_intensity += 0.1;
break;
case '{': case '[': // switch the lighting
if (light_id <= 0) return;
light_id -= 1;
break;
case '}': case ']': // switch the lighting
//if (light_id >= 1) return;
light_id += 1;
break;
/*
case '.': // switch the lighting
//if (light_id >= 1) return;
f0 += 5;
break;
case ',': // switch the lighting
//if (light_id >= 1) return;
f0 -= 5;
break;
case 'm': // switch the lighting
//if (light_id >= 1) return;
f1 += 5;
break;
case 'n': // switch the lighting
//if (light_id >= 1) return;
f1 -= 5;
break;
case 'b': // switch the lighting
//if (light_id >= 1) return;
f2 += 5;
break;
case 'v': // switch the lighting
//if (light_id >= 1) return;
f2 -= 5;
break;
*/
case 'v': case 'V': // switch the lighting
camera.perspective = camera.perspective ? 0 : 1;
printf("camera.perspective is now: %d\n", camera.perspective);
break;
case 'c': case 'C': // drop crate
dropCrate();
break;
default:
printf ("Key %d pressed. No action there yet.\n", key);
break;
}
keyboard_state = 0;
}
mhagain
04-02-2011, 07:43 PM
In other words: There is no OpenGL keyboard function. OpenGL does not handle keyboard input. OpenGL does not know the difference between a keyboard and an elephant. OpenGL does not know the difference between a keyboard and the planet Mars. OpenGL provides functions for interfacing with your graphics hardware, nothing else. Page 1, line 1 of any OpenGL manual. GLUT provides some simple keyboard functions. GLUT is a library built on top of OpenGL for the purposes of simplifying code in demos and tutorials. GLUT is not OpenGL.
This really should be a FAQ entry...
V-man
04-02-2011, 08:39 PM
http://www.opengl.org/wiki/FAQ#What_is_OpenGL.3F
enjoycrf
04-02-2011, 10:47 PM
omfg rub it in jfc
or u can get a dirt bike a street bike a skate board surf board and a snowboard one mountain bike a drumset and a bass guitar and get the f outside and enjoy the sun
jk
loling is ok
free country!
usa!
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.