|
Tutorials > OpenGL ES > Keyboard Input
IntroductionIn many of your programs you will need the option of accepting keyboard input. This tutorial will explain how to capture messages sent from the keyboard. If you are using GLUT|ES, please consult the GLUT on how to make use of keyboard input with OpenGL ES. The code is almost exactly the same. Also make sure that you download the source code at the bottom of this page. The problem with the GLUT|ES library is that the entire screen is taken up by the OpenGL ES window, which hides the keyboard popup button. This will most likely result in you not using keyboard input much. Contents of main.cpp : The first step is to create a function that will process all keyboard input. This function must accept certain parameters. The first parameter must accept a UGWindow variable. The second parameter must be an integer. This specifies what key has been pressed. The third and fourth parameters must also both be integers. These specify the x and y value of the pointing device when the key was pressed. void keyboard(UGWindow uwin, int key, int x, int y) { We check to see what key has been pressed.
switch(key)
{
You can compare the key variable to any character as shown below. If a lower case q is pressed, the program will exit. case 'q' : exit(0); break; Special keys are also available and are listed in the table below :
Below, we exit the program if the up arrow is pressed. case UG_KEY_UP : exit(0); break; } } The window needs to know what function we are using for processing key presses. We therefore place a call to the ugKeyboardFunc function along with the ugDisplayFunc function. This function takes the handle to the OpenGL window as its first parameter and the keyboard function as its second. ugKeyboardFunc(uwin, keyboard); You should now be able to process keyboard messages. If you run this program, you will still not see any graphics occur, but you can quit the program by either pressing the q key or the up arrow. Please let me know of any comments you may have : Contact Me
Last Updated : 20 November 2005
All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005 Read the Disclaimer |
|