icybird_r 发表于 2004-4-7 21:24:17

请问linux有没有象getche那样的函数~~~

要求是如果键盘有输入那么立即输入,不会象cin等那样需要 enter才正式输入。
用tc的时候可以包涵头文件 conio,但在LINUX下没有这个头文件。郁闷啊~~!!

mozilla 发表于 2004-4-7 22:43:54

没有现成的函数,我有一个函数能实现这点,(ncurse有现成,但俺不喜欢那东东),明天给你一个例子

icybird_r 发表于 2004-4-8 09:06:48

谢斑竹~~~~

mozilla 发表于 2004-4-8 12:50:27


#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>
                                                                                                                                                
int Get_KeyValue()
{
      struct termios stored_settings;
      struct termios new_settings;
      int key_value;
      int key_press=0;
                                                                                                                                                
      tcgetattr(0,&stored_settings);
      new_settings = stored_settings;
      new_settings.c_lflag &= (~ICANON);
      new_settings.c_cc[VTIME] = 0;
      new_settings.c_cc[VMIN] = 1;
      tcsetattr(0,TCSANOW,&new_settings);
                                                                                                                                                
      ioctl(0,FIONREAD,&key_press);
      if(key_press)
      {
                key_value=getc(_IO_stdin);
      }
      else
      {
                key_value=0;
      }
      return key_value;
                                                                                                                                                
      tcsetattr(0,TCSANOW,&stored_settings);
                                                                                                                                                
}
main()
{
      int new_char=0;
      while(1)
      {
                new_char=Get_KeyValue();
                if(new_char)
                {
                        if(new_char=='q')
                              break;
                        printf("you press key:'%c'\n",new_char);
                }
                usleep(10000);
      }
}



DRIFT 发表于 2007-3-1 19:10:42

getch在windows中是在conio.h里的,不可移植的。

在linux下 :
#include <stdio.h>
#include <curses.h>

int main()
{
    initscr(); /* 开始curses模式 */
    getch();
    endwin(); /* 结束curses模式 */
}

gcc -o 1.out 1.c -lcurses
页: [1]
查看完整版本: 请问linux有没有象getche那样的函数~~~