Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:22:00+00:00 2026-05-27T17:22:00+00:00

I wrote the code below, but at runtime, when I press keys, the program

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:22:00+00:00Added an answer on May 27, 2026 at 5:22 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote the code below but the last printf return 0 for sumofall how
I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
I write the code below to delete a file: FileInfo file = new FileInfo(filename);
I use the below code to write code to query a web method in
I wrote some code with a lot of recursion, that takes quite a bit
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I have write below code String strId = 1,2 try { myDB = myDbHelper.openDataBase();
in the code below i want to implement the floyd algorithm,the user enter some
How can I check for null values in JavaScript? I wrote the code below
I have difficulties calculating percentage of a double value. I wrote the code below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.