C++,如何通过键盘输入控制程序流程
我有一个无限循环的主例程。通过使用键盘输入更改 bool 变量,我希望能够控制是否调用该循环中的某些 if{} 语句。我找到了这个帖子:
C 非阻塞键盘输入,
但对于看似基本的功能来说,似乎过于费力和复杂。有更简单的方法吗?
I have a main routine that loops infinitely. By changing bool variables using keyboard input, I want to be able to control whether certain if{} statements within that loop are getting called. I found this thread:
C non-blocking keyboard input,
but it seems excessively laborious and complicated for seemingly basic functionality. Is there an easier way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用操作系统/固件/框架/环境 API 来获取输入事件,或者使用为您执行此操作的库。不管怎样,C++ 中没有内置的方法可以做到这一点。
我经常在游戏中使用OIS。它是跨平台的并且易于使用。不确定它对游戏以外的其他情况是否有用,但它可以为您完成工作。
You'll have to use the OS/Firmware/Framework/environment API to get input events, or use a library that do this for you. Anyway, there is no built-in way of doing this in C++.
I often use OIS in games. It's cross-platform and easy to use. Not sure it's useful for other cases than games but it does the job for you.
SDL 库是实现跨平台的一种方法。这是轮询键盘事件的示例。
The SDL library is one way to do it cross-platform. Here's an example of polling keyboard events.
将主例程放在一个线程中,然后使用诸如
线程代码之类的东西,然后可以根据按下的键执行不同的操作。
Put the main routine in a thread, then have something like
Threaded code can then do different things based on what key was pressed.