# How to Detect Key Down Events?

**URL:** https://discourse.julialang.org/t/how-to-detect-key-down-events/95011
**Category:** General Usage
**Tags:** input-output
**Created:** [February 22, 2023, 11:28am UTC](https://discourse.julialang.org/t/how-to-detect-key-down-events/95011 "2023-02-22T11:28:35Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [February 22, 2023, 12:53pm UTC](https://discourse.julialang.org/t/how-to-detect-key-down-events/95011/2 "2023-02-22T12:53:10Z")

</div>

I found a similar question on [stackoverflow](https://stackoverflow.com/questions/56888266/how-to-read-keyboard-inputs-at-every-keystroke-in-julia).

Based on that you could do something like this

```julia
using REPL

# Run listener as separate task using channels, put keypresses in channel for main loop
function key_listener(c::Channel)
    t = REPL.TerminalMenus.terminal
    while true
        REPL.Terminals.raw!(t, true) || error("unable to switch to raw mode")
        keypress = Char(REPL.TerminalMenus.readkey(t.in_stream))
        REPL.Terminals.raw!(t, false) || error("unable to switch back from raw mode")
        put!(c, keypress)
    end
end

function main()
    channel = Channel(key_listener, 10) # Start task, 10 is buffer size for channel
    stop = false
    while !stop
        println("Doing some long calculation")
        sleep(1)
        while !isempty(channel) # Process all keypresses
            c = take!(channel)
            if c == 'q'
                println("quitting")
                stop = true
                close(channel)
                break
            else
                println("$c is not a recognized command")
            end
        end
    end
end

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-detect-key-down-events/95011)._
