Created
December 13, 2022 05:16
-
-
Save michael-grunder/c7276df94febbbfbc39414e77ed75df8 to your computer and use it in GitHub Desktop.
Changing terminal settings so `getc` doesn't wait for the user to press ENTER
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
// Compile with: cc -Wall -otermio termio.c | |
#include <stdio.h> | |
#include <termios.h> | |
#include <unistd.h> | |
int main(void) { | |
static struct termios oldt, newt; | |
/* Get current termio settings */ | |
tcgetattr( STDIN_FILENO, &oldt); | |
/* We only want to disable ICANON mode (don't wait for \n) */ | |
newt = oldt; | |
newt.c_lflag &= ~(ICANON); | |
tcsetattr( STDIN_FILENO, TCSANOW, &newt); | |
/* Grab a character */ | |
char c = getc(stdin); | |
printf("\nPressed: %c\n", c); | |
/* Restore old termio settings */ | |
tcsetattr( STDIN_FILENO, TCSANOW, &oldt); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment