0

How to get keystrokes in a bash terminal on MacOS?

I found the code here (author:Chris Redford): enter link description here

How to add ESC key to exit loop? Thank you for your help

Modified code:

#!/bin/bash

ESC=$'\033'
# distinguish between enter and space
IFS=''

while true; do
    read -rsn1 a
    # is the first character ESC?
    if [[ $ESC == $a ]]; then
        read -rsn2 b
    fi

    input=$a$b
    unset b

    case $input in
        $ESC[A) echo UP ;;
        $ESC[B) echo DOWN ;;
        $ESC[C) echo RIGHT ;;
        $ESC[D) echo LEFT ;;

        $ESC) break ;; ### This line does not work ###
        

        '') echo ENTER ;;
        q) break ;;
    esac
done

martpet
  • 1
  • 1
  • You can't; ESC will most probably intercepted by the terminal before you can read it, so you should use an other key, for ex. `q` for "quit" – Fravadona Oct 10 '22 at 10:57
  • Thanks, I need to use the ESC key.. Other options? Another script? – martpet Oct 10 '22 at 11:48

0 Answers0