Menu
OpenGL ES Tutorials

OpenGL ES

Keyboard Input



Tutorials > OpenGL ES > Keyboard Input

View Full Source

Introduction

Keyboard Input In 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 :

Identifier Description
UG_KEY_F1 - UG_KEY_F2 F1 through F12 keys.
UG_KEY_LEFT Left arrow
UG_KEY_RIGHT Right arrow
UG_KEY_UP Up arrow
UG_KEY_DOWN Down arrow
UG_KEY_PAGE_UP Page Up key
UG_KEY_PAGE_DOWN Page Down key
UG_KEY_HOME Home key
UG_KEY_END End key
UG_KEY_INSERT Insert key

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

GLUT|ES Source Files : Embedded Visual C++ 4.0
UG Source Files : Embedded Visual C++ 4.0

Last Updated : 20 November 2005


< Tutorial 03 - OpenGL Window Tutorial 05 - Mouse Input >

Back to Top


All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer

Links