Ava Bahari Ava Bahari - 4 months ago 15
C Question

How write GLUT keyboard functions?

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();
}

Answer

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.

#include <stdio.h>
#include <GL/glut.h>

float red=1,green=1,blue=1;



void init (void)
{
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0,200.0,0.0,200.0);
}


void process_SHIFT_ALT_CTRL(unsigned char key, int x, int y) 
{
    // Press ALT or  SHIFT or  CTRL in combination with other keys.
    printf("key_code =%d  \n",key);

    int mod = glutGetModifiers();

    if (mod != 0) //ALT=4  SHIFT=1  CTRL=2
    {      
          switch(mod)
          {
             case 1 :  printf("SHIFT key %d\n",mod);  break;
             case 2 :  printf("CTRL  key %d\n",mod);  break;
             case 4 :  printf("ALT   key %d\n",mod);  break;
             mod=0;
          }
    }
}


void process_Normal_Keys(int key, int x, int y) 
{
     switch (key) 
    {    
       case 27 :      break;
       case 100 : printf("GLUT_KEY_LEFT %d\n",key);   break;
       case 102: printf("GLUT_KEY_RIGHT %d\n",key);  ;  break;
       case 101   : printf("GLUT_KEY_UP %d\n",key);  ;  break;
       case 103 : printf("GLUT_KEY_DOWN %d\n",key);  ;  break;
    }

}


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glFlush();
}


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

    printf("Press ALT or  SHIFT or  CTRL in combination with other keys. \n\n");

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowPosition(800,20);
    glutInitWindowSize(400, 300);
    glutCreateWindow("A simple example");
    init();

    // famous GLUT callback functions

    glutDisplayFunc(display);


    glutSpecialFunc( process_Normal_Keys );
    glutKeyboardFunc( process_SHIFT_ALT_CTRL );  


    glutMainLoop();

return 0;
}