Created
September 20, 2022 13:14
-
-
Save nkh/8fdfa4e5475953c71858feff8a1a096e to your computer and use it in GitHub Desktop.
Bash code to handle keyboard input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
get_key() | |
{ | |
OIFS="$IFS" ; IFS= | |
[[ "$1" ]] && { read -rsn 1 -t "$1" || REPLY=ERROR_$? ; } || { read -rsn 1 || REPLY=ERROR_$? ; } | |
read -rsn 4 -t 0.004 E1 E2 E3 E4 | |
case "$REPLY$E1$E2$E3$E4" in | |
$'\e' ) KEY_NAME=ESCAPE ;; | |
$'\177' ) KEY_NAME=BACKSPACE ;; | |
$'\\' ) KEY_NAME=BACKSLASH ;; | |
$' ' ) KEY_NAME=SPACE ;; | |
$'*' ) KEY_NAME=STAR ;; | |
$'@' ) KEY_NAME=AT ;; | |
$"'" ) KEY_NAME=QUOTE ;; | |
$'"' ) KEY_NAME=DQUOTE ;; | |
$'\t' ) KEY_NAME=TAB ;; | |
$'' ) KEY_NAME=ENTER ;; | |
$'\e[A' | $'\e[OA' ) KEY_NAME=UP ;; | |
$'\e[B' | $'\e[0B' ) KEY_NAME=DOWN ;; | |
$'\e[C' | $'\e[OC' ) KEY_NAME=RIGHT ;; | |
$'\e[D' | $'\e[OD' ) KEY_NAME=LEFT ;; | |
$'\e[2~' ) KEY_NAME=INS ;; | |
$'\e[3~' ) KEY_NAME=DEL ;; | |
$'\e[1~' | $'\e[H' ) KEY_NAME=HOME ;; | |
$'\e[4~' | $'\e[F' ) KEY_NAME=END ;; | |
$'\e[5~' ) KEY_NAME=PGUP ;; | |
$'\e[6~' ) KEY_NAME=PGDN ;; | |
$'\e[11~' | $'\e[[A' | $'\eOP' ) KEY_NAME=F1 ;; | |
$'\e[12~' | $'\e[[B' | $'\eOQ' ) KEY_NAME=F2 ;; | |
$'\e[13~' | $'\e[[C' | $'\eOR' ) KEY_NAME=F3 ;; | |
$'\e[14~' | $'\e[[D' | $'\eOS' ) KEY_NAME=F4 ;; | |
$'\e['15~ | $'\e[''[E' ) KEY_NAME=F5 ;; | |
$'\e['17~ | $'\e[''[F' ) KEY_NAME=F6 ;; | |
$'\e['18~ ) KEY_NAME=F7 ;; | |
$'\e['19~ ) KEY_NAME=F8 ;; | |
$'\e['20~ ) KEY_NAME=F9 ;; | |
$'\e['21~ ) KEY_NAME=F10 ;; | |
$'\e['23~ ) KEY_NAME=F11 ;; | |
$'\e['24~ ) KEY_NAME=F12 ;; | |
$'\001' ) KEY_NAME=CTL-A ;; | |
$'\002' ) KEY_NAME=CTL-B ;; | |
#$'\0??' ) KEY_NAME=CTL-C ;; | |
$'\004' ) KEY_NAME=CTL-D ;; | |
$'\005' ) KEY_NAME=CTL-E ;; | |
$'\006' ) KEY_NAME=CTL-F ;; | |
$'\a' ) KEY_NAME=CTL-G ;; | |
$'\b' ) KEY_NAME=CTL-H ;; | |
#$'\0??' ) KEY_NAME=CTL-I ;; | |
#$'\0??' ) KEY_NAME=CTL-J ;; | |
$'\v' ) KEY_NAME=CTL-K ;; | |
$'\f' ) KEY_NAME=CTL-L ;; | |
#$'\0??' ) KEY_NAME=CTL-M ;; | |
$'\016' ) KEY_NAME=CTL-N ;; | |
$'\017' ) KEY_NAME=CTL-O ;; | |
$'\020' ) KEY_NAME=CTL-P ;; | |
$'\021' ) KEY_NAME=CTL-Q ;; | |
$'\022' ) KEY_NAME=CTL-R ;; | |
$'\023' ) KEY_NAME=CTL-S ;; | |
$'\024' ) KEY_NAME=CTL-T ;; | |
$'\025' ) KEY_NAME=CTL-U ;; | |
$'\026' ) KEY_NAME=CTL-V ;; | |
$'\027' ) KEY_NAME=CTL-W ;; | |
$'\030' ) KEY_NAME=CTL-X ;; | |
$'\031' ) KEY_NAME=CTL-Y ;; | |
#$'\0??' ) KEY_NAME=CTL-Z ;; | |
* ) KEY_NAME="$REPLY" ;; | |
esac | |
[[ "$2" ]] && printf "%-10s < %-5q | %-5q | %-5q | %-5q | %-5q >\n" "$KEY_NAME" "${key}" "$E1" "$E2" "$E3" "$E4" | |
IFS="$OIFS" | |
} | |
#key() { while : ; do get_key 0.3 DEBUG ; done ; } | |
#key() { while : ; do get_key ; echo $KEY_NAME ; done ; } | |
# vim: set filetype=bash : | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment