免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 992 | 回复: 0
打印 上一主题 下一主题

《Linux 程序设计入门》 第四版 §6.4 键盘 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-13 16:10 |只看该作者 |倒序浏览

§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
        password = getch();
        if (password == '\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
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21908/showart_1717799.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP