shell script respond to keypress

后端 未结 5 1595
逝去的感伤
逝去的感伤 2020-12-06 00:25

I have a shell script that essentially says something like

while true; do
    read -r input
    if [\"$input\" = \"a\"]; then 
        echo \"hello world\"          


        
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 00:49

    I have a way to do this in my project: https://sourceforge.net/p/playshell/code/ci/master/tree/source/keys.sh

    It reads a single key everytime key_readonce is called. For special keys, a special parsing loop would run to also be able to parse them.

    This is the crucial part of it:

    if read -rn 1 -d '' "${T[@]}" "${S[@]}" K; then
        KEY[0]=$K
    
        if [[ $K == $'\e' ]]; then
            if [[ BASH_VERSINFO -ge 4 ]]; then
                T=(-t 0.05)
            else
                T=(-t 1)
            fi
    
            if read -rn 1 -d '' "${T[@]}" "${S[@]}" K; then
                case "$K" in
                \[)
                    KEY[1]=$K
    
                    local -i I=2
    
                    while
                        read -rn 1 -d '' "${T[@]}" "${S[@]}" "KEY[$I]" && \
                        [[ ${KEY[I]} != [[:upper:]~] ]]
                    do
                        (( ++I ))
                    done
                    ;;
                O)
                    KEY[1]=$K
                    read -rn 1 -d '' "${T[@]}" 'KEY[2]'
                    ;;
                [[:print:]]|$'\t'|$'\e')
                    KEY[1]=$K
                    ;;
                *)
                    __V1=$K
                    ;;
                esac
            fi
        fi
    
        utils_implode KEY __V0
    

提交回复
热议问题