2012-03-03 14 views
9

私はコンソールゲーム(pac-man)をプログラミングしています。私はEnterキーを押さずにユーザー入力を得る方法を知りました。私はインターネットをちょっと見渡して、_getch()についていくつかのものを見つけましたが、現在はもはや最新のものではなく、私がまだC++にはまったく新しいので自分ができない限り、ヘッダファイルは宣言されていません。 これを行うためのコードをどうすれば作成できますか? おかげEnterキーを押さずにユーザー入力を取得する方法はありますか?

+1

呪いのゲームライブラリには、このような関数をサポートしています。http://invisible-island.net/ncurses/man/curs_get_wch.3x.html –

+0

お使いのプラットフォームは何ですか? LinuxまたはWindows? –

+0

Mac。 [C++のキーストロークをキャプチャ] [1] を[1]:私はdscussionはすでにここで行われているのXcode –

答えて

7

はこれが(私はLinux上で午前)私の作品:

#include <stdio.h> 
#include <unistd.h> 
#include <termios.h> 

int main() 
{ 
    struct termios old_tio, new_tio; 
    unsigned char c; 

    /* get the terminal settings for stdin */ 
    tcgetattr(STDIN_FILENO,&old_tio); 

    /* we want to keep the old setting to restore them a the end */ 
    new_tio=old_tio; 

    /* disable canonical mode (buffered i/o) and local echo */ 
    new_tio.c_lflag &=(~ICANON & ~ECHO); 

    /* set the new settings immediately */ 
    tcsetattr(STDIN_FILENO,TCSANOW,&new_tio); 

    do { 
     c=getchar(); 
     printf("%d ",c); 
    } while(c!='q'); 

    /* restore the former settings */ 
    tcsetattr(STDIN_FILENO,TCSANOW,&old_tio); 

    return 0; 
} 

これは、コンソールバッファなしになります。

参照:http://shtrom.ssji.net/skb/getc.html

2

あなたはconio.hライブラリおよび機能_getch()がライブ形式で入力を取得すると、あなたはまた、複数の入力のためのループを設定することができますを使用することができます。

#include<conio.h> 
#include<iostream> 
using namespace std; 
int main() 
{ 
char n='a'; //Just to initialize it. 
while(n!='e') // Will exit if you press e. 
{ 
n=_getch(); 
} 
} 
関連する問題