You have to use some platform-specific code to do it. There is no way around it.
If you are using Windows, the stuff in <conio.h> is a perfectly acceptable option, though I would still encourage you to use the Windows Console Functions directly
(instead of the ancient conio stuff).
Either will suit your purpose. However, if you plan to do this for cross-platform code, check out
A getting started guide:
It looks scary at first but is really very simple. The basic setup is to initialize the terminal, then do your drawing/reading, and finalize the terminal before quitting. A simple example:
initscr(); // enable curses
raw(); // get direct keyboard input (not line-buffered)
(void)noecho(); // don't echo keys to screen
nonl(); // don't translate newlines
intrflush( stdscr, FALSE ); // don't process interrupt keys (like Ctrl-C)
(void)keypad( stdscr, TRUE ); // read extended keys (like arrow keys)
// start_color(); // (if you plan to use text attributes)
// draw stuff
wclear( stdscr );
wmove( stdscr, /*row*/ 5, /*column*/ 10 );
waddstr( stdscr, (char*)"Hello, world!" );
wmove( stdscr, 6, 10 );
wprintw( stdscr, (char*)"There are %d lines and %d columns.", LINES, COLS );
wmove( stdscr, 10, 0 );
waddch( stdscr, '?' );
wrefresh( stdscr ); // this makes sure the stuff is displayed
// get input
ch = wgetch( stdscr );
/* do what you will with ch here */
Basic extended/special key codes are:
KEY_DC // delete key
KEY_IC // insert key
KEY_NPAGE // pgdn/next key
KEY_PPAGE // pgup/prior key
KEY_F( n ) // function keys. n = 1, 2, 3, ...
You can find others in the <curses.h> header.
key is not
defined there, since a lot of keyboards use it to start multi-byte key sequences. You can
#define KEY_ESC 27
to use it.
A lot of the output commands (like waddch
()) can be combined with the move commands:
mvwaddch( y, x, c )
You can type "man 3x curses
" and "man 3x addch
" for documentation on the web.
Wow, bigger response than I meant....
Hope this helps.
Oh, before I forget, those casts in initialize() get rid of some warning messages when using PDCurses, since some of those functions are actually macros that translate into sequenced statements (stuff like
foo(), baz = 12, quux();
 Removed a call to a routine I
wrote that got in there by accident. Also added the cast on line 31 to get rid of compiler warnings.