2010-06-06 78 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

相關問題