Add getch() implement without conio.h
This commit is contained in:
parent
eb028441da
commit
41371775e3
@ -1,12 +1,49 @@
|
|||||||
#include "ColorUI.h"
|
#include "ColorUI.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <conio.h> // getch
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
|
#include <conio.h> // getch
|
||||||
#else
|
#else
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
/// Fix getch() without conio.h
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
|
struct termios _old, _new;
|
||||||
|
/* Initialize new terminal i/o settings */
|
||||||
|
void initTermios(int echo)
|
||||||
|
{
|
||||||
|
tcgetattr(0, &_old); /* grab old terminal i/o settings */
|
||||||
|
new = old; /* make new settings same as old settings */
|
||||||
|
new.c_lflag &= ~ICANON; /* disable buffered i/o */
|
||||||
|
new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
|
||||||
|
tcsetattr(0, TCSANOW, &_new); /* use these new terminal i/o settings now */
|
||||||
|
}
|
||||||
|
/* Restore old terminal i/o settings */
|
||||||
|
void resetTermios(void)
|
||||||
|
{
|
||||||
|
tcsetattr(0, TCSANOW, &old);
|
||||||
|
}
|
||||||
|
/* Read 1 character - echo defines echo mode */
|
||||||
|
char getch_(int echo)
|
||||||
|
{
|
||||||
|
char ch;
|
||||||
|
initTermios(echo);
|
||||||
|
ch = getchar();
|
||||||
|
resetTermios();
|
||||||
|
return ch;
|
||||||
|
}
|
||||||
|
/* Read 1 character without echo */
|
||||||
|
char getch(void)
|
||||||
|
{
|
||||||
|
return getch_(0);
|
||||||
|
}
|
||||||
|
/* Read 1 character with echo */
|
||||||
|
char getche(void)
|
||||||
|
{
|
||||||
|
return getch_(1);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace _cns
|
namespace _cns
|
||||||
|
Reference in New Issue
Block a user