纯干货:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <curses.h>
#include <unistd.h>
#include <term.h>
static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard();
void close_keyboard();
int kbhit();
int readch();
int main()
{
int ch = 0;
init_keyboard();
while(ch != 'q')
{
printf("looping\n");
sleep(1);
if(kbhit())
{
ch = readch();
printf("you hit %c\n",ch);
}
}
close_keyboard();
exit(0);
}
void init_keyboard()
{
tcgetattr(0, &initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECH