C语言 不按回车键就能得到一个字符

1:在windows平台上,使用conio.h 函数库

getch直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch就立刻返回,getch返回值是用户输入的ASCII码,出错返回-1.输入的字符不会回显在屏幕上.

2:linux上没有conio.h 函数库,但是可以

可以使用curses.h 函数库

如何开始我的第一个 curses 程式:

开始使用 curses 的一切命令之前, 您必须先利用 initscr()这个函式来开启 curses 模式.

相对的, 在结束 curses 模式前 ( 通常在您结束程式前 ) 也必须以endwin()来关闭 curses 模式.

#include <stdio.h>
#include <curses.h>
int main()
{
   initscr();
    char ch;
    int i;
    while(1){
        ch=getch();
        printf("%c",ch);
        fflush(stdout);
    }
    endwin();
    return 0;

}

 在<c专家编程>中,提供了如下方法,同样可以实现该功能

 1 #include<stdio.h>
 2 
 3 int main()
 4 
 5 {
 6 
 7   int c;
 8 
 9   system("stty raw");
10 
11   c=getchar();
12 
13   system("stty cooked");
14 
15   return 0;
16 
17 }

 

posted @ 2017-08-11 09:10  颜小雀  阅读(2477)  评论(0编辑  收藏  举报