如何显示用户按下的键

  • 本文关键字:用户 何显示 显示 bash
  • 更新时间 :
  • 英文 :


请帮助如何在此脚本中添加变量,有人知道吗?谢谢大家。

#!/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

尝试这样的东西(您可能需要添加一个案例以脱离循环):

#!/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
      $'x1Bx5Bx41'*) # Up
        KEYS="${KEYS##???}"
        echo "Up"
        ;;
      $'x1Bx5Bx42'*) # Down
        KEYS="${KEYS##???}"
        echo "Down"
        ;;
      $'x1Bx5Bx44'*) # Left
        KEYS="${KEYS##???}"
        echo "Left"
        ;;
      $'x1Bx5Bx43'*) # Right
        KEYS="${KEYS##???}"
        echo "Right"
        ;;          
    esac
done

更多详细信息。

简短的答案是您不能。使用真实的编程语言。

这里可以找到一些过于复杂的解决方案,但我不认可它们。同一页面底部的键BD陷阱解决方案很好,但需要ksh93。

相关内容

  • 没有找到相关文章

最新更新