Wait for a keypress


#1

Absolutely stupid question - and yes RTFM.
How do I make a Julia program pause until a key is pressed?


#2
readline()

should do the trick? (maybe not the most elegant thing to do though…)


#3

Thankyou


#4
julia> wait_for_key(prompt) = (print(stdout, prompt); read(stdin, 1); nothing)
wait_for_key (generic function with 1 method)

julia> wait_for_key("press any key to continue")
press any key to continue

#5

The wait_for_key method only responds on enter for me, am I doing something wrong?


#6

No, I think you need to work with a “raw” terminal. Perhaps

function wait_for_key(; prompt = "press any key", io = stdin)
    setraw!(raw) = ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid},Int32), io.handle, raw)
    print(io, prompt)
    setraw!(true)
    read(io, 1)
    setraw!(false)
    nothing
end

The REPL standard library has some functionality for this, but for terminals.