Quick actions

cmd+k|ctrl+k

Navigation

Languages

kbhit

Snippet info

Language

C

Visibility

public

Author

amatov

Created

2016-11-16T11:36:24Z

Updated

2016-11-16T11:37:58Z

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

static struct termios initial_settings, new_settings;
static int peek_character = -1;

int kbhit()
{
    char ch;
    int nread;

    if(peek_character != -1)
        return 1;
    new_settings.c_cc[VMIN]=0;
    tcsetattr(0, TCSANOW, &new_settings);
    nread = read(0,&ch,1);
    new_settings.c_cc[VMIN]=1;
    tcsetattr(0, TCSANOW, &new_settings);

    if(nread == 1) {
        peek_character = ch;
        return 1;
    }
    return 0;
}

int main(void) {
    if(kbhit()) printf("Hello World!\n");
    return 0;
}
INFO