| 1 | #!/bin/bash |
| 2 | # Author: Sigurd Solaas, 20 Apr 2011 |
| 3 | # Used in ABS Guide with permission. |
| 4 | # Requires version 4.2+ of Bash. |
| 5 | |
| 6 | key="no value yet" |
| 7 | while true; do |
| 8 | clear |
| 9 | echo "Bash Extra Keys Demo. Keys to try:" |
| 10 | echo |
| 11 | echo "* Insert, Delete, Home, End, Page_Up and Page_Down" |
| 12 | echo "* The four arrow keys" |
| 13 | echo "* Tab, enter, escape, and space key" |
| 14 | echo "* The letter and number keys, etc." |
| 15 | echo |
| 16 | echo " d = show date/time" |
| 17 | echo " q = quit" |
| 18 | echo "================================" |
| 19 | echo |
| 20 | |
| 21 | # Convert the separate home-key to home-key_num_7: |
| 22 | if [ "$key" = $'\x1b\x4f\x48' ]; then |
| 23 | key=$'\x1b\x5b\x31\x7e' |
| 24 | # Quoted string-expansion construct. |
| 25 | fi |
| 26 | |
| 27 | # Convert the separate end-key to end-key_num_1. |
| 28 | if [ "$key" = $'\x1b\x4f\x46' ]; then |
| 29 | key=$'\x1b\x5b\x34\x7e' |
| 30 | fi |
| 31 | |
| 32 | case "$key" in |
| 33 | $'\x1b\x5b\x32\x7e') # Insert |
| 34 | echo Insert Key |
| 35 | ;; |
| 36 | $'\x1b\x5b\x33\x7e') # Delete |
| 37 | echo Delete Key |
| 38 | ;; |
| 39 | $'\x1b\x5b\x31\x7e') # Home_key_num_7 |
| 40 | echo Home Key |
| 41 | ;; |
| 42 | $'\x1b\x5b\x34\x7e') # End_key_num_1 |
| 43 | echo End Key |
| 44 | ;; |
| 45 | $'\x1b\x5b\x35\x7e') # Page_Up |
| 46 | echo Page_Up |
| 47 | ;; |
| 48 | $'\x1b\x5b\x36\x7e') # Page_Down |
| 49 | echo Page_Down |
| 50 | ;; |
| 51 | $'\x1b\x5b\x41') # Up_arrow |
| 52 | echo Up arrow |
| 53 | ;; |
| 54 | $'\x1b\x5b\x42') # Down_arrow |
| 55 | echo Down arrow |
| 56 | ;; |
| 57 | $'\x1b\x5b\x43') # Right_arrow |
| 58 | echo Right arrow |
| 59 | ;; |
| 60 | $'\x1b\x5b\x44') # Left_arrow |
| 61 | echo Left arrow |
| 62 | ;; |
| 63 | $'\x09') # Tab |
| 64 | echo Tab Key |
| 65 | ;; |
| 66 | $'\x0a') # Enter |
| 67 | echo Enter Key |
| 68 | ;; |
| 69 | $'\x1b') # Escape |
| 70 | echo Escape Key |
| 71 | ;; |
| 72 | $'\x20') # Space |
| 73 | echo Space Key |
| 74 | ;; |
| 75 | d) |
| 76 | date |
| 77 | ;; |
| 78 | q) |
| 79 | echo Time to quit... |
| 80 | echo |
| 81 | exit 0 |
| 82 | ;; |
| 83 | *) |
| 84 | echo You pressed: \'"$key"\' |
| 85 | ;; |
| 86 | esac |
| 87 | |
| 88 | echo |
| 89 | echo "================================" |
| 90 | |
| 91 | unset K1 K2 K3 |
| 92 | read -s -N1 -p "Press a key: " |
| 93 | K1="$REPLY" |
| 94 | read -s -N2 -t 0.001 |
| 95 | K2="$REPLY" |
| 96 | read -s -N1 -t 0.001 |
| 97 | K3="$REPLY" |
| 98 | key="$K1$K2$K3" |
| 99 | |
| 100 | done |
| 101 | |
| 102 | exit $? |