Keyboard and Special Keys
(updated in 26/05/2001)

Previous | Index | Next

With this lesson you will start to learn how to make your HOpenGL program deal with the input provided by the user. At first, we'll talk about keyboard input, and then about special keys input (such as Home, End, F1, F2 and so on).



Keyboard:

As you must have seen before, the keyboard input, in HOpenGL, is managed by a callback procedure called keyboardFunc, that must be declared in the main function (check out the Your First (and Simple) Program lesson if you still have any doubt about callback procedures). Let keyboard be the name of our callback procedure that will manage keyboard routines. The following code line must be included in the main function:

	keyboardFunc (Just keyboard)

Notice that the keyboardFunc function is Maybe, while the displayFunc function is not. That's why the Just is necessary. Now let's understand how our function keyboard must be structured:

	keyboard :: KeyboardAction
	keyboard key Window_Position = procedure_1
	keyboard key Window_Position = procedure_2
	...

As you can see, KeyboardAction is the same thing as Char -> WindowPosition -> IO (). The Char corresponds to the key pressed by the user, and it can have two formats:

  • 'x' (where x is a character) or
  • '\num' (where num is a decimal number from ASCII code).

For example, the key n can be represented by both 'n' or '\110'. Check out the following pseudo-code:

	keyboard :: KeyboardAction
	keyboard 'n' _ = procedure_1
	keyboard 'k' _ = procedure_2
	keyboard 'y' _ = procedure_3
	

This program calls procedure_1, procedure_2 or procedure_3, if the user presses the keys n, k or y, respectively. The following program is equivalent:

	keyboard :: KeyboardAction
	keyboard '\110' _ = procedure_1
	keyboard '\107' _ = procedure_2
	keyboard '\121' _ = procedure_3
	

Do not worry about the WindowPosition field by now (observe that it is ignored in all examples of this lesson).

Let me show you a very interesting example: a program that ends successfully when the ESC key is pressed. In order to create it, you need to know that the ASCII code for this key in decimal is 27 (so it will be allowed for us to use '\27' to represent this key). The command used to quit successfully from a program is exitWith ExitSuccess. Hence, the following program fit our purposes:

	keyboard :: KeyboardAction
	keyboard '\27' _ = exitWith ExitSuccess
	keyboard _     _ = return ()

Notice that the keyboard function above simply doesn't do anything if the user presses a key other than ESC. You need to do the following in order to be able to use the function exitWith:

	import System(ExitCode(..), exitWith)

If you wish, include this keyboard function we created above (and its declaration in the main function) in the examples we developed in the previous lessons, and watch the result.



Special Keys:

Once you understand how to deal with the common keys of the keyboard, it is very easy to get in touch with the special keys. The callback procedure that deals with them is the specialFunc. Let special be the name of our specialFunc callback procedure. Let's declare it in the main function:

	specialFunc (Just special)

It must have the following format:

	special :: SpecialAction
	special Special_Key Window_Position = procedure_1
	special Special_Key Window_Position = procedure_2
	...

Notice that SpecialAction is the same thing as SpecialKey -> WindowPosition -> IO (). A SpecialKey can be: KeyF1, KeyF2, KeyF3, KeyF4, KeyF5, KeyF6, KeyF7, KeyF8, KeyF9, KeyF10, KeyF11, KeyF12, KeyLeft, KeyUp, KeyRight, KeyDown, KeyPageUp, KeyPageDown, KeyHome, KeyEnd or KeyInsert.

The following example finishes successfully a program when the Home key is pressed:

	special :: SpecialAction
	special KeyHome _ = exitWith ExitSuccess
	special _       _ = return ()

Both keyboardFunc and specialFunc are defined in module GLUT_CBWindow, which is automatically imported if your program already imports module GLUT.



Hints and Tips:


Downloads:
  • Source code of the first example (keyboard management) you've learned in this module.
  • Source code of the second example (special keys management) you've learned in this module.


HOpenGL Tutorial - Andre W B Furtado
Keyboard and Special Keys
www.cin.ufpe.br/~haskell/hopengl/keyboard.html
Last updated in 26/05/2001
Informatics Center (CIn) - UFPE
Recife - PE - Brazil