Next: , Previous: , Up: Interfacing with the keyboard   [Contents][Index]

4.11.1 The basics of keys

No GUI is complete without a strong user interface and to interact with the user, a curses program should be sensitive to key presses or mouse actions done by the user. Let’s deal with keys first.

As you have seen in almost all of the above examples, it’s very easy to get key input from the user. A simple way of getting key presses is the getch procedure. The cbreak! mode should be enabled to read keys when you are interested in reading individual key hits rather than complete lines of text (which usually end with a CR). keypad! should be enabled to get the function keys, arrow keys and so on.

getch returns a character if the key pressed maps to a standard character or it returns an integer code for those keys that don’t map to characters, such as PAGE UP or DELETE. This integer code which can be matched with the KEY_ constants. For example, if the user presses F1, the integer returned is 265. The procedure (key-f 1) returns 265.

For example, if you call getch like this

(let ((x (getch win)))

getch will wait for the user to press a key, (unless you specified a timeout), and when the user presses a key, the corresponding character or integer is returned. If it is an integer, then you can check the value returned with the KEY_ constants or the result of the function key procedure key-f.

The following code piece will do that job

(let ((ch (getch win)))
    (if (eqv? ch KEY_LEFT)
        (addstr win "Left arrow is pressed")))

Let’s write a small program which creates a menu that can be navigated by up and down arrows.

Next: , Previous: , Up: Interfacing with the keyboard   [Contents][Index]