Skip to content

Instantly share code, notes, and snippets.

@kurahaupo
kurahaupo / readkey
Last active December 28, 2015 10:59
How to read ANSI keyboard sequences in Bash
readkey() {
local char
IFS= \
read -rs -d '' -N1 key || return $? # use -n1 on older versions
while
case "$key" in
('') key=$'\n' ; break ;; # compensate for bug
( $'\e[M'??? ) break ;; # mouse report
( $'\e' | $'\e[' | $'\e['[?O] | $'\e['*[\;0-9] | $'\e[M'* | $'\eO' ) ;;
( [$'\xc0'-\$'xfe'] \