Reading Unbuffered Keyboard Input in Clojure

How to read a single keystroke from a terminal (not Swing) in Clojure?

I tried several things, including various versions of the JLine library, but did not get them (see example below).

I will gladly accept a working Unix-only example (Mac, Linux, ...). Ideally, I would like to know how to disable buffering for both stdin and stdout.

Something is close here:

;; project.clj dependencies: ;; [[org.clojure/clojure "1.4.0"] ;; [jline/jline "2.8"]]) (ns slosh.core (:import [jline.console ConsoleReader]) (:gen-class)) (defn -main [] (println "start") (let [cr (ConsoleReader.)] (.readCharacter cr) (println "done"))) 

This prints "start", but does not respond to any input other than control-C.

+7
source share
2 answers

I'm not sure how you use this, but if you use lein run , you will have problems. Try using lein trampoline run .

I would associate the console entry with a single character in java / clojure , but I do not have enough internet points for this.

+5
source

Perhaps you can also look at clojure-lanterna .

+1
source

All Articles