-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
71 lines (62 loc) · 1.61 KB
/
test.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int main()
{
struct termios old, new;
char c;
// Get the current terminal settings
tcgetattr(STDIN_FILENO, &old);
new = old;
// Put the terminal in raw mode
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new);
// Print message that stays on the screen
printf("Type something: ");
fflush(stdout);
// Save cursor position
printf("\033[s");
while (1)
{
// Read a single character from the terminal
c = getchar();
// Check for arrow keys
if (c == '\033')
{
getchar(); // Consume the [
c = getchar(); // Get the actual arrow key code
if (c == 'A')
{
printf("\033[uYou pressed the up arrow! ");
fflush(stdout);
}
else if (c == 'B')
{
printf("\033[uYou pressed the down arrow! ");
fflush(stdout);
}
else if (c == 'C')
{
printf("\033[uYou pressed the right arrow! ");
fflush(stdout);
}
else if (c == 'D')
{
printf("\033[uYou pressed the left arrow! ");
fflush(stdout);
}
}
else if (c == '\n')
{
break;
}
else
{
putchar(c);
fflush(stdout);
}
}
// Restore the terminal settings
tcsetattr(STDIN_FILENO, TCSANOW, &old);
return 0;
}