【问题标题】:I need a Function to read a key press in C++ [duplicate]我需要一个函数来读取 C++ 中的按键 [重复]
【发布时间】:2021-11-08 19:45:15
【问题描述】:

我正在尝试创建一个程序来检查是否按下了键盘中的特定键并将布尔值返回到 while 循环中

类似这样的:

int main(int argc, char** argv){

   std::cout << "Press the spacebar to exit loop";
   while (true){
      if (IsKeyPressed("space")){
         break;
      }
   }
   return 0;
}

如果这很重要,我会使用 Linux。

【问题讨论】:

标签: c++


【解决方案1】:

C++ 标准本身没有任何读取输入的功能。如果要读取输入,则必须使用外部库。任何游戏开发库都会有这方面的功能,比如SFML

使用 SFML:

#include <SFML/Window.hpp>
int main(int argc, char** argv){

   std::cout << "Press the spacebar to exit loop";
   while (true){
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
         break;
      }
   }
   return 0;
}

【讨论】:

    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2021-07-23
    • 1970-01-01
    • 2019-08-18
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多