getch() for Unix

I find this code
Author very respects!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <termios.h>
#include <unistd.h>

using namespace std;

int getch(void)
{
	struct termios oldt, newt;
	int ch;

	tcgetattr(STDIN_FILENO, &oldt);
	newt = oldt;
	newt.c_lflag &= ~(ICANON | ECHO);
	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
	ch = getchar();
	tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

	return ch;
}

int main()
{
	cout << "Input: ";

	int key = getch();

	cout << (char)key << ' ';
	cout << dec << uppercase << key << ' ';
	cout << hex << uppercase << "0x" << key << ' ';
	cout << oct << uppercase << "0" << key << ' ';
	cout << endl;

return 0;
}
Last edited on
Topic archived. No new replies allowed.