Skip to content

Commit f2968ae

Browse files
committed
cross-platform getch() is done
1 parent b00b0c5 commit f2968ae

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/utils.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ string_array_free(char** str_array, unsigned int length)
109109

110110
// INPUT UNTILS //
111111

112+
#ifdef __unix__
113+
112114
/*
113115
*
114116
*/
@@ -133,34 +135,51 @@ getch() {
133135
return buf;
134136
}
135137

138+
#endif
139+
136140
/*
137141
*
138142
*/
139143
int
140144
get_key_pressed()
141145
{
142-
int getch_buf;
143-
getch_buf = getch();
146+
int init_buffer, ch = -1;
147+
init_buffer = getch();
148+
149+
//printf("\nInit buffer: %d", init_buffer);
144150

145151
// in different machines backspace can be 8 or 127 coded
146-
if (getch_buf == BS_KEY || getch_buf == BS_KEY2)
152+
if (init_buffer == BS_KEY || init_buffer == BS_KEY2)
147153
return BACKSPACE;
148154

149-
if (getch_buf == ESCAPE_CODE) {
155+
#ifdef __unix__
156+
157+
if (init_buffer == ESCAPE_CODE) {
150158
getch();
151-
switch(getch())
152-
{
153-
case A_KEY:
154-
return ARROW_UP;
155-
case B_KEY:
156-
return ARROW_DOWN;
157-
case C_KEY:
158-
return ARROW_RIGHT;
159-
case D_KEY:
160-
return ARROW_LEFT;
161-
}
159+
ch = getch();
162160
}
163-
return getch_buf;
161+
162+
#elif _WIN32
163+
164+
if (init_buffer == WIN_META_CODE1 || init_buffer == WIN_META_CODE2)
165+
ch = getch();
166+
167+
#endif
168+
169+
switch (ch)
170+
{
171+
case A_KEY:
172+
return ARROW_UP;
173+
case B_KEY:
174+
return ARROW_DOWN;
175+
case C_KEY:
176+
return ARROW_RIGHT;
177+
case D_KEY:
178+
return ARROW_LEFT;
179+
default: break;
180+
}
181+
182+
return init_buffer;
164183
}
165184

166185

0 commit comments

Comments
 (0)