Created
April 29, 2016 05:36
-
-
Save Hikari9/05acca065cc11330e18ff812f18b1cf7 to your computer and use it in GitHub Desktop.
getch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <unistd.h> //_getch | |
| #include <termios.h> //_getch | |
| #include <cstdio> | |
| #include <iostream> | |
| using namespace std; | |
| char getch(){ | |
| char buf=0; | |
| struct termios old={0}; | |
| fflush(stdout); | |
| if(tcgetattr(0, &old)<0) | |
| perror("tcsetattr()"); | |
| old.c_lflag&=~ICANON; | |
| old.c_lflag&=~ECHO; | |
| old.c_cc[VMIN]=1; | |
| old.c_cc[VTIME]=0; | |
| if(tcsetattr(0, TCSANOW, &old)<0) | |
| perror("tcsetattr ICANON"); | |
| if(read(0,&buf,1)<0) | |
| perror("read()"); | |
| old.c_lflag|=ICANON; | |
| old.c_lflag|=ECHO; | |
| if(tcsetattr(0, TCSADRAIN, &old)<0) | |
| perror ("tcsetattr ~ICANON"); | |
| if (buf == 127) { | |
| printf("\b "); | |
| buf = '\b'; | |
| } | |
| printf("%c", buf); | |
| return buf; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment