Created
December 31, 2013 10:08
-
-
Save dagon666/8194870 to your computer and use it in GitHub Desktop.
linux tty with canonical mode turned off - non blocking getchar()
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 <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <termios.h> | |
| void cm_off(void) | |
| { | |
| struct termios t; | |
| tcgetattr(STDIN_FILENO, &t); | |
| t.c_lflag &= ~ICANON; | |
| t.c_lflag &= ~ECHO; | |
| // Apply the new settings | |
| tcsetattr(STDIN_FILENO, TCSANOW, &t); | |
| } | |
| void cm_on(void) | |
| { | |
| struct termios t; | |
| tcgetattr(STDIN_FILENO, &t); | |
| t.c_lflag |= ICANON; | |
| t.c_lflag |= ECHO; | |
| // Apply the new settings | |
| tcsetattr(STDIN_FILENO, TCSANOW, &t); | |
| } | |
| int main(int argc, char const *argv[]) | |
| { | |
| cm_off(); | |
| while (1) { | |
| printf("%02x\n", (unsigned char)getchar()); | |
| usleep(300000); | |
| } | |
| cm_on(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment