I wrote the code below, but at runtime, when I press keys, the program doesn’t work.
I want to write a program that draws a triangle when the user presses right_key and alt_key. But this doesn’t work at all. It always shows a black screen.
#include <GL/glut.h>
void init (void)
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,200.0);
}
float red=1,green=1,blue=1;
void processSpecialKeys(int key, int x, int y) {
if(key=='0')
exit(0);
int mod;
if(key== GLUT_KEY_RIGHT) {
mod = glutGetModifiers();
if (mod==GLUT_ACTIVE_ALT) {
red = 1.0; green = 0.0; blue = 0.0;
glColor3f(red,green,blue);
glBegin(GL_TRIANGLES);
glVertex2f(0,0);
glVertex2f(0,100);
glVertex2f(50,100);
glEnd();
}
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,20);
glutInitWindowSize(400, 300);
glutCreateWindow("A simple example");
init();
glutDisplayFunc(display);
glutSpecialFunc(processSpecialKeys);
glutMainLoop();
}
OK it works now. I’ve modified your code to enable ALT or SHIFT or CTRL. The code prints out the scancodes of the keys.
To print the ALT, CTRL OR SHIFT scancode, simply press ALT or SHIFT or CTRL in combination with other keys.
Otherwse GLUT won’t register it.