Created
May 9, 2013 02:50
-
-
Save ecmendenhall/5545237 to your computer and use it in GitHub Desktop.
A keypress listener using Jline and Clojure futures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns keylistener.core | |
(:require [clojure.java.io :as io]) | |
(:import [jline.console ConsoleReader]) | |
(:gen-class)) | |
(def console (ConsoleReader.)) | |
(defn keypress-loop [] | |
(let [char (.readCharacter console)] | |
(if (not (nil? char)) | |
char | |
(recur)))) | |
(defn main-thread [n listener] | |
(println n) | |
(Thread/sleep 500) | |
(if (future-done? listener) | |
(do (println (str "You pressed char: " @listener)) | |
(recur (inc n) (future (keypress-loop)))) | |
(recur (inc n) listener))) | |
(defn -main [& args] | |
(let [listener (future (keypress-loop))] | |
(main-thread 0 listener))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment