C++ || Snippet – Custom Conio.h Sample Code For Linux
This page will consist of a brief implementation of “conio.h” for Linux.
Conio.h is a C header file used in old MS-DOS compilers to create text user interfaces. It is not part of the C standard library, ISO C, nor is it defined by POSIX. As a result, compilers that target Unix platforms do not contain this header file, and do not implement its library functions.
Included in the sample code are the following:
(1) getch() - Reads a character directly from the console without buffer, and without echo
(2) getche() - Reads a character directly from the console without buffer, but with echo.
(3) kbhit() - Determines if a keyboard key was pressed.
(4) clrscr() - Clears data from the console screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include <termios.h> #include <unistd.h> #include <stdio.h> #include <fcntl.h> /* Reads a character directly from the console without buffer, and without echo. */ int getch() { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; } /* Reads a character directly from the console without buffer, but with echo */ int getche() { struct termios oldattr, newattr; int ch; tcgetattr( STDIN_FILENO, &oldattr ); newattr = oldattr; newattr.c_lflag &= ~( ICANON ); tcsetattr( STDIN_FILENO, TCSANOW, &newattr ); ch = getchar(); tcsetattr( STDIN_FILENO, TCSANOW, &oldattr ); return ch; } /* Determines if a keyboard key was pressed */ int kbhit() { struct termios oldt, newt; int ch; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if(ch != EOF) { ungetc(ch, stdin); return 1; } return 0; } /* Clears data from the console screen */ void clrscr() { char str[] = " [H [2J"; str[3] = str[0] = 27; write(1, str, 7); }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
Note: Not all functions in conio.h are included in the above sample code.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
===== DEMONSTRATION HOW TO USE =====
Use of the above header file is the same as its DOS counterpart, and can be utilized by simply naming the above snippet as “conio.h.” Here is a sample program demonstrating its use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include "conio.h" using namespace std; int main() { char userInput; while(!kbhit()) { cout<<"Press a key!n"; } userInput=getche(); cout<<"nYou pressed '"<<userInput<<"'!n"; cout<<"nThe screen will now clearn"; cout<<"Press any key to continue..n"; getch(); clrscr(); cout<<"The screen successfully clearedn"; cout<<"Press any key to continue..n"; getch(); return 0; }// http://programmingnotes.org/ |
Once compiled, you should get this as your output
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
Press a key!
p
You pressed 'p'!The screen will now clear
Press any key to continue..[ THE SCREEN CLEARS HERE ]
The screen successfully cleared
Press any key to continue..
Hello,
if you can write comment about each statement in the getch(), it would be really helpful. Actually I am new to termios and don’t know much about its functionality to set the terminal. If possible explain the snippet of getch() in much detailed manner so that it would be easy for me to read and understand how the process is going on.
With regards,
Rohit