分类: LINUX
2008-12-13 16:10:14
*
键盘模式
#include
int echo(void);
int noecho(void);
int cbreak(void);
int nocbreak(void);
int raw(void);
int noraw(void);
函数说明:
The
two echo functions simply turn the echoing of typed characters on and off. The
remaining four
function
calls control how characters typed on the terminal are made available to the curses
program.
Cbreak模式会立即读取字符,cooked模式则按回车后菜读取。Raw关闭特殊字符处理,Calling nocbreak
sets the input mode
back to cookedmode, but leaves special character processing unchanged; calling noraw
restores both cooked
mode andspecial character handling.
*
键盘输入
#include
int getch(void);
int getstr(char *string);
int getnstr(char *string, int number_of_characters);
int scanw(char *format, ...);
和getchar, gets,
and scanf类似,不过getstr无法控制字符长度。Getnstr则可以控制长度。
实例:键盘模式和输入:
# cat ipmode.c
/* First, we set up the program and the initial
curses calls. */
#include
#include
#include
#include
#define PW_LEN 256
#define NAME_LEN 256
int main() {
char name[NAME_LEN];
char password[PW_LEN];
const char *real_password =
"xyzzy";
int i = 0;
initscr();
move(5, 10);
printw("%s", "Please
login:");
move(7, 10);
printw("%s", "User name:
");
getstr(name);
move(8, 10);
printw("%s", "Password:
");
refresh();
/* When the user enters their password, we need
to stop the password being echoed to the screen.
Then we check the password against
xyzzy. */
cbreak();
noecho();
memset(password, '\0',
sizeof(password));
while (i < PW_LEN) {
password[i] = getch();
if (password[i] == '\n') break;
move(8, 20 + i);
addch('*');
refresh();
i++;
}
/* Finally, we re-enable the keyboard echo and
print out success or failure. */
echo();
nocbreak();
move(11, 10);
if (strncmp(real_password, password,
strlen(real_password)) == 0)
printw("%s",
"Correct");
else printw("%s",
"Wrong");
printw("%s", "
password");
refresh();
sleep(2);
endwin();
exit(EXIT_SUCCESS);
}
运行结果:
# ./ipmode
Please login:
User name: root
Password: *****
Correct password