Skip to content

Instantly share code, notes, and snippets.

@michael-grunder
Created December 13, 2022 05:16
Show Gist options
  • Save michael-grunder/c7276df94febbbfbc39414e77ed75df8 to your computer and use it in GitHub Desktop.
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
// 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