NCURSES Programming HOWTO

v2.1, 2024-09-08

Revision History
Revision 2.1 2024-09-08 Revised by: dickey
Fixes for the sample programs. Remove obsolete mailing addresses. Update publication date.
Revision 2.0 2022-12-03 Revised by: dickey
Fixes for the sample programs, Correct documentation errata.
Revision 1.9 2005-06-20 Revised by: ppadala
The license has been changed to the MIT-style license used by NCURSES. Note that the programs are also re-licensed under this.
Revision 1.8 2005-06-17 Revised by: ppadala
Lots of updates. Added references and perl examples. Changes to examples. Many grammatical and stylistic changes to the content. Changes to NCURSES history.
Revision 1.7.1 2002-06-25 Revised by: ppadala
Added a README file for building and instructions for building from source.
Revision 1.7 2002-06-25 Revised by: ppadala
Added "Other formats" section and made a lot of fancy changes to the programs. Inlining of programs is gone.
Revision 1.6.1 2002-02-24 Revised by: ppadala
Removed the old Changelog section, cleaned the makefiles
Revision 1.6 2002-02-16 Revised by: ppadala
Corrected a lot of spelling mistakes, added ACS variables section
Revision 1.5 2002-01-05 Revised by: ppadala
Changed structure to present proper TOC
Revision 1.3.1 2001-07-26 Revised by: ppadala
Corrected maintainers paragraph, Corrected stable release number
Revision 1.3 2001-07-24 Revised by: ppadala
Added copyright notices to main document (LDP license) and programs (GPL), Corrected printw_example.
Revision 1.2 2001-06-05 Revised by: ppadala
Incorporated ravi's changes. Mainly to introduction, menu, form, justforfun sections
Revision 1.1 2001-05-22 Revised by: ppadala
Added "a word about window" section, Added scanw_example.

This document is intended to be an "All in One" guide for programming with ncurses and its sister libraries. We graduate from a simple "Hello World" program to more complex form manipulation. No prior experience in ncurses is assumed. Send comments to this address


Table of Contents
1. Introduction
1.1. What is NCURSES?
1.2. What we can do with NCURSES
1.3. Where to get it
1.4. Purpose/Scope of the document
1.5. About the Programs
1.6. Other Formats of the document
1.6.1. Alternative formats
1.6.2. Building from source
1.7. Credits
1.8. Wish List
1.9. Copyright
2. Hello World !!!
2.1. Compiling With the NCURSES Library
2.2. Dissection
2.2.1. About initscr()
2.2.2. The mysterious refresh()
2.2.3. About endwin()
3. The Gory Details
4. Initialization
4.1. Initialization functions
4.2. raw() and cbreak()
4.3. echo() and noecho()
4.4. keypad()
4.5. halfdelay()
4.6. Miscellaneous Initialization functions
4.7. An Example
5. A Word about Windows
6. Output functions
6.1. addch() class of functions
6.2. mvaddch(), waddch() and mvwaddch()
6.3. printw() class of functions
6.3.1. printw() and mvprintw
6.3.2. wprintw() and mvwprintw
6.3.3. vw_printw()
6.3.4. A Simple printw example
6.4. addstr() class of functions
6.5. A word of caution
7. Input functions
7.1. getch() class of functions
7.2. scanw() class of functions
7.2.1. scanw() and mvscanw
7.2.2. wscanw() and mvwscanw()
7.2.3. vw_scanw()
7.3. getstr() class of functions
7.4. Some examples
8. Attributes
8.1. The details
8.2. attron() vs attrset()
8.3. attr_get()
8.4. attr_ functions
8.5. wattr functions
8.6. chgat() functions
9. Windows
9.1. The basics
9.2. Let there be a Window !!!
9.3. Explanation
9.4. The other stuff in the example
9.5. Other Border functions
10. Colors
10.1. The basics
10.2. Changing Color Definitions
10.3. Color Content
11. Interfacing with the key board
11.1. The Basics
11.2. A Simple Key Usage example
12. Interfacing with the mouse
12.1. The Basics
12.2. Getting the events
12.3. Putting it all Together
12.4. Miscellaneous Functions
13. Screen Manipulation
13.1. getyx() functions
13.2. Screen Dumping
13.3. Window Dumping
14. Miscellaneous features
14.1. curs_set()
14.2. Temporarily Leaving Curses mode
14.3. ACS_ variables
15. Other libraries
16. Panel Library
16.1. The Basics
16.2. Compiling With the Panels Library
16.3. Panel Window Browsing
16.4. Using User Pointers
16.5. Moving and Resizing Panels
16.6. Hiding and Showing Panels
16.7. panel_above() and panel_below() Functions
17. Menus Library
17.1. The Basics
17.2. Compiling With the Menu Library
17.3. Menu Driver: The work horse of the menu system
17.4. Menu Windows
17.5. Scrolling Menus
17.6. Multi Columnar Menus
17.7. Multi Valued Menus
17.8. Menu Options
17.9. The useful User Pointer
18. Forms Library
18.1. The Basics
18.2. Compiling With the Forms Library
18.3. Playing with Fields
18.3.1. Fetching Size and Location of Field
18.3.2. Moving the field
18.3.3. Field Justification
18.3.4. Field Display Attributes
18.3.5. Field Option Bits
18.3.6. Field Status
18.3.7. Field User Pointer
18.3.8. Variable-Sized Fields
18.4. Form Windows
18.5. Field Validation
18.6. Form Driver: The work horse of the forms system
18.6.1. Page Navigation Requests
18.6.2. Inter-Field Navigation Requests
18.6.3. Intra-Field Navigation Requests
18.6.4. Scrolling Requests
18.6.5. Editing Requests
18.6.6. Order Requests
18.6.7. Application Commands
19. Tools and Widget Libraries
19.1. CDK (Curses Development Kit)
19.1.1. Widget List
19.1.2. Some Attractive Features
19.1.3. Conclusion
19.2. The dialog
19.3. Perl Curses Modules CURSES::FORM and CURSES::WIDGETS
20. Just For Fun !!!
20.1. The Game of Life
20.2. Magic Square
20.3. Towers of Hanoi
20.4. Queens Puzzle
20.5. Shuffle
20.6. Typing Tutor
21. References

1. Introduction

In the olden days of teletype terminals, terminals were away from computers and were connected to them through serial cables. The terminals could be configured by sending a series of bytes. All the capabilities (such as moving the cursor to a new location, erasing part of the screen, scrolling the screen, changing modes, etc.) of terminals could be accessed through these series of bytes. These control sequences are usually called escape sequences, because they start with an escape(0x1B) character. Even today, with proper emulation, we can send escape sequences to the emulator and achieve the same effect on a terminal window.

Suppose you wanted to print a line in color. Try typing this on your console.

echo "^[[0;31;40mIn Color"

The first character is an escape character, which looks like two characters ^ and [. To be able to print it, you have to press CTRL+V and then the ESC key. All the others are normal printable characters. You should be able to see the string "In Color" in red. It stays that way and to revert back to the original mode type this.

echo "^[[0;37;40m"

Now, what do these magic characters mean? Difficult to comprehend? They might even be different for different terminals. So the designers of UNIX invented a mechanism named termcap. It is a file that lists all the capabilities of a particular terminal, along with the escape sequences needed to achieve a particular effect. In the later years, this was replaced by terminfo. Without delving too much into details, this mechanism allows application programs to query the terminfo database and obtain the control characters to be sent to a terminal or terminal emulator.

1.1. What is NCURSES?

You might be wondering, what the import of all this technical gibberish is. In the above scenario, every application program is supposed to query the terminfo and perform the necessary stuff (sending control characters, etc.). It soon became difficult to manage this complexity and this gave birth to 'CURSES'. Curses is a pun on the name "cursor optimization". The Curses library forms a wrapper over working with raw terminal codes, and provides highly flexible and efficient API (Application Programming Interface). It provides functions to move the cursor, create windows, produce colors, play with mouse, etc. The application programs need not worry about the underlying terminal capabilities.

So what is NCURSES? NCURSES is a clone of the original System V Release 4.0 (SVr4) curses. It is a freely distributable library, fully compatible with older version of curses. In short, it is a library of functions that manages an application's display on character-cell terminals. In the remainder of the document, the terms curses and ncurses are used interchangeably.

A detailed history of NCURSES can be found in the NEWS file from the source distribution. The current package is maintained by Thomas Dickey. You can contact the maintainers at bug-ncurses@gnu.org.

1.2. What we can do with NCURSES

NCURSES not only creates a wrapper over terminal capabilities, but also gives a robust framework to create nice looking UI (User Interface)s in text mode. It provides functions to create windows, etc. Its sister libraries panel, menu and form provide an extension to the basic curses library. These libraries usually come along with curses. One can create applications that contain multiple windows, menus, panels and forms. Windows can be managed independently, can provide 'scrollability' and even can be hidden.

Menus provide the user with an easy command selection option. Forms allow the creation of easy-to-use data entry and display windows. Panels extend the capabilities of ncurses to deal with overlapping and stacked windows.

These are just some of the basic things we can do with ncurses. As we move along, We will see all the capabilities of these libraries.

1.3. Where to get it

All right, now that you know what you can do with ncurses, you must be rearing to get started. NCURSES is usually shipped with your installation. In case you don't have the library or want to compile it on your own, read on.

Compiling the package

NCURSES can be obtained from

Read the README and INSTALL files for details on to how to install it. It usually involves the following operations.

    tar zxvf ncurses<version>.tar.gz  # unzip and untar the archive
    cd ncurses<version>               # cd to the directory
    ./configure                       # configure the build according to your
                                      # environment
    make                              # make it
    su root                           # become root
    make install                      # install it

1.4. Purpose/Scope of the document

This document is intended to be a "All in One" guide for programming with ncurses and its sister libraries. We graduate from a simple "Hello World" program to more complex form manipulation. No prior experience in ncurses is assumed. The writing is informal, but a lot of detail is provided for each of the examples.

1.5. About the Programs

All the programs in the document are available in gzipped form here. Ungzip and untar it. The directory structure looks like this.

ncurses
   |
   |----> JustForFun     -- just for fun programs
   |----> basics         -- basic programs
   |----> demo           -- output files go into this directory after make
   |          |
   |          |----> exe -- exe files of all example programs
   |----> forms          -- programs related to form library
   |----> menus          -- programs related to menus library
   |----> panels         -- programs related to panels library
   |----> perl           -- perl equivalents of the examples (contributed
   |                            by Anuradha Ratnaweera)
   |----> Makefile       -- the top level Makefile
   |----> README         -- the top level README file. contains instructions
   |----> COPYING        -- copyright notice

The individual directories contain the following files.

Description of files in each directory
--------------------------------------
JustForFun
    |
    |----> hanoi.c   -- The Towers of Hanoi Solver
    |----> life.c    -- The Game of Life demo
    |----> magic.c   -- An Odd Order Magic Square builder
    |----> queens.c  -- The famous N-Queens Solver
    |----> shuffle.c -- A fun game, if you have time to kill
    |----> tt.c      -- A very trivial typing tutor

  basics
    |
    |----> acs_vars.c            -- ACS_ variables example
    |----> hello_world.c         -- Simple "Hello World" Program
    |----> init_func_example.c   -- Initialization functions example
    |----> key_code.c            -- Shows the scan code of the key pressed
    |----> mouse_menu.c          -- A menu accessible by mouse
    |----> other_border.c        -- Shows usage of other border functions apa
    |                               -- rt from box()
    |----> printw_example.c      -- A very simple printw() example
    |----> scanw_example.c       -- A very simple getstr() example
    |----> simple_attr.c         -- A program that can print a c file with
    |                               -- comments in attribute
    |----> simple_color.c        -- A simple example demonstrating colors
    |----> simple_key.c          -- A menu accessible with keyboard UP, DOWN
    |                               -- arrows
    |----> temp_leave.c          -- Demonstrates temporarily leaving curses mode
    |----> win_border.c          -- Shows Creation of windows and borders
    |----> with_chgat.c          -- chgat() usage example

  forms
    |
    |----> form_attrib.c     -- Usage of field attributes
    |----> form_options.c    -- Usage of field options
    |----> form_simple.c     -- A simple form example
    |----> form_win.c        -- Demo of windows associated with forms

  menus
    |
    |----> menu_attrib.c     -- Usage of menu attributes
    |----> menu_item_data.c  -- Usage of item_name(), etc. functions
    |----> menu_multi_column.c    -- Creates multi columnar menus
    |----> menu_scroll.c     -- Demonstrates scrolling capability of menus
    |----> menu_simple.c     -- A simple menu accessed by arrow keys
    |----> menu_toggle.c     -- Creates multi valued menus and explains
    |                           -- REQ_TOGGLE_ITEM
    |----> menu_userptr.c    -- Usage of user pointer
    |----> menu_win.c        -- Demo of windows associated with menus

  panels
    |
    |----> panel_browse.c    -- Panel browsing through tab. Usage of user
    |                           -- pointer
    |----> panel_hide.c      -- Hiding and Un hiding of panels
    |----> panel_resize.c    -- Moving and resizing of panels
    |----> panel_simple.c    -- A simple panel example

  perl
    |----> 01-10.pl          -- Perl equivalents of first ten example programs

There is a top level Makefile included in the main directory. It builds all the files and puts the ready-to-use exes in demo/exe directory. You can also do selective make by going into the corresponding directory. Each directory contains a README file explaining the purpose of each c file in the directory.

For every example, I have included path name for the file relative to the examples directory.

All the programs are released under the same license that is used by ncurses (MIT-style). This gives you the ability to do pretty much anything other than claiming them as yours. Feel free to use them in your programs as appropriate.

1.6. Other Formats of the document

This howto is also available in other formats. Here are the links to other formats of this document.

1.6.2. Building from source

The sources for this HOWTO can be retrieved from

These tools were used to format the HOWTO and build the examples:

  • docbook-utils (a Debian package)

  • gcc

1.7. Credits

I thank Sharath and Emre Akbas for helping me with few sections. The introduction was initially written by Sharath. I rewrote it with few excerpts taken from his initial work. Emre helped in writing printw and scanw sections.

Perl equivalents of the example programs were contributed by Anuradha Ratnaweera.

Then comes Ravi Parimi, my dearest friend, who has been on this project before even one line was written. He constantly bombarded me with suggestions and patiently reviewed the whole text. He also checked each program on Linux and Solaris.

1.8. Wish List

This is the wish list, in the order of priority. If you have a wish or you want to work on completing the wish, mail me.

  • Add examples to last parts of forms section.

  • Prepare a Demo showing all the programs and allow the user to browse through description of each program. Let the user compile and see the program in action. A dialog based interface is preferred.

  • Add debug info. _tracef, _tracemouse stuff.

  • Accessing termcap, terminfo using functions provided by ncurses package.

  • Working on two terminals simultaneously.

  • Add more stuff to miscellaneous section.

1.9. Copyright

Copyright © 2001 by Pradeep Padala.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, distribute with modifications, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.