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

#include    <sys/select.h>









static  char  Wait_For_Key(int ms)
{
    struct termios old_tio, new_tio;
    tcgetattr(STDIN_FILENO, &old_tio); // Get current terminal settings
    new_tio = old_tio;
    new_tio.c_lflag &= (~ICANON & ~ECHO); // Disable canonical mode and echo
    tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); // Apply new settings

    fd_set fds;
    struct timeval tv;
    char c = 0;

    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds);

    tv.tv_sec = ms/1000;
    tv.tv_usec = ms*1000;

    int ret = select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
    if(ret == -1){
        perror("select failed");
    }else if(ret == 0){
        // Timeout occurred
        //std::cout << "Timeout!" << std::endl;
    }else{
        // Key pressed
        size_t   rlen = read(STDIN_FILENO, &c, 1);
        std::cout << "Key pressed: " << c << std::endl;
    }
    tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); // Restore original settings
    return c;
}



char key = Wait_For_Key(10);





标签: none

评论已关闭