playground/bash/keyboard-keys.sh

122 lines
3.8 KiB
Bash

#!/bin/bash
# As of now, this can't detect escape key
# If IFS is not defined, Enter and Space keys would be indistinguishable
# https://stackoverflow.com/a/35468493
IFS=
while true; do
read -rsn1 inp # get 1 character
if [[ $inp == $'\u1b' ]]; then
# Escape or Alt
read -rsn1 inp # read 1 more char
case $inp in
'f') echo 'M-f' ;; # ^[f
'b') echo 'M-b' ;; # ^[b
'O')
read -rsn1 inp # read 1 more char
case $inp in
'P') echo 'F1' ;; # ^[OP
'Q') echo 'F2' ;; # ^[OQ
'R') echo 'F3' ;; # ^[OR
'S') echo 'F4' ;; # ^[OS
esac;;
'[')
read -rsn1 inp # read 1 more char
case $inp in
'A') echo 'Up' ;; # ^[[A
'B') echo 'Down' ;; # ^[[B
'D') echo 'Left' ;; # ^[[D
'C') echo 'Right' ;; # ^[[C
'5') read -rsn1 inp; echo 'Page up' ;; # ^[[5~
'6') read -rsn1 inp; echo 'Page down' ;; # ^[[6~
'2')
read -rsn1 inp
case $inp in
'~') echo 'Insert' ;;
'0') read -rsn1 inp; echo 'F9' ;;
'1') read -rsn1 inp; echo 'F10' ;;
'3') read -rsn1 inp; echo 'F11' ;;
'4') read -rsn1 inp; echo 'F12' ;;
esac ;;
'1')
read -rsn1 inp
case $inp in
'~') echo 'Home' ;;
#'1') read -rsn1 inp; echo '-1' ;;
#'2') read -rsn1 inp; echo '-2' ;;
#'3') read -rsn1 inp; echo '-3' ;;
#'4') read -rsn1 inp; echo '-4' ;;
'5') read -rsn1 inp; echo 'F5' ;;
#'6') read -rsn1 inp; echo '-6' ;;
'7') read -rsn1 inp; echo 'F6' ;;
'8') read -rsn1 inp; echo 'F7' ;;
'9') read -rsn1 inp; echo 'F8' ;;
esac ;;
'4') read -rsn1 inp; echo 'End' ;; # ^[[4~
'3') read -rsn1 inp; echo 'Delete' ;; # ^[[3~
# https://stackoverflow.com/questions/35429671/detecting-key-press-within-bash-scripts
*) ;;
esac
esac
else
case $inp in
$'\0') echo 'C-@' ;; # Don't know how to invoke this
$'\1') echo 'C-a' ;; # Can't use from screen
$'\2') echo 'C-b' ;; # ✓
$'\3') echo 'C-c' ;; # Can't use. Terminate program
$'\4') echo 'C-d' ;; # ✓
$'\5') echo 'C-e' ;; # ✓
$'\6') echo 'C-f' ;; # ✓
$'\7') echo 'C-g' ;; # ✓
# $'\8') echo 'C-h' ;;
# $'\9') echo 'C-??' ;;
# $'\10') echo 'C-@' ;; # Duplicate ??
$'\11') echo 'C-i' ;; # ✓
# $'\12') echo 'C-L' ;; # ✗
$'\13') echo 'C-k' ;; # ✓
$'\14') echo 'C-l' ;; # ✓
# $'\15') echo 'C-@' ;; # not o? Duplicate??
$'\16') echo 'C-n' ;; # ✓
$'\17') echo 'C-o' ;; # ✓
# $'\18') echo 'C-x' ;; # ✗
# $'\19') echo 'C-s' ;; # ?
$'\20') echo 'C-p' ;; # ✓
# $'\21') echo 'C-21' ;; # ✗
$'\22') echo 'C-r' ;; # ✓
# $'\23') echo 'C-w' ;; # ✗
$'\24') echo 'C-t' ;; # ✓
$'\25') echo 'C-u' ;; # ✓
$'\26') echo 'C-v' ;; # ✓
$'\177') echo 'Backspace' ;;
' ') echo 'Space' ;;
$'\0a') echo 'Enter' ;; # Also same as C-j
*) echo "$inp";;
esac
fi
done
# read -rsn2 inp # read 2 more chars
# case $inp in
# # '') echo 'Esc' ;; # ^[[A
# '[A') echo 'Up' ;; # ^[[A
# '[B') echo 'Down' ;; # ^[[B
# '[D') echo 'Left' ;; # ^[[D
# '[C') echo 'Right' ;; # ^[[C
# '[5') read -rsn1 inp; echo 'Page up' ;; # ^[[5~
# '[6') read -rsn1 inp; echo 'Page down' ;; # ^[[6~
# *) echo "$inp";;
# esac
# else
# case $inp in
# $'\6') echo 'C-f' ;;
# $'\2') echo 'C-b' ;;
# *) echo "$inp";;
# esac
# fi
# done