Timed wait for key press

function inkey(s)
    #=
    requires   import REPL   before using
    This is for a single key stroke and returns what key it was
    based on a terminal setting in PuTTY of Ctrl-H for BackSpace and
    Linux function keys
    s is a string
         if null, checks bytesavailable one time
         if parse(Int, s) == 0 , has no time out
         otherwise, time out after parse(Int, s) seconds
    key pressed is returned as:
         Key Char
         number                   return
         ------                   ------
         1 - 7                    Ctrl-A to Ctrl-G
         8                        BackSpace (Ctrl-H)
         9                        Tab (Ctrl-I)
         13                       Enter (Ctrl-M)
         11 - 26                  Ctrl-K to   Ctrl-Z
         0x1b                     Esc
         0x1b 0x5b 0x31 0x31 0x7e F1
         0x1b 0x5b 0x31 0x32 0x7e F2
         0x1b 0x5b 0x31 0x33 0x7e F3
         0x1b 0x5b 0x31 0x34 0x7e F4
         0x1b 0x5b 0x31 0x35 0x7e F5
         0x1b 0x5b 0x31 0x37 0x7e F6
         0x1b 0x5b 0x31 0x38 0x7e F7
         0x1b 0x5b 0x31 0x39 0x7e F8
         0x1b 0x5b 0x32 0x30 0x7e F9
         0x1b 0x5b 0x32 0x31 0x7e F10
         0x1b 0x5b 0x32 0x33 0x7e F11
         0x1b 0x5b 0x32 0x34 0x7e F12
         0x1b 0x5b 0x32 0x7e      Ins
         0x1b 0x5b 0x33 0x7e      Del
         0x1b 0x5b 0x31 0x7e      Home
         0x1b 0x5b 0x34 0x7e      End
         0x1b 0x5b 0x35 0x7e      PgUp
         0x1b 0x5b 0x36 0x7e      PgDn
         0x1b 0x5b 0x41           UpArrow
         0x1b 0x5b 0x42           DnArrow
         0x1b 0x5b 0x43           RightArrow
         0x1b 0x5b 0x44           LeftArrow
    =#
    seconds = -1
    term = REPL.Terminals.TTYTerminal("xterm",stdin,stdout,stderr)
    REPL.Terminals.raw!(term,true)
    Base.start_reading(stdin)
    while true
        sleep(.1)
        ba = bytesavailable(stdin)
        if ba > 0
            str = read(stdin, ba)
            if str[1] == 0x1b
                # We have an Escape
                if length(str) == 1
                   return "ESC"
                end
                if length(str) > 4
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x31 &&
                                         str[5] == 0x7e
                        return "F1"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x32 &&
                                         str[5] == 0x7e
                        return "F2"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x33 &&
                                         str[5] == 0x7e
                        return "F3"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x34 &&
                                         str[5] == 0x7e
                        return "F4"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x35 &&
                                         str[5] == 0x7e
                        return "F5"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x37 &&
                                         str[5] == 0x7e
                        return "F6"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x38 &&
                                         str[5] == 0x7e
                        return "F7"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x39 &&
                                         str[5] == 0x7e
                        return "F8"
                    end
                    if str[2] == 0x5b && str[3] == 0x32 && str[4] == 0x30 &&
                                         str[5] == 0x7e
                        return "F9"
                    end
                    if str[2] == 0x5b && str[3] == 0x32 && str[4] == 0x31 &&
                                         str[5] == 0x7e
                        return "F10"
                    end
                    if str[2] == 0x5b && str[3] == 0x32 && str[4] == 0x33 &&
                                         str[5] == 0x7e
                        return "F11"
                    end
                    if str[2] == 0x5b && str[3] == 0x32 && str[4] == 0x34 &&
                                         str[5] == 0x7e
                        return "F12"
                    end
                    return ""
                end # if length(str) > 4
                if length(str) > 3
                    if str[2] == 0x5b && str[3] == 0x32 && str[4] == 0x7e
                       return "Ins"
                    end
                    if str[2] == 0x5b && str[3] == 0x33 && str[4] == 0x7e
                       return "Del"
                    end
                    if str[2] == 0x5b && str[3] == 0x31 && str[4] == 0x7e
                       return "Home"
                    end
                    if str[2] == 0x5b && str[3] == 0x34 && str[4] == 0x7e
                       return "End"
                    end
                    if str[2] == 0x5b && str[3] == 0x35 && str[4] == 0x7e
                       return "PgUp"
                    end
                    if str[2] == 0x5b && str[3] == 0x36 && str[4] == 0x7e
                       return "PgDn"
                    end
                    return ""
                end # if length(str) > 3
                if str[2] == 0x5b && sr[3] == 0x41
                    return "UpArrow"
                end
                if str[2] == 0x5b && sr[3] == 0x42
                    return "DnArrow"
                end
                if str[2] == 0x5b && sr[3] == 0x43
                    return "RightArrow"
                end
                if str[2] == 0x5b && sr[3] == 0x44
                    return "LeftArrow"
                end
                return ""
                # We had an Escape
            end # if str[1] == 0x1b
            x = convert(Int, str[1])
            if x == 8
                return "BackSpace"
            end
            if x == 9
                return "Tab"
            end
            if x == 13
                return "Enter"
            end
            if x < 27
                y = 64 + x
                return "Ctrl-" * Char(y)
            end
            if x < 128 && x > 31
                return String(str)
            end
            return ""
            # We had a key pressed!
        end # if ba > 0
        if s == ""
           # this is a one time test
           return s
        end
        if s != "0"
            # this means we have a timed wait
            if seconds < 0
                # first time
                seconds = parse(Int, s)
            else
                seconds = seconds - 1
                if seconds == 0
                   return ""
                end
            end
    #   else
            # this means we wait until aa key is pressed
        end
        sleep(.9)
    end # while true
end # function inkey(s)
1 Like