Skip to content

Instantly share code, notes, and snippets.

@injust90
Created April 13, 2025 23:23
Show Gist options
  • Save injust90/e2c9e420348a94857822a4222042246c to your computer and use it in GitHub Desktop.
Save injust90/e2c9e420348a94857822a4222042246c to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
struct termios orig_termios;
void disableRawMode()
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void enableRawMode()
{
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int main()
{
enableRawMode();
int c;
/*
while (read(STDIN_FILENO, &c, 1) == 1 && c!= 'q')
{
// if (iscntrl(c)) {
if (c == 0x7F){
printf("%d\n", c);
}
else {
printf("%d ('%c')\n", c, c);
}
}
*/
while ((c = getchar()) != EOF)
{
if (c == ' ')
printf("\\t");
if (c == '\\')
printf("\\\\");
if (c == 0x7F)
printf("backspace");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment