Organized getch.c a little more

This commit is contained in:
R. Eric Wheeler 2021-01-07 15:55:29 -08:00
parent d50a05a566
commit 382ca844d5
2 changed files with 25 additions and 7 deletions

View File

@ -1,19 +1,37 @@
#include <stdio.h> #include <stdio.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
static struct termios oldattr;
static void setRawMode(void)
{
struct termios newattr;
tcgetattr(STDIN_FILENO, &oldattr);
newattr = oldattr;
cfmakeraw(&newattr);
tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
}
static void setNormalMode(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
}
/* reads from keypress, doesn't echo */ /* reads from keypress, doesn't echo */
int _getch(void) int _getch(void)
{ {
struct termios oldattr, newattr;
int ch; int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr; setRawMode();
cfmakeraw(&newattr); atexit(setNormalMode);
//newattr.c_lflag &= ~( ICANON | ECHO );
//tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar(); ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
setNormalMode();
return ch; return ch;
} }

Binary file not shown.