This gets close to what you want, and works in a script (on Julia v0.4.6):

exitflag = [false]

@async while true
g = utf8(readavailable(STDIN))
println(g)
if g[1] == 'q'
println("Exiting task...")
exitflag[1] = true
break
end
end

while true
yield()
if exitflag[1]
break
end
end


This code prints what you type, and both the task and main loop exit when
you press 'q'. It's less than ideal, though, because it needs a newline
("enter") before it reads STDIN, and what you type is echoed to the screen
before it's read by `readavailable()`. These issues should be fixable. HTH,

-- mb

On Mon, Jul 25, 2016 at 9:37 PM, <adam...@whitman.edu> wrote:

> I am trying to write a simple program that prints a message every time the
> user presses a key. The trick is that I would like this to happen
> *regardless* of what the program is doing otherwise. Eventually, I would
> like to use this method to interrupt a long running process, and allow me
> to check it's progress and modify parameters before restarting it, however
> I am struggling to get even this basic example to work. The following code
> snippet works in the REPL:
>
> function myTask()
>     wait(STDIN.readnotify)
>     println("Key Pressed!")
> end
>
> @async myTask()
>
> However it doesn't work when run as a script (because it exits before
> myTask completes). Adding an @sync begin ... end block around the @async
> call doesn't fix the problem in the script, and actually breaks the REPL
> version as well. How should I check for user input without actually
> stopping to wait if there is none?
>
> Thanks in advance for your help,
> Luke
>

Reply via email to