C ++ - 使事件循环 [英] C++ - Making an event loop

查看:109
本文介绍了C ++ - 使事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在没有库的情况下在c ++中进行事件循环?它不一定是跨平台的,我在Mac上。基本上,我想让程序运行,直到用户按下向上箭头键,然后程序将输出你按下或什么。所有我可以想到的是有一个无限的循环,并获得输入与 cin ,但我不认为 cin 可以检测箭头键,我相信它会暂停程序,直到达到'\\\
';



我希望它看起来像这样:

  void RUN )
{
while(true)
{
//投票事件,如果需要可以做某事
}
}

int main()
{
RUN();
}

我确信没有线程是可能的,我听说过这可以通过 fd_set 或某些东西来实现,但我不知道如何。



任何帮助将是真的



编辑:



程序 >在没有任何事件的情况下运行在后台。例如,Microsoft Word不会停止,直到用户按下一个按钮,它不断运行。我想要这样的东西,但命令行不是GUI。

解决方案

你可以使用 ncurses 并启用 cbreak 获取原始输入流。


Does anyone know how to make an event loop in c++ without a library? It doesn't have to be cross-platform, I'm on a Mac. Basically, I want the program to run and do nothing until the user presses the up arrow key, then the program will output "You pressed up" or something. All i can think of is having an infinite while or for loop and get input with cin, but I don't think cin can detect arrow keys and I believe it pauses the program until it reaches a '\n';

I would want it to look like this:

void RUN()
{
   while(true)
   {
      // poll events and do something if needed
   }
}

int main()
{
   RUN();
}

I'm kinda sure it's possible without threads, and I've heard that this can be accomplished with fd_set or something, but I'm not sure how.

Any help would be really appreciated.

EDIT:

The program has to run in the background when there aren't any events. For example, Microsoft Word doesn't stop until the user presses a button, it keeps running. I want something like that, but command-line not GUI.

解决方案

You can use ncurses and enable cbreak to get the raw input stream.

这篇关于C ++ - 使事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆