• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • Linux下getch的模擬實現

    發表于:2007-05-26來源:作者:點擊數: 標簽:
    linux下默認是沒有提供getch這個函數的,我們這里使用termios來模擬實現一個getch()函數,其基本原理為將終端設定為raw(或不緩沖的)模式。 int getch(void) { struct termios tm, tm_old; int fd = STDIN_FILENO, c; if(tcgetattr(fd, tm) 0) return -1; t

    linux下默認是沒有提供getch這個函數的,我們這里使用termios來模擬實現一個getch()函數,其基本原理為將終端設定為raw(或不緩沖的)模式。

     int getch(void) 
    {
    struct termios tm, tm_old;
    int fd = STDIN_FILENO, c;

    if(tcgetattr(fd, &tm) < 0)
    return -1;

    tm_old = tm;

    cfmakeraw(&tm);

    if(tcsetattr(fd, TCSANOW, &tm) < 0)
    return -1;

    c = fgetc(stdin);

    if(tcsetattr(fd, TCSANOW, &tm_old) < 0)
    return -1;

    return c;
    }

    這里的getch()實際相當于turbo c中的kbhit(), 其中關鍵為cfmakeraw對終端的相關控制

    ,man page 說明cfmakeraw()實際上做了:


    cfmakeraw sets the terminal attributes as follows:
    termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
    |INLCR|IGNCR|ICRNL|IXON);
    termios_p->c_oflag &= ~OPOST;
    termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
    termios_p->c_cflag &= ~(CSIZE|PARENB);
    termios_p->c_cflag |= CS8;

     


    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>