2012-10-24 6 views
0

이 스크립트에서 변수를 추가하는 방법을 알려주십시오. 다들 감사 해요.사용자가 누른 키를 표시하는 방법은 무엇입니까?

#!/bin/bash 

ARROW_UP=??? # I do not know 
ARROW_DOWN=??? # I do not know 
ARROW_LEFT=??? # I do not know 
ARROW_RIGHT=??? # I do not know 
ARROW_ENTER=??? # I do not know 

case "$KEY" in "$ARROW_UP") echo "press the up arrow key" 
       "$ARROW_DOWN") echo "press the down arrow key";; 
       "$ARROW_LEFT") echo "press the left arrow key" 
       "$ARROW_RIGHT") echo "press the right arrow key" 
       "$ARROW_ENTER") echo "press the enter key" 
esac 

답변

1

것은이 같은 시도 (당신은 루프에서 빠져 나오는 경우를 추가 할 수 있습니다) :

#!/bin/bash 

# Reset terminal to current state when we exit. 
trap "stty $(stty -g)" EXIT 

# Disable echo and special characters, set input timeout to 0.2 seconds. 
stty -echo -icanon time 2 || exit $? 

# String containing all keypresses. 
KEYS="" 

# Set field separator to BEL (should not occur in keypresses) 
IFS=$'\a' 

# Input loop. 
while [ 1 ]; do 

    # Read more input from keyboard when necessary. 
    while read -t 0 ; do 
     read -s -r -d "" -N 1 -t 0.2 CHAR && KEYS="$KEYS$CHAR" || break 
    done 

    # If no keys to process, wait 0.05 seconds and retry. 
    if [ -z "$KEYS" ]; then 
     sleep 0.05 
     continue 
    fi 

    # Check the first (next) keypress in the buffer. 
    case "$KEYS" in 
     $'\x1B\x5B\x41'*) # Up 
     KEYS="${KEYS##???}" 
     echo "Up" 
     ;; 
     $'\x1B\x5B\x42'*) # Down 
     KEYS="${KEYS##???}" 
     echo "Down" 
     ;; 
     $'\x1B\x5B\x44'*) # Left 
     KEYS="${KEYS##???}" 
     echo "Left" 
     ;; 
     $'\x1B\x5B\x43'*) # Right 
     KEYS="${KEYS##???}" 
     echo "Right" 
     ;;   
    esac 
done 

자세한 내용 here.

+0

내가 표시 :-(만 루트 @ 메일을 작동하지 않습니다 ~ # /test.sh ^ [[A^[[D^[[B^[[C^[[D^[[B^[[C 당신은 가고 있습니까? – Charlie

+0

일부 코드를 잊어 버렸습니다. 업데이트 된 답변을 확인하고 끝에있는 링크를 읽으십시오. –

+0

대단히 감사합니다 ... – Charlie

2

짧은 대답은 할 수 없습니다. 실제 프로그래밍 언어를 사용하십시오.

일부 지나치게 복잡한 솔루션은 here이지만 그 중 일부는 보증하지 않습니다. 같은 페이지 하단의 KEYBD 트랩 솔루션은 좋지만 ksh93이 필요합니다.

관련 문제