getch

Korn Shell getch: Capture a single key press without requiring carriage return or enter key.

See visualSelect menu system for a full implementation of "getch" capturing function keys, arrow keys, and other cursor movement keys.



################################################################
####    Korn Shell function to read one character from
####    standard input (STDIN) without requiring a carriage
####    return.  This function would typically be used in a
####    shell script to detect a key press.
####
####    Load this file into your current environment as follows:
####
####    . ./getch
####
####    Thats "dot-space-dot-slash-getch-dot-sh"
####
####    You will then be able to issue the command "getch"
####    from your current environment to retrieve one character.
####
####    SYNTAX: getch [-q]
####
####                   -q = quiet mode, no output
####
####    AUTHOR: Dana French (dfrench@mtxia.com)
####
####
#### See http://www.mtxia.com/js/Downloads/Scripts/Korn/Functions/visualSelect/index.shtml
#### visualSelect menu system for a full implementation of "getch"
#### capturing function keys, arrow keys, and other cursor movement keys.
####
################################################################
function getch {
    typeset TMP_GETCH
    typeset STAT_GETCH="0"
    stty raw
    TMP_GETCH=`dd bs=1 count=1 2> /dev/null`
    STAT_GETCH="${?}"
    stty -raw

    if [[ "_${1}" != "_-q" ]]
    then
        print -r -- "${TMP_GETCH}"
    fi
    return ${STAT_GETCH}
}