頭檔案
#include函式原型
int keypad(WINDOW *window, bool keypad_on);說明
keypad函式用來決定是否開啟和關閉轉義序列與邏輯鍵之間的轉換功能。該函式在調用成功的時候返回OK,失敗的時候返回ERR。將keypad_on參數設定為true,然後調用keypad函式來啟用keypad模式,在該模式中,curses將接管按鍵轉義序列的處理工作,讀鍵盤操作不僅能夠返回用戶按下的鍵,還能返回與邏輯按鍵對應的KEY_定義。
範例
1/* Having initialized the program and the curses library, we set the Keypad mode TRUE. */2
3 #include
4 #include
5 #include
6
7 #define LOCAL_ESCAPE_KEY 27
8
9 int main()
10 {
11 int key;
12
13 initscr();
14 crmode();
15 keypad(stdscr, TRUE);
16
17 /* Next, we must turn echo off
18 to prevent the cursor being moved when some cursor keys are pressed.
19 The screen is cleared and some text displayed.
20 The program waits for each key stroke
21 and, unless it's q, or produces an error, the key is printed.
22 If the key strokes match one of the terminal's keypad sequences,
23 then that sequence is printed instead. */
24
25 noecho();
26
27 clear();
28 mvprintw(5, 5, "Key pad demonstration. Press 'q' to quit");
29 move(7, 5);
30 refresh();
31
32 key = getch();
33 while(key != ERR && key != 'q') {
34 move(7, 5);
35 clrtoeol();
36
37 if ((key >= 'A' && key <= 'Z') ||
38 (key >= 'a' && key <= 'z')) {
39 printw("Key was %c", (char)key);
40 }
41 else {
42 switch(key) {
43 case LOCAL_ESCAPE_KEY: printw("%s", "Escape key"); break;
44 case KEY_END: printw("%s", "END key"); break;
45 case KEY_BEG: printw("%s", "BEGINNING key"); break;
46 case KEY_RIGHT: printw("%s", "RIGHT key"); break;
47 case KEY_LEFT: printw("%s", "LEFT key"); break;
48 case KEY_UP: printw("%s", "UP key"); break;
49 case KEY_DOWN: printw("%s", "DOWN key"); break;
50 default: printw("Unmatched - %d", key); break;
51 } /* switch */
52 } /* else */
53
54 refresh();
55 key = getch();
56 } /* while */
57
58 endwin();
59 exit(EXIT_SUCCESS);
60 }
[root@localhost keypad]# gcc -o keypad keypad.c -lcurses
[root@localhost keypad]# ./keypad