如何设置不同字符的读数?有人知道解决方案吗?
while true
do
read -sn3 KEY
case "$KEY" in
$'x1b[A') echo "press arrow up";; # OK
$'x1b[B') echo "press arrow down";; # OK
$'x1b[15~') echo "press key F5";; # Problem !!!
esac
done
谢谢
这有点过大!
所以我首先命令您查看dialog
,whiptail
和其他curses
包装器。
,但所有这些都可以成为工作。(我希望你永远不会把它们放在生产阶段; - )
$ char=""; while [ ! "$char" ] ;do
char="$(
(
stty -echo raw
dd bs=1 if=/dev/tty of=/dev/fd/4 >/dev/null 2>&1 &
sleep .1
kill $! 2>/dev/null
stty sane
) 4>&1 >/dev/null
)"
done
$ echo -n "$char" | od -A n -t c
033 [ 2 4 ~
更新(再次)
...假设您不会在小于0.1秒内按两个不同的键。(这甚至是不正确的,但是在对话框菜单上,这可能是...)
从那里:
readKey() {
local char
while [ ! "$char" ] ;do
char="$(
(
stty -echo raw
dd bs=1 if=/dev/tty of=/dev/fd/4 >/dev/null 2>&1 &
sleep .1
kill $! 2>/dev/null
stty sane
) 4>&1 >/dev/null
)"
done
printf -v ${1:-REPLY} "$char"
}
str=""
while [ "$str" != "exit" ] ;do
readKey KEY
str+="$KEY"
[ ${#str} -gt 4 ] && str="${str:${#str}-4}"
case "$KEY" in
$'x1b[A') echo "press arrow up";; # OK
$'x1b[B') echo "press arrow down";; # OK
$'x1b[15~') echo "press key F5";; # Ok, for me.
* ) echo "pressed: '$(echo -n "$KEY"| od -A n -t c)'" ;;
esac
done
击中一些键...
press key F5
press arrow up
press arrow down
pressed: ' 033 [ F'
pressed: ' 033 [ 6 ~'
pressed: ' 033 [ 3 ~'
pressed: ' q'
pressed: ' e'
pressed: ' x'
pressed: ' i'
pressed: ' t'
hit e x i t
用于结束循环。