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).
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
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 corresponds to the key pressed by the user, and it can have two formats:
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_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
. 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
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.
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
special be the name of our
specialFunc callback procedure. Let's declare it in the
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 ...
SpecialAction is the same thing as
SpecialKey can be:
KeyF1, KeyF2, KeyF3, KeyF4, KeyF5, KeyF6, KeyF7, KeyF8, KeyF9, KeyF10, KeyF11, KeyF12, KeyLeft, KeyUp, KeyRight, KeyDown, KeyPageUp, KeyPageDown, KeyHome, KeyEnd or
The following example finishes successfully a program when the Home key is pressed:
special :: SpecialAction special KeyHome _ = exitWith ExitSuccess special _ _ = return ()
specialFunc are defined in module GLUT_CBWindow, which is automatically imported if your program already imports module GLUT.