aboutsummaryrefslogtreecommitdiff
path: root/readkey
diff options
context:
space:
mode:
Diffstat (limited to 'readkey')
-rwxr-xr-xreadkey36
1 files changed, 36 insertions, 0 deletions
diff --git a/readkey b/readkey
new file mode 100755
index 0000000..94836bd
--- /dev/null
+++ b/readkey
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+if [ "a$1" == "a-h" ] || [ "a$1" == "a--help" ]; then
+ cat <<EOF
+Usage: $0
+
+Reads a single key from stdin and parses some key codes such as arrow keys and enter.
+EOF
+fi
+read -rsN1 key
+
+read -rsN1 -t 0.0001 k1
+read -rsN1 -t 0.0001 k2
+read -rsN1 -t 0.0001 k3
+key+="${k1}${k2}${k3}"
+
+case "$key" in
+$'\e[A' | $'\e0A')
+ echo up
+ ;;
+$'\e[D' | $'\e0D')
+ echo left
+ ;;
+$'\e[B' | $'\e0B')
+ echo down
+ ;;
+$'\e[C' | $'\e0C')
+ echo right
+ ;;
+$'\n')
+ echo enter
+ ;;
+*)
+ echo "$key"
+ ;;
+esac