2011-09-19 89 views
41

我無法在Linux中找到conio.h的等效頭文件。什麼等同於Linux中的getch()&getche()?

在Linux中有沒有getch() & getche()函數的選項?

我想製作一個開關盒的基本菜單,用戶只需按一個鍵就可以給出他的選擇&過程應該提前。我不想讓用戶按他的選擇後按ENTER鍵。

+0

看看這些答案。可以幫助你:http://stackoverflow.com/questions/1513734/problem-with-kbhitand-getch-for-linux –

回答

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

static struct termios old, new; 

/* Initialize new terminal i/o settings */ 
void initTermios(int echo) 
{ 
    tcgetattr(0, &old); /* grab old terminal i/o settings */ 
    new = old; /* make new settings same as old settings */ 
    new.c_lflag &= ~ICANON; /* disable buffered i/o */ 
    new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ 
    tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ 
} 

/* Restore old terminal i/o settings */ 
void resetTermios(void) 
{ 
    tcsetattr(0, TCSANOW, &old); 
} 

/* Read 1 character - echo defines echo mode */ 
char getch_(int echo) 
{ 
    char ch; 
    initTermios(echo); 
    ch = getchar(); 
    resetTermios(); 
    return ch; 
} 

/* Read 1 character without echo */ 
char getch(void) 
{ 
    return getch_(0); 
} 

/* Read 1 character with echo */ 
char getche(void) 
{ 
    return getch_(1); 
} 

/* Let's test it out */ 
int main(void) { 
    char c; 
    printf("(getche example) please type a letter: "); 
    c = getche(); 
    printf("\nYou typed: %c\n", c); 
    printf("(getch example) please type a letter..."); 
    c = getch(); 
    printf("\nYou typed: %c\n", c); 
    return 0; 
} 

只需複製這些功能並使用它。我很久以前就在谷歌上找到了這個代碼片段,並且我保存了它,最後我在很長一段時間後爲你打開了它!希望它有幫助!由於

+0

讓我試試,如果它的工作,那麼我會接受你的答案...!提前預告.. !! –

+0

嘿謝謝你...你幫了我很多...... !!! –

+3

很高興聽到這個消息,更多的幫助,只需打我們@stackoverflow,我們會幫助你!謝謝! – niko

4

ncurses庫中有一個getch()函數。 你可以通過安裝ncurses-dev包來獲得它。

+1

在一種情況下,我不想安裝新的東西,..任何其他選項? –

+2

您需要編寫自己的函數 - 如niko所示。 –

7

我建議你使用curses.h或ncurses.h這些實現鍵盤管理例程,包括getch()。您有幾個選項來改變getch的行爲(即等待按鍵或不按鍵)。

26
char getch(){ 
    /*#include <unistd.h> //_getch*/ 
    /*#include <termios.h> //_getch*/ 
    char buf=0; 
    struct termios old={0}; 
    fflush(stdout); 
    if(tcgetattr(0, &old)<0) 
     perror("tcsetattr()"); 
    old.c_lflag&=~ICANON; 
    old.c_lflag&=~ECHO; 
    old.c_cc[VMIN]=1; 
    old.c_cc[VTIME]=0; 
    if(tcsetattr(0, TCSANOW, &old)<0) 
     perror("tcsetattr ICANON"); 
    if(read(0,&buf,1)<0) 
     perror("read()"); 
    old.c_lflag|=ICANON; 
    old.c_lflag|=ECHO; 
    if(tcsetattr(0, TCSADRAIN, &old)<0) 
     perror ("tcsetattr ~ICANON"); 
    printf("%c\n",buf); 
    return buf; 
} 

複製此功能,並使用它,不要忘了包括

remove the last printf if you dont want the char to be displayed

+0

哦很好的解決方法...!將在星期一看我什麼時候會在工作..! –

+1

@ mr-32這是與windows的visual studio使用的getch()完全等價的,如果這完全適合您的需要,請減去此函數的最後一行的printf() –

+0

@ mr-32,請標記爲正確答案 –

-1

getch()更換爲stdio.h聲明getchar()。在Windows和Linux上可以使用getchar()

+21

'getch()'和'getchar()'之間有一些(稍微重要的)區別。 1)只要按下鍵,'getch()'立即返回。 'getchar()'讓你無限期地輸入,直到你輸入一個EOL。 2)'getch()'不會在屏幕上打印任何東西。 'getchar()'將你輸入的所有內容寫入屏幕(甚至是EOL)。如果這兩個差異對用戶不重要,那麼可以真正使用'getchar()'作爲替換,否則這可能不是最好的想法。 –

0

如其他答案中所述,您可以在linux中使用curses.h庫。

您可以在Ubuntu安裝:

sudo易於得到更新

命令和apt-get安裝ncurses的開發

我代爲安裝部分來自here

相關問題