Advertisement
sNow_32

getch()

Nov 18th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1.  
  2. #include <termios.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5.  
  6. int getch() {
  7.     struct termios oldtc, newtc;
  8.     int ch;
  9.     tcgetattr(STDIN_FILENO, &oldtc);
  10.     newtc = oldtc;
  11.     newtc.c_lflag &= ~(ICANON | ECHO);
  12.     tcsetattr(STDIN_FILENO, TCSANOW, &newtc);
  13.     ch=getchar();
  14.     tcsetattr(STDIN_FILENO, TCSANOW, &oldtc);
  15.     return ch;
  16. }
  17. int main(int argc, char **argv) {
  18.     int ch;
  19.     printf("Press x to exit.\n\n");
  20.     for (;;) {
  21.         ch = getch();
  22.         printf("ch = %c (%d)\n", ch, ch);
  23.         if(ch == 'x')
  24.               break;
  25.     }
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement