分类: LINUX
2008-12-15 09:54:37
小键盘中的字符要用字符串来表示,且不通的终端还有不通表示方法,识别起来要困难一些。Curses把它存储在terminfo中,curses.h定义了KEY_来表示逻辑键。
序列到逻辑键转换的开启:
#include
int keypad(WINDOW
*window_ptr, bool keypad_on);
使用keypad模式的3个注意点:
❑转移序列有时间依赖性,注意网络影响。
❑ 注意按键适当的时间间隔
❑ 同样的多个键可能有不通功能。
# cat keypad.c
/* Having initialized the program and the curses
library, we set the Keypad mode TRUE. */
#include
#include
#include
#define
LOCAL_ESCAPE_KEY 27
int main()
{
int key;
initscr();
crmode();
keypad(stdscr, TRUE);
/* Next, we must turn echo off
to prevent the cursor being moved when some
cursor keys are pressed.
The screen is cleared and some text
displayed.
The program waits for each key stroke
and, unless it's q, or produces an error,
the key is printed.
If the key strokes match one of the
terminal's keypad sequences,
then that sequence is printed instead. */
noecho();
clear();
mvprintw(5, 5, "Key pad demonstration.
Press 'q' to quit");
move(7, 5);
refresh();
key = getch();
while(key != ERR && key != 'q') {
move(7, 5);
clrtoeol();
if ((key >= 'A' && key <=
'Z') ||
(key >= 'a' && key <=
'z')) {
printw("Key was %c",
(char)key);
}
else {
switch(key) {
case LOCAL_ESCAPE_KEY:
printw("%s", "Escape key"); break;
case KEY_END:
printw("%s", "END key"); break;
case KEY_BEG:
printw("%s", "BEGINNING key"); break;
case KEY_RIGHT:
printw("%s", "RIGHT key"); break;
case KEY_LEFT: printw("%s",
"LEFT key"); break;
case KEY_UP: printw("%s",
"UP key"); break;
case KEY_DOWN:
printw("%s", "DOWN key"); break;
default: printw("Unmatched -
%d", key); break;
} /* switch */
} /* else */
refresh();
key = getch();
} /* while */
endwin();
exit(EXIT_SUCCESS);
}