Reading Single Char in Linux

/* mygetchar.c Read a single char without pressing the 'Enter' */#include <stdio.h> #include<unistd.h> /* Need for myGetChar */ #include <termios.h> /* Need for myGetChar */ /* myGetChar Function */ int myGetChar() {   int ch;   struct termios olds, news;   /* Get Terminal Settings */   tcgetattr(0, &olds);   news = olds;   /* Change the Canonical Mode */   news.c_lflag &= ~(ICANON | ECHO);    /* Set the new settings */   tcsetattr(0, TCSANOW, &news);   ch = getchar();   /* Set the old settings */   tcsetattr(0, TCSANOW, &olds);   return ch; } /* Main */ int main() {   int i;   char ch;   /* Test Loop: Read 5 Chars */   for (i=0; i<5; i++)   {       printf("Hit a Key: ");       ch = myGetChar();       printf ("The key you hit was '%c'.n", ch);   }   return 0; }

Tagged with: ,
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: