W
W
Warinator2015-10-03 19:10:39
C++ / C#
Warinator, 2015-10-03 19:10:39

How to make the waiting for a keypress to occur in parallel with the data update?

I need to implement a simple chat emulator. To do this, you need to update the message buffer in a loop and, if there is a new one, display it on the screen. At the same time, you need to be able to enter a message yourself and put it in the buffer, as well as the ability to exit the loop, for example, by pressing Esc.
The principle should be like this:

while(1)
{
      //если есть новое сообщение, вывести его на экран
     //если пользователь вводит сообщение (или нажимает Esc), обработать это действие
     //иначе, перейти к след. итерации
}

Variants like getch(), scanf() stop the loop, regardless of whether the user wants to enter something. Please tell me how to solve this problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Warinator, 2015-10-03
@Warinator

All I needed was the kbhit() function.

G
GavriKos, 2015-10-03
@GavriKos

Multi-threading will save you.
Here it’s still over to look at what you draw the GUI with, maybe you can somehow implement it in it.

T
Tlito, 2015-10-03
@tlito

i.e. you need to run a parallel process in the background that catches keystrokes and calls a function when esc is pressed?
I haven't tried but found OpenMP https://software.intel.com/ru-ru/blogs/2011/11/21/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question