謝謝netman大哥及jeffer兄的解答:
實做後發現有點小問題
getc()
{
stty raw
eval $1='`dd bs=1 count=1 2>/dev/null`'
stty cooked
}
echo "press a key"
getc ans
echo "this is you type:"
echo $ans
echo "bye !"
無法關閉輸入鍵的回應
輸出如下:
#press a key
gthis is you type:
g
bye !
經過一番測試修正如下:
getc()
{
stty raw -echo
eval $1=`dd bs=1 count=1 2>/dev/null`
stty cooked echo
}
echo "press a key"
getc ans
echo "this is you type:"
echo $ans
echo "bye!"
大致正常了,但是如果按下的是兩碼的功能鍵還是有Bug要處理
> #!/bin/sh
>
> echo "Enter a keystroke"
> oldstty=`stty -g`
> stty -icanon -echo min 1 time 0
> key=`dd bs=4 count=1 <&0 2>/dev/null`
> stty $oldstty
> echo "key is" $key
小州兄這段程式則避開了這個問題
只是執行後所有KEYIN均不顯示了
只好綜合兩支程式
getc()
{
oldstty=`stty -g`
stty raw -echo min 1 time 0
eval $1=`dd bs=4 count=1 <&0 2>/dev/null`
stty $oldstty
stty cooked echo
}
echo "press a key"
getc ans
echo "this is you type:"
echo $ans
echo "bye!"
再研究如何取得功能鍵碼嚕
