# Wait for a keypress

**URL:** <https://discourse.julialang.org/t/wait-for-a-keypress/20218>\
**Category:** New to Julia\
**Tags:** io\
**Created:** [January 29, 2019, 5:10am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218 "2019-01-29T05:10:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [January 29, 2019, 5:10am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/1 "2019-01-29T05:10:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [January 29, 2019, 7:35am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/2 "2019-01-29T07:35:03Z")

</div>

```julia
readline()

```

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

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [January 29, 2019, 10:58am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/3 "2019-01-29T10:58:05Z")

</div>

Thankyou

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 29, 2019, 11:11am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/4 "2019-01-29T11:11:00Z")

</div>

```julia
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

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [January 29, 2019, 11:37am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/5 "2019-01-29T11:37:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 29, 2019, 11:45am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/6 "2019-01-29T11:45:06Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [March 21, 2024, 12:57pm UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/7 "2024-03-21T12:57:57Z")

</div>

This thread is the top hit when searching for “julia wait for key” on google and the simple solution

```julia
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

```

is exactly what I needed, and for me it’s ok that Enter/Return is to be pressed.

But, perhaps it’s Windows, when used in a script (not REPL) after the first call stdin isn’t empty. Consecutive calls will not wait, because stdin still has something to read.

So an improvement, which works for me even for consecutive calls, would be:

```julia
wait_for_key(prompt) = (println(stdout, prompt);
	read(stdin, 1); 
	while bytesavailable(stdin)>0 read(stdin, Char); end; 
	nothing)

```

which reads all still available bytes from stdin until it’s really empty.

---

<div class="post-metadata">

**Author:** ![alancummings](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alancummings/32/46788_2.png) [@alancummings](https://discourse.julialang.org/u/alancummings)\
**Post date:** [August 9, 2024, 7:06pm UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/8 "2024-08-09T19:06:26Z")

</div>

If you are OK pressing Enter, but need to discard all (or most) of the characters, this might be simpler:

# discard all

`readline(stdin)`

# keep first character, even if it is Enter

`c, _... = readline(stdin, keep=true)`

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 9, 2024, 8:51pm UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/9 "2024-08-09T20:51:22Z")

</div>

This [SO solution](https://stackoverflow.com/a/56893331/14071775) from @bkamins is the only one that works for me in Windows.  
_NB: it is the same as @TamasPapp’s above, but without typo._

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [August 10, 2024, 1:59am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/11 "2024-08-10T01:59:59Z")

</div>

Can anyone write a single julia function called press\_any\_key\_to\_continue()

such that

1. Does NOT require user to press ENTER/RETURN button
2. Works on Unix,Windows and Mac
3. (hopefully) put it in a package that user can use easily. (; like “use easypackage” )

Regards…

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 10, 2024, 7:40am UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/12 "2024-08-10T07:40:53Z")

</div>

All that is missing from your list is a package. Since the code is by @bkamins, it would be best if he put it in a mini-package, but if he does not want to I can package it up and register it with his permission.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 28, 2025, 6:08pm UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/13 "2025-05-28T18:08:05Z")

</div>

Sorry for a late reply - if this is useful I am OK that you package it.

---

<div class="post-metadata">

**Author:** ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)\
**Post date:** [November 15, 2025, 7:13pm UTC](https://discourse.julialang.org/t/wait-for-a-keypress/20218/14 "2025-11-15T19:13:55Z")

</div>

The SO solution linked above uses  
`REPL.TerminalMenus.terminal`  
which disappeared circa 2024-03-12:

> <https://github.com/JuliaLang/julia/pull/53704/files>

Bummer for me because I had been using it

> <https://github.com/JeffFessler/MIRTjim.jl/blob/main/src/prompt.jl>

so now I have to update my repo. But that’s to be expected for using non-public internals…

The update is to use  
`REPL.TerminalMenus.default_terminal()`
