shell读取键盘上下左右的问题?


方向键值查出来的,应该没错。不知哪里有错,请教。
代码如下

   
  #!/bin/sh
  

uparrowkey='^[[A'
downarrowkey='^[[B'
leftarrowkey='^[[D'
rightarrowkey='^[[C'

#---------------------------------------------------
echo "Press a control key then hit return"
read KEY
echo $KEY
case $KEY in
$uparrowkey)
echo "UP Arrow"
;;
$downarrowkey)
echo "DOWN Arrow"
;;
$leftarrowkey)
echo "LEFT Arrow"
;;
$rightarrowkey)
echo "RIGHT Arrow"
;;
*)
echo "UNKNOW KEY $Key"
;;
esac

Linux shell

埃尼默兽瞳 12 years, 4 months ago

用cat -v看到这些方向键输入的字符,长度是3,只须判断最后一个字符,即A,B,C,D,即可:

   
  #!/bin/sh
  

echo "Press a control key then hit return"

while :
do
read -s -n 1 KEY

case ${KEY[0]} in
"A")
echo "UP Arrow"
;;
"B")
echo "DOWN Arrow"
;;
"D")
echo "LEFT Arrow"
;;
"C")
echo "RIGHT Arrow"
;;
esac
done

mldicon answered 12 years, 4 months ago

Your Answer