-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeys.c
250 lines (234 loc) · 4.55 KB
/
keys.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keys.h"
//
// Keyboard functions
//
void initkeys() {
memset(last_keys, 0xFF, LAST_KEYS_LENGTH);
}
int getchar_logged() {
int ch = getchar();
for(int i = 1; i < LAST_KEYS_LENGTH; i++) {
last_keys[i-1] = last_keys[i];
}
last_keys[LAST_KEYS_LENGTH - 1] = ch;
return ch;
}
int getkey() {
int ch, shift, ctrl;
ch = getchar_logged();
if (ch < 0)
return ch;
switch (ch) {
case 0x08:
return KEY_BACKSPACE;
case 0x09:
return KEY_TAB;
case 0x0D:
return KEY_ENTER;
case 0x0A:
return KEY_ENTER;
case 0x1B:
ch = getchar_logged();
switch (ch) {
case 0x1B:
return KEY_ESC;
case 0x4F:
ch = getchar_logged();
switch (ch) {
case 0x46:
return KEY_END;
case 0x48:
return KEY_HOME;
case 0x50:
return KEY_F1;
case 0x52:
return KEY_F3;
case 0x54:
return KEY_F5;
default:
return KEY_UNKNOWN;
}
break;
case 0x5B:
shift = ctrl = 0;
ch = getchar_logged();
if (ch == 0x31) {
ch = getchar_logged();
if (ch == 0x3B) {
ch = getchar_logged();
if (ch == 0x32)
shift = 1;
if (ch == 0x35)
ctrl = 1;
if (ch == 0x36)
shift = ctrl = 1;
ch = getchar_logged();
} else {
switch (ch) {
case 0x35:
return getchar_logged() == 0x7E ? KEY_F5 : KEY_UNKNOWN;
case 0x37:
return getchar_logged() == 0x7E ? KEY_F6 : KEY_UNKNOWN;
case 0x38:
return getchar_logged() == 0x7E ? KEY_F7 : KEY_UNKNOWN;
}
}
}
switch (ch) {
case 0x31:
return getchar_logged() == 0x7E ? KEY_HOME : KEY_UNKNOWN;
case 0x32:
return getchar_logged() == 0x7E ? KEY_INS : KEY_UNKNOWN;
case 0x33:
return getchar_logged() == 0x7E ? KEY_DEL : KEY_UNKNOWN;
case 0x34:
return getchar_logged() == 0x7E ? KEY_END : KEY_UNKNOWN;
case 0x35:
return getchar_logged() == 0x7E ? KEY_PGUP : KEY_UNKNOWN;
case 0x36:
return getchar_logged() == 0x7E ? KEY_PGDN : KEY_UNKNOWN;
case 0x41:
if (shift && ctrl)
return KEY_SHIFT_CTRL_UP;
if (shift)
return KEY_SHIFT_UP;
if (ctrl)
return KEY_CTRL_UP;
return KEY_UP;
case 0x42:
if (shift && ctrl)
return KEY_SHIFT_CTRL_DOWN;
if (shift)
return KEY_SHIFT_DOWN;
if (ctrl)
return KEY_CTRL_DOWN;
return KEY_DOWN;
case 0x43:
if (shift && ctrl)
return KEY_SHIFT_CTRL_RIGHT;
if (shift)
return KEY_SHIFT_RIGHT;
if (ctrl)
return KEY_CTRL_RIGHT;
return KEY_RIGHT;
case 0x44:
if (shift && ctrl)
return KEY_SHIFT_CTRL_LEFT;
if (shift)
return KEY_SHIFT_LEFT;
if (ctrl)
return KEY_CTRL_LEFT;
return KEY_LEFT;
case 0x46:
if (shift && ctrl)
return KEY_SHIFT_CTRL_END;
if (shift)
return KEY_SHIFT_END;
if (ctrl)
return KEY_CTRL_END;
return KEY_END;
case 0x48:
if (shift && ctrl)
return KEY_SHIFT_CTRL_HOME;
if (shift)
return KEY_SHIFT_HOME;
if (ctrl)
return KEY_CTRL_HOME;
return KEY_HOME;
case 0x5A:
return KEY_SHIFT_TAB;
default:
return KEY_UNKNOWN;
}
break;
default:
return KEY_UNKNOWN;
}
break;
case 0x00:
case 0xE0:
ch = getchar_logged();
switch (ch) {
case 0x0F:
return KEY_SHIFT_TAB;
case 0x3B:
return KEY_F1;
case 0x3D:
return KEY_F3;
case 0x3F:
return KEY_F5;
case 0x47:
return KEY_HOME;
case 0x48:
return KEY_UP;
case 0x49:
return KEY_PGUP;
case 0x4B:
return KEY_LEFT;
case 0x4D:
return KEY_RIGHT;
case 0x4F:
return KEY_END;
case 0x50:
return KEY_DOWN;
case 0x51:
return KEY_PGDN;
case 0x52:
return KEY_INS;
case 0x53:
return KEY_DEL;
case 0x73:
return KEY_CTRL_LEFT;
case 0x74:
return KEY_CTRL_RIGHT;
case 0x75:
return KEY_CTRL_END;
case 0x77:
return KEY_CTRL_HOME;
case 0x8D:
return KEY_CTRL_UP;
case 0x91:
return KEY_CTRL_DOWN;
case 0x94:
return KEY_CTRL_TAB;
case 0xB8:
return KEY_SHIFT_UP;
case 0xB7:
return KEY_SHIFT_HOME;
case 0xBF:
return KEY_SHIFT_END;
case 0xB9:
return KEY_SHIFT_PGUP;
case 0xBB:
return KEY_SHIFT_LEFT;
case 0xBD:
return KEY_SHIFT_RIGHT;
case 0xC0:
return KEY_SHIFT_DOWN;
case 0xC1:
return KEY_SHIFT_PGDN;
case 0xDB:
return KEY_SHIFT_CTRL_LEFT;
case 0xDD:
return KEY_SHIFT_CTRL_RIGHT;
case 0xD8:
return KEY_SHIFT_CTRL_UP;
case 0xE0:
return KEY_SHIFT_CTRL_DOWN;
case 0xD7:
return KEY_SHIFT_CTRL_HOME;
case 0xDF:
return KEY_SHIFT_CTRL_END;
default:
return KEY_UNKNOWN;
}
break;
case 0x7F:
return KEY_BACKSPACE;
default:
return ch;
}
}