標籤: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函數的實現