linux系統下的getch和getche函數的實現

來源:互聯網
上載者:User

標籤:c   io   gcc   linux   

首先要知道:getch與getche是在conio.h這個標頭檔裡面的,但是,但是conio.h這個檔案不是c語言的標準庫檔案,所以在linux的gcc下是沒有這個標頭檔的,而我們大多數人使用的主要是getch與getche函數,因為getchar函數的功能不是很強大,言歸正傳,先灌輸一些關於linux的知識
stty是一個用來改變並列印終端行設定的常用命令。
1 ,在命令列下,禁止輸出大寫的方法
stty -icanon 設定一次性讀完操作,如使用getchar()讀操作,不需要按enter
stty icanon 取消上面設定
stty -a 查看 資訊
stty -echo 設定命令不被顯示
stty echo 取消不顯設定
stty raw -echo
stty raw echo
stty iuclc #開啟
stty -iuclc #恢複
2,在命令列下禁止輸出小寫
stty olcuc #開啟
stty -olcuc#恢複
3,打首先要知道:getch與getche是在conio.h這個標頭檔裡面的,但是,但是印出終端的行數和列數
stty size
4,改變ctrl+D的方法:
stty eof "string"
系統預設是ctrl+D來表示檔案的結束,而通過這種方法,可以改變!
5,屏蔽顯示
stty -echo #禁止回顯
stty echo #開啟回顯
測試方法:
stty -echo;read;stty echo;read
6,忽略斷行符號符
stty igncr #開啟
stty -igncr#恢複
7.定時輸入
timeout_read()
{
 
timeout=$1
 
old_stty_settings=`stty -g`  #save current settings
 
stty -icanon min 0 time 100  #set 10seconds,not 100seconds
 
eval read varname  #=read $varname
 
stty "$old_stty_settings"  #recover settings
 
}
 
更簡單的方法就是利用read命令的-t選項
read -t 10 varname
可能上面說得有些多,那就開始上代碼把,注意,乾貨來了
char getch()
{
char c;
system("stty -echo");
system("stty -icanon");
c=getchar();
system("stty icanon");
system("stty echo");
reutrn c;
}
char getche()
{
char c;
system("stty -icanon");
c=getchar();
system("stty icanon");
return c; 
}
ok完成了,你可以直接用一個.h檔案把上面的代碼寫進去然後就可以直接使用了,謝謝

linux系統下的getch和getche函數的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.