-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyhandler.c
More file actions
165 lines (119 loc) · 2.63 KB
/
keyhandler.c
File metadata and controls
165 lines (119 loc) · 2.63 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* keyhandler.c */
/*
A simple program to handle keys (in particular, function keys,
Ctrl keys, Alt keys and arrow keys).
This code is released to the public domain.
"Share and enjoy...." ;)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <ctype.h>
void func1(void)
{
printf("Hey, you entered foo! \n");
}
void func2(void)
{
printf("Hey, you entered bar! \n");
}
void alt_a(void)
{
printf("Hey, you pressed ALT a! \n");
}
void alt_b(void)
{
printf("Hey, you pressed ALT b! \n");
}
void ctrl_a(void)
{
printf("Hey, you pressed CTRL a! \n");
}
void ctrl_g(void)
{
printf("Hey, you pressed CTRL g! \n");
}
void up_arrow(void)
{
printf("Hey, you pressed the up arrow! \n");
}
void down_arrow(void)
{
printf("Hey, you pressed the down arrow! \n");
}
void left_arrow(void)
{
printf("Hey, you pressed the left arrow! \n");
}
void right_arrow(void)
{
printf("Hey, you pressed the right arrow! \n");
}
void f2(void)
{
printf("Hey, you pressed F2! \n");
}
void f3(void)
{
printf("Hey, you pressed F3! \n");
}
void f4(void)
{
printf("Hey, you pressed F4! \n");
}
int main(void)
{
char word[80];
char ch;
do {
puts("Enter some text :");
scanf("%s", word);
if ( !strcmp(word, "foo") ) {
func1();
}
else if (!strcmp(word, "bar") ) {
func2();
}
else if (!strcmp(word, "\x1b\x61") ) {
alt_a();
}
else if (!strcmp(word, "\x1b\x62") ) {
alt_b();
}
else if (!strcmp(word, "\x07") ) {
ctrl_g();
}
else if (!strcmp(word, "\x01") ) {
ctrl_a();
}
else if (!strcmp(word, "\x1b\x5b\x41") ) {
up_arrow();
}
else if (!strcmp(word, "\x1b\x5b\x42") ) {
down_arrow();
}
else if (!strcmp(word, "\x1b\x5b\x43") ) {
right_arrow();
}
else if (!strcmp(word, "\x1b\x5b\x44") ) {
left_arrow();
}
else if (!strcmp(word, "\x1b\x4f\x51") ) {
f2();
}
else if (!strcmp(word, "\x1b\x4f\x52") ) {
f3();
}
else if (!strcmp(word, "\x1b\x4f\x53") ) {
f4();
}
else {
printf("Nope - I do not recognise that phrase.... \n");
}
printf("Try again? (y/n) : ");
scanf(" %c%*c", &ch);
}
while( toupper(ch) != 'N' );
return 0;
}