【问题标题】:How detect spacebar? [closed]如何检测空格键? [关闭]
【发布时间】:2015-01-14 07:17:33
【问题描述】:

我有一个 C++ 程序,我必须检测空格键,我该怎么做?我看到我需要函数getch(),但在我的程序中我没有conio.h。是否有其他解决方案?

getchar我需要按intro,是否存在我只按空格键的其他形式?

例如,我可以在不按intro的情况下介绍intro吗???

【问题讨论】:

  • getchar()getc 怎么样?
  • @IrrationalPerson conio.h 不是标准 C++ 头文件。

标签: c++ codeblocks


【解决方案1】:

简单示例

#include <iostream>

using namespace std;

int main()
{
    char ans;
    do
    {
        //code
    }
    while(getchar() != 32 || getchar() != ' ');

   cout << "Space pressed" << endl;
   return 0;
}

Compiled Code

Windows.h:

if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
     MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);

看不到 conio.h

【讨论】:

  • 如果您发现任何问题,请告诉我
  • 主要问题是行缓冲。只有在回车后你才会得到char(32)。第二个问题是32,应该是' '
  • @IrrationalPerson 之一,您缺少分号。您是否尝试过编译您的解决方案?
  • 最后的if 有什么意义?在该条件为真之前,您不会离开 while 循环。
  • 你评论的时候我正在编译
猜你喜欢
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2022-12-07
  • 1970-01-01
相关资源
最近更新 更多