屏幕上运行的C ++终端句柄按键

时间:2018-07-23 14:09:19

标签: c++ linux console gnu-screen

我需要在终端上阅读按键。

#include <stdio.h>
#include <unistd.h>
#include <termios.h>

int main()
{
    struct termios old_tio, new_tio;
    unsigned char c;

    /* get the terminal settings for stdin */
    tcgetattr(STDIN_FILENO,&old_tio);

    /* we want to keep the old setting to restore them a the end */
    new_tio=old_tio;

    /* disable canonical mode (buffered i/o) and local echo */
    new_tio.c_lflag &=(~ICANON & ~ECHO);

    /* set the new settings immediately */
    tcsetattr(STDIN_FILENO,TCSANOW,&new_tio);

    do {
     c=getchar();
     printf("%d ",c);
    } while(c!='q');

    /* restore the former settings */
    tcsetattr(STDIN_FILENO,TCSANOW,&old_tio);

    return 0;
}

使用命令gcc test.cpp -o test

它可以正常工作,但是如果我在屏幕上运行以下代码:screen -dmS m ./test我在重新连接屏幕后在终端中看到了

255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

我的CPU负载为100%。 我该如何解决这个问题?

0 个答案:

没有答案