How to get input from keyboard while doing other things at the same time?

孤人 提交于 2019-11-30 18:38:15

make getch a non-blocking call using nodelay option.

nodelay(stdscr,TRUE);

More info can be found at http://www.gsp.com/cgi-bin/man.cgi?topic=nodelay

Depending on your environment you maybe have the function kbhit(), it just peaks in the keyboard buffer and returns non zero if there is a key there - then you can do getch().

(conio.h)

Maybe fork()? Just keep in mind that the child runs in a separate process so variables can't be directly shared between them

pid_t pID = fork();
if (pID == 0) {                // child
   // Code  executed only by child process
   // getch() ...
} else if (pID < 0) {          // failed to fork
   // Throw exception
   // ... 
} else {                       // parent
   // Code executed only by parent process
   // ...
}

Try select() or poll(). They both do the same thing, but with different interfaces. You give it a list of file descriptors, and it will wait until at least one of those descriptors is ready, then return and tell you which ones can be read from (or written to) without blocking. Check out the links for examples and more details.

abhishek

use cbreak() to disable line buffering

nodelay(win, TRUE); 

this will get you a non blocking getch() function.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!