I'm making a multiplayer game with TCP connection between client and server in C++. The client is ment to detect keypresses (arrowkeys) from the console and send data to the server. The server is supposed to send data to all connected clients about player position etc. This means I need to receive data from the server at anytime whilst detecting whenever an arrowkey is pressed in order to send data back to the server. How do I listen for keyevent and receive data at the same time?
I've tried the following, however the loop gets stuck at _getch(); which waits for input.
int keyListener() {
int keypress = _getch();
return keypress;
}
int main() {
int c = 0;
bool run = true;
while (run) {
cout << "Listening to socket" << endl;
c = keyListener();
switch (c) {
case KEY_UP:
cout << "UP" << endl;
break;
case KEY_DOWN:
cout << "DOWN" << endl;
break;
case KEY_LEFT:
cout << "LEFT" << endl;
break;
case KEY_RIGHT:
cout << "RIGHT" << endl;
break;
default:
cout << "Listening for keypress" << endl;
break;
}
}
return 0;
}