Reading unbuffered keyboard input in Clojure

試著忘記壹切 提交于 2019-12-21 10:51:13

问题


How does one read a single keystroke from the terminal (not Swing) in Clojure?

I have tried a few things including various versions of the JLine library, but have not gotten it working (see example below).

I will happily accept a working, Unix-only (Mac, Linux, ...) example. Ideally I'd like to know how to switch buffering off for both stdin and stdout.

Here's something close:

;; 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 except control-C.


回答1:


I'm not sure how you are running this, but if you are using lein run, you will run into problems. Try using lein trampoline run.

I would link Single character console input in java/clojure but I don't seem to have enough Internet Points to do that.




回答2:


Maybe also have a look at clojure-lanterna.



来源:https://stackoverflow.com/questions/13435541/reading-unbuffered-keyboard-input-in-clojure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!