C语言中的连续键盘输入

21

我正在使用C语言创建一个控制台应用程序。这是一个游戏,角色会从上方掉落,用户需要按键盘上的特定键来消除角色。但是我不知道如何在不暂停下落角色的情况下检测到用户按下了哪个键。当我使用scanf时,程序会等待输入,一切都会暂停。


这真的很有趣..期待一些答案。 - sukhvir
http://stackoverflow.com/a/13035523/1119701 - ouah
有没有其他方法可以在不使用ncurses的情况下完成这个任务? - sukhvir
你可以尝试钩取窗口过程并监听事件。与 scanf 不同的是,这不应该阻塞。 - MasterMastic
你可以将获取键盘输入的函数推送到另一个线程中。 - Shubham
http://en.wikipedia.org/wiki/Conio.h - hyde
3个回答

11

有一个叫做kbhit()或者_kbhit的函数,它在<conio.h>库中,如果有键被按下,它会返回true,否则返回false。你可以使用以下代码:

while (1){
    if ( _kbhit() )
        key_code = _getch();
        // do stuff depending on key_code
    else 
        continue;

同时可以使用 getch()_getch 直接从控制台读取字符,而不是从缓冲区中读取。你可以在这里阅读有关conio.h函数的更多信息,它们可能非常适合你想做的事情。

注意:conio.h 不是标准库,实现可能因编译器而异。


使用getch()函数后,我的游戏仍然会暂停并等待输入。我只是不需要在输入字符后按回车键。这不是问题的关键! - Zain Zafar
1
@zaingz 这就是为什么你必须使用 if ( kbhit() ),只有在按下某个键时它才会 "等待" 。但是因为在执行 key_code = getch(); 时已经按下了键,所以它读取的是已经被按下的键。 - Alexandru Barbarosie
非常感谢...这真的帮了我很多。 对于使用Visual Studio的人来说,一个提示是应该在每个conio函数的开头添加下划线“_”。例如:_kbhit()和_getch()。 - Zain Zafar
只是为了更新一下,在 ANSI C 中,kbhit() 和 getch() 目前已经被弃用,而是使用 _kbhit() 和 _getch()!虽然回答很有用 :) - Sanket Patel
@SanketPatel 可以随意使用这些信息编辑我的答案。 - Alexandru Barbarosie

6
你可能在寻找ncurses

ncurses(新 curses)是一个编程库,提供了一个 API,允许程序员以与终端无关的方式编写基于文本的用户界面。它是用于开发在终端仿真器下运行的“类似 GUI”的应用软件的工具包。

此外,请查看C/C++:捕获标准输入的字符而无需等待按下回车键
#include <conio.h>

if (kbhit()!=0) {
    cout<<getch()<<endl;
}

不是很确定,但如果涉及到图形实现,则可以使用SDL完成。 - Rahul Tripathi
ncurses 不可行。在 windows.h 库中没有任何东西吗? - Zain Zafar
在Windows中,ncurses的等效物是PDCurses。http://pdcurses.sourceforge.net/ - Rahul Tripathi

4
我认为这可能是你正在寻找的非阻塞键盘输入。
void simple_keyboard_input()  //win32 & conio.h
    {
        if (kbhit())
          {
                KB_code = getch();
                //cout<<"KB_code = "<<KB_code<<"\n";

                switch (KB_code)
                {

                    case KB_ESCAPE:

                        QuitGame=true;

                    break;
                 }//switch
             }//if kb
          }//void

对于字符掉落的效果,这里是相应的代码:

《黑客帝国》中的掉落数字

如果您使用的是Windows系统,以下是相关代码:

/* The Matrix  falling numbers */

#include <iostream>
#include <windows.h> 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;


#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66


/* Variables*/

char screen_buffer[2000]={' '};
int y_coord[2000]={0};
int x=0, y=0,dy=0;
int XMAX=77;
int YMAX=23;
int KB_code=0;
bool QuitGame=false;
int platformX=35, platformY=23;

/* function prototypes*/

void gotoxy(int x, int y);
void clrscr(void);
void setcolor(WORD color); 
void simple_keyboard_input();  
void draw_falling_numbers();
void draw_platform();

/*  main  */

int main(void)
{
  /* generate random seed */
  srand ( time(NULL) );

  /* generate random number*/
  for(int i=0;i<XMAX;i++) y_coord[i]=   rand() % YMAX;

  while(!QuitGame)
  {
      /* simple keyboard input */
      simple_keyboard_input();

      /* draw falling numbers */
      draw_falling_numbers();

  }

  /* restore text color */
  setcolor(7);
  clrscr( );
  cout<<" \n";

  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}

/* functions  */

void draw_falling_numbers()
{

    for(x=0;x<=XMAX;x++)
    {
        /* generate random number */
        int MatixNumber=rand() % 2 ;

        /* update falling number */
        y_coord[x]=y_coord[x]+1;

        if (y_coord[x]>YMAX) y_coord[x]=0;

        /* draw dark color */
        setcolor(2);
        gotoxy(x ,y_coord[x]-1); cout<<"  "<<MatixNumber<<"   ";

        /* draw light color */
        setcolor(10);
        gotoxy(x ,y_coord[x]); cout<<"  "<<MatixNumber<<"   ";
    }
    /* wait some milliseconds */
    Sleep(50);
    //clrscr( );
}


void draw_platform()
{
  setcolor(7);
 gotoxy(platformX ,platformY);cout<<"       ";

 gotoxy(platformX ,platformY);cout<<"ÜÜÜÜÜÜ";
 setcolor(7);
 Sleep(5);
}




void simple_keyboard_input()
{
    if (kbhit())
      {
            KB_code = getch();
            //cout<<"KB_code = "<<KB_code<<"\n";

            switch (KB_code)
            {

                case KB_ESCAPE:

                    QuitGame=true;

                break;

                case KB_LEFT:
                           //Do something
                    platformX=platformX-4;if(platformX<3) platformX=3;
                break;

                case KB_RIGHT:
                           //Do something     
                    platformX=platformX+4;if(platformX>74) platformX=74;
                break;

                case KB_UP:
                           //Do something                     
                break;

                case KB_DOWN:
                           //Do something                     
                break;

            }        

      }

}


void setcolor(WORD color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}


void gotoxy(int x, int y)
{
  static HANDLE hStdout = NULL;
  COORD coord;

  coord.X = x;
  coord.Y = y;

  if(!hStdout)
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  }

  SetConsoleCursorPosition(hStdout,coord);
}


void clrscr(void)
{
  static HANDLE hStdout = NULL;      
  static CONSOLE_SCREEN_BUFFER_INFO csbi;
  const COORD startCoords = {0,0};   
  DWORD dummy;

  if(!hStdout)               
  {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout,&csbi);
  }

  FillConsoleOutputCharacter(hStdout,
                             ' ',
                             csbi.dwSize.X * csbi.dwSize.Y,
                             startCoords,
                             &dummy);    
  gotoxy(0,0);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接