1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <string>
#include <curses.h>
std::string name_key( int c ) {
// This function takes a keycode and turns it into a string.
// I'm pretty sure there is a curses function to do this,
// but I'm not going to look it up right now... (sorry)
std::string result;
if (c < 32) { // control keys
result = "^";
result += (char)c +'A' -1;
}
else if (c == 127) result = "^?";
else if (c > 127) result = "FN"; // any function key (arrows, F1, Cut, etc)
else result = c; // normal alphanumeric key
return result;
}
int main() {
std::string s = "Press the 'any' key...";
int c;
// Initialze
initscr();
raw(); // line-buffering off, echo off, etc.
nonl(); // NL conversions off
intrflush( stdscr, FALSE ); // we want ^C, etc...
(void) keypad( stdscr, TRUE ); // extended keys ON
curs_set( 1 ); // make sure the cursor is visible
// clear the screen
wclear( stdscr );
// move the cursor to (x, y) = (10, 5)
wmove( stdscr, 5, 10 );
// display our message (curses routines are not prototyped with const)
waddstr( stdscr, const_cast<char*>(s.c_str()) );
// update the screen so that our changes are visible
wrefresh( stdscr );
// wait for the user to press a single key
c = wgetch( stdscr );
// complain that the user did not press the 'any' key
wmove( stdscr, 7, 10 );
s = std::string( "Hey, you pressed the '" ) +name_key( c ) +"' key!";
waddstr( stdscr, const_cast<char*>(s.c_str()) );
// wait for the user to quit
wmove( stdscr, 20, 0 );
waddstr( stdscr, "Press any key to quit..." );
wrefresh( stdscr );
wgetch( stdscr );
// restore the terminal to its proper state and quit
endwin();
return EXIT_SUCCESS;
}
|