2010-06-06 77 views
14

我正在使用Linux控制台,我想做一个程序,输出随机字符,直到ESC被按下。我怎样才能做出这样的键盘处理程序?如何处理在C中的Linux控制台按下的键?

+0

相似和/或相关 - [C无阻塞键盘输入](http://stackoverflow.com/q/448944/203667) – jschmier 2011-01-10 16:00:43

回答

2

getch()可能来自Curses图书馆吗?此外,您将需要使用notimeout()来告诉getch()不要等待下一个按键。

+0

你应该明确提到你在谈论(N)curses库。 – 2010-06-06 13:56:29

+0

是的,当然。更新。谢谢。 – 2010-06-06 14:18:06

+2

注意:来自ncurses的getch()需要正确的ncurses“屏幕”进行初始化,否则它将无法工作。 – ShinTakezou 2010-06-06 17:56:55

-3
#include <stdio.h> 

#include <stdlib.h> 

#include <unistd.h> 

#include <signal.h> 

char * me = "Parent"; 

void sigkill(int signum) 
{ 
    //printf("=== %s EXIT SIGNAL %d ===\n", me, signum); 
    exit(0); 
} 

main() 
{ 
    int pid = fork(); 
    signal(SIGINT, sigkill); 
    signal(SIGQUIT, sigkill); 
    signal(SIGTERM, sigkill); 
    if(pid == 0) //IF CHILD 
    { 
     int ch; 
     me = "Child"; 
     while(1) 
     { 
      ch = (rand() % 26) + 'A'; // limit range to ascii A-Z 
      printf("%c",ch); 
      fflush(stdout); // flush output buffer 
      sleep(2); // don't overwhelm 
      if (1 == getppid()) 
      { 
       printf("=== CHILD EXIT SINCE PARENT DIED ===\n"); 
       exit(0); 
      } 
     } 
     printf("==CHILD EXIT NORMAL==\n"); 
    } 
    else //PARENT PROCESS 
    { 
     int ch; 
     if((ch = getchar())==27) 
      kill(pid, SIGINT); 
     //printf("==PARENT EXIT NORMAL (ch=%d)==\n", ch); 
    } 
    return(0); 
} 

在这个程序U将只需要后esc焦炭按enter,因为getchar()是阻塞函数。 你也可以根据你的需要去除或减少孩子进程的睡眠时间。

+1

除了'getchar'等待输入,所以'getchar'等待用户按[enter]时不会输出随机字符。 – 2010-06-06 13:53:35

4

改变一个按键的tty设置:

int getch(void) { 
     int c=0; 

     struct termios org_opts, new_opts; 
     int res=0; 
      //----- store old settings ----------- 
     res=tcgetattr(STDIN_FILENO, &org_opts); 
     assert(res==0); 
      //---- set new terminal parms -------- 
     memcpy(&new_opts, &org_opts, sizeof(new_opts)); 
     new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL); 
     tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); 
     c=getchar(); 
      //------ restore old settings --------- 
     res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); 
     assert(res==0); 
     return(c); 
} 
+0

“ICRNL”不会进入“c_iflag”字段,而不是“c_lflag”字段? – 2012-09-07 14:17:24

20

的终端设备线路规程通常默认工作在规范的模式。在此模式下,终端驱动程序不会向用户空间显示缓冲区,直到看到换行符(输入键被按下)。

通过使用tcsetattr()来操纵termios结构,可以将终端设置为原始(非规范)模式。分别清除ECHOICANON标志禁止在输入字符时回显字符,并使读取请求直接从输入队列中满足。在c_cc阵列中将VTIMEVMIN的值设置为零会导致读取请求(fgetc())立即返回而不是阻止;有效地查询stdin。如果字符在流中不可用,则调用fgetc()将返回EOF

#define _XOPEN_SOURCE 700 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <termios.h> 
#include <time.h> 

int getkey() { 
    int character; 
    struct termios orig_term_attr; 
    struct termios new_term_attr; 

    /* set the terminal to raw mode */ 
    tcgetattr(fileno(stdin), &orig_term_attr); 
    memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios)); 
    new_term_attr.c_lflag &= ~(ECHO|ICANON); 
    new_term_attr.c_cc[VTIME] = 0; 
    new_term_attr.c_cc[VMIN] = 0; 
    tcsetattr(fileno(stdin), TCSANOW, &new_term_attr); 

    /* read a character from the stdin stream without blocking */ 
    /* returns EOF (-1) if no character is available */ 
    character = fgetc(stdin); 

    /* restore the original terminal attributes */ 
    tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr); 

    return character; 
} 

int main() 
{ 
    int key; 

    /* initialize the random number generator */ 
    srand(time(NULL)); 

    for (;;) { 
     key = getkey(); 
     /* terminate loop on ESC (0x1B) or Ctrl-D (0x04) on STDIN */ 
     if (key == 0x1B || key == 0x04) { 
      break; 
     } 
     else { 
      /* print random ASCII character between 0x20 - 0x7F */ 
      key = (rand() % 0x7F); 
      printf("%c", ((key < 0x20) ? (key + 0x20) : key)); 
     } 
    } 

    return 0; 
}

注意:为简单起见,此代码省略了错误检查。

+1

这似乎工作,但它似乎每次都提供整个缓冲区。所以如果我按了一个然后b然后c,在c按后它显示aababc – Jackie 2013-06-29 14:03:31

+0

@Jackie也许做一个'while(getchar()!= EOF);'在'character = fgetc(stdin) – 2016-07-02 16:45:52

相关问题