Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19732818
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2008-12-13 16:10:14

§6.4  键盘

     键盘模式

 

#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

阅读(4086) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~