aboutsummaryrefslogtreecommitdiff
path: root/readkey
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-01-13 00:01:56 +0100
committernea <romangraef@gmail.com>2022-01-13 00:01:56 +0100
commitd9031203e22e4ef381c482b4594521323647487e (patch)
tree1ea7dcfb53840a42f10d19cf25317138e497911a /readkey
downloadscripts-d9031203e22e4ef381c482b4594521323647487e.tar.gz
scripts-d9031203e22e4ef381c482b4594521323647487e.tar.bz2
scripts-d9031203e22e4ef381c482b4594521323647487e.zip
Initial commit
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