OpenGL-GLUT tutorial (5) glut keyboard control

Source: Internet
Author: User

GLUT Tutorial Keyboard InputGLUT allows us to write programs and add keyboard input control, including common keys and other special keys (such as F1 and up ). In this chapter, we will learn how to check which key is pressed, what information can be obtained from glut, and how to process keyboard input. Now, you should note that as long as you want to control the processing of an event, you must tell glut in advance which function will complete this task. So far, we have used glut to tell the window system which rendering function we want to call when the window is re-painted, but when the system is idle, which function is called. And which function will be called when the window size changes. Similarly, we must do the same thing to process key messages. We must use glut to notify the window system which function will complete the required operation when a key is pressed. We also call a callback function for function registration. After you press the next key, Glut provides two functions to register callback for the Keyboard Message. The first one is glukeyboardfunc. This function tells the window system which function will be called to process common key messages. A common key is a letter, number, or other key that can be expressed in ASCII code. The function prototype is as follows: void glukeyboardfunc (void (* func) (unsigned char key, int X, int y); parameter: FUNC: name of the function that processes the common button message. If null is passed, the glut ignores the normal button message. The function that serves as the parameter of the function of the glukeyboardfunc requires three parameters. The first one indicates the ASCII code of the key to be pressed, and the other two provide the current mouse position when the key is pressed. The cursor position is relative to the upper left corner of the current customer window. It is often used to exit the application when the Escape key is pressed. Note: As we mentioned, the glumainloop function produces a endless loop. The only way to exit the loop is to call the system exit function. This is what our function is going to do. When you press the Escape key to call the exit function to terminate the application (remember to include the header file stdlib. h In the source code ). The code for this function is as follows: void processnormalkeys (unsigned char key, int X, int y) {If (Key = 27) Exit (0 );} next let's take control of the key-specific messages. GLUT provides the function gluspecialfunc so that you can register your function when a message with a special key is pressed. The function prototype is as follows: void gluspecialfunc (void (* func) (INT key, int X, int y); parameter: FUNC: the name of the function that processes the message by pressing the special key. If null is passed, the glut ignores the special key message. Next we will write a function to change the color of our triangle when some special keys are pressed. This function sets the triangle to red when the F1 key is pressed, green when the F2 key is pressed, and blue when the F3 key is pressed.
void processSpecialKeys(int key, int x, int y) {
 
         switch(key) {
                 case GLUT_KEY_F1 : 
                                   red = 1.0; 
                                   green = 0.0; 
                                   blue = 0.0; break;
                 case GLUT_KEY_F2 : 
                                   red = 0.0; 
                                   green = 1.0; 
                                   blue = 0.0; break;
                 case GLUT_KEY_F3 : 
                                   red = 0.0; 
                                   green = 0.0; 
                                   blue = 1.0; break;
         }
}
The above maid has been predefined as a constant in glut. h. The constants are as follows:
GLUT_KEY_F1               F1 function key
GLUT_KEY_F2               F2 function key
GLUT_KEY_F3               F3 function key
GLUT_KEY_F4               F4 function key
GLUT_KEY_F5               F5 function key
GLUT_KEY_F6               F6 function key
GLUT_KEY_F7               F7 function key
GLUT_KEY_F8               F8 function key
GLUT_KEY_F9               F9 function key
GLUT_KEY_F10              F10 function key
GLUT_KEY_F11              F11 function key
GLUT_KEY_F12              F12 function key
GLUT_KEY_LEFT             Left function key
GLUT_KEY_RIGHT            Up function key
GLUT_KEY_UP               Right function key
GLUT_KEY_DOWN             Down function key
GLUT_KEY_PAGE_UP          Page Up function key
GLUT_KEY_PAGE_DOWN        Page Down function key
GLUT_KEY_HOME             Home function key
GLUT_KEY_END              End function key
GLUT_KEY_INSERT           Insert function key
To allow the preceding processspecialkeys function to pass compilation, we must also define three variables, red, green, and blue. In addition, to get the desired result, we must modify the renderscene function.
...
// All variables are initialized to 1, indicating that the triangle is white at the beginning.
float red=1.0, blue=1.0, green=1.0;
 
 
 
void renderScene(void) {
         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
         glPushMatrix();
         glRotatef(angle,0.0,1.0,0.0);
 
         // glColor3fSet the color of the triangle to be drawn.
         glColor3f(red,green,blue);
 
         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();
         glPopMatrix();
         angle++;
         glutSwapBuffers();
}
. Next we should tell glut that the function we just defined is used to process and press the message. That is, you can call the functions of glukeyboardfunc and gluspecialfunc. We call them in the main function. The following is the latest main function. The vcss project can be downloaded here (glu3.zip) Ctrl, ALT And shift  Sometimes we want to know how to deal with a modifier key, that is, Ctrl, ALT, or shift. GLUT provides a function to detect when a key combination is pressed. This function can only be called in the function for processing key messages or mouse messages. The function prototype is as follows: int glugetmodifiers (void); the return value of this function is one of the three pre-defined constants in glut. H, or their or combinations. The three constants are: 1: glu_active_shift: return it. When you press shift or Caps Lock, note that this value is not returned when both are pressed. 2: grant _ active_ctrl: return it. Press ctrl. 3: Maid: returns it when the ATL key is pressed. Note that the window system may intercept some modifiers, so no callback occurs. Now let's expand processnormalkeys to process the key combination. When you press the r key, the red variable is set to 0.0. When you press ATL + R, the red variable is set to 1.0. The Code is as follows:
void processNormalKeys(unsigned char key, int x, int y) {
 
         if (key == 27) 
                 exit(0);
         else if (key=='r') {
                 int mod = glutGetModifiers();
                 if (mod == GLUT_ACTIVE_ALT)
                          red = 0.0;
                 else
                          red = 1.0;
         }
}
Note that if we press the r key, nothing will happen, because the ASCII code of the r key is different from that of the r key. That is, two different keys. Finally, how can I detect pressing CTRL + ALT + F1 ?. In this case, we must detect two key combinations at the same time. To complete the operation, we need to use or operators. The following code snippet changes the color to red when you press CTRL + ALT + F1.
void processSpecialKeys(int key, int x, int y) {
 
         int mod;
         switch(key) {
                 case GLUT_KEY_F1 : 
                    mod = glutGetModifiers();
                    if (mod == (GLUT_ACTIVE_CTRL|GLUT_ACTIVE_ALT)) {
                          red = 1.0; green = 0.0; blue = 0.0;
                    }
                    break;
                 case GLUT_KEY_F2 : 
                    red = 0.0; 
                    green = 1.0; 
                    blue = 0.0; break;
                 case GLUT_KEY_F3 : 
                    red = 0.0; 
                    green = 0.0; 
                    blue = 1.0; break;
         }
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.