Skip to content
Snippets Groups Projects
getchtest.c 1.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • KosmX's avatar
    KosmX committed
    
    #include<stdio.h>
    #include<termios.h>
    #include<unistd.h>
    
    #include<sys/select.h>
    #include<sys/ioctl.h>
    
    int _kbhit(){
        int kbhits;
        ioctl(0, FIONREAD, &kbhits);
        return kbhits;
    }
    
    KosmX's avatar
    KosmX committed
    
    int main(int argc, char const *argv[])
    {
    
    
        //struct termios info;          // This is from stackoverflow
        //tcgetattr(0, &info);          /* get current terminal attirbutes; 0 is the file descriptor for stdin */
        //info.c_lflag &= ~ICANON;      /* disable canonical mode */
        //info.c_cc[VMIN] = 1;          /* don't wait for keypress, return EOF instead */
        //info.c_cc[VTIME] = 1;         /* no timeout */
        //info.c_cc[ECHO] = 0; //Turn off echo
        //tcsetattr(0, TCSANOW, &info); /* set immediately */
    
    KosmX's avatar
    KosmX committed
    
    
        //initscr();
        //noecho();
        //cbreak();
        //scrollok(stdscr, true);
        //nodelay(stdscr, true);
    
    KosmX's avatar
    KosmX committed
    
    
        struct termios info;
        tcgetattr(0, &info); //get attr
        info.c_lflag &= ~ICANON; //turn off canonical mode
        info.c_lflag &= ~ECHO;
        tcsetattr(0, TCSANOW, &info); //set attr
        setbuf(stdin, NULL); //???
        
    
    
        int c = 0;
    
    KosmX's avatar
    KosmX committed
        sleep(1);
    
        int l = _kbhit();
    
    KosmX's avatar
    KosmX committed
        c = getchar();
    
        printf("\nint: %d\nchar: %c\nkbhit: %d\n", c, c, l);
    
    KosmX's avatar
    KosmX committed
    
        sleep(5);
        c = 0;
        while(c != -1){
            c = getchar();
            printf("\nint: %d\nchar: %c\n", c, c);
        }
        return 0;
    }