Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for a keypress in clojure

I created a java frame with seesaw

(def f (frame :title "my app"))

and I would like to catch user keypress.

I tried to gather code here and there and ended with this

(ns myapp.core
  (:use seesaw.core)
  (:use seesaw.font)
  (:import [java.awt.event ActionListener KeyListener KeyEvent])
)

(defn input-listener []
(proxy [ActionListener KeyListener] []
  (actionPerformed [e])
  (keyPressed [e] (alert e "You pressed a key!"))
  (keyReleased [e])
  (keyTyped [e])))

(doto f
  (.addKeyListener (input-listener)))

but it won't work at all. I am new to clojure and since I absolutely don't know anything JAVA (and don't really want to ding into it) I am a bit lost. Is there a simple way to catch user input for keyboard shortcuts in the whole app ?

help please.

like image 471
sai Avatar asked Dec 26 '22 14:12

sai


1 Answers

If you'd just like to map specific key presses to different functions in a frame, seesaw.keymap/map-key is probably what you want:

; When 'e' is pressed in frame f, call this function
(map-key f "e" (fn [_] (... do something ))

(this is all built on top of the keybinding stuff @Bill references)

Take a look at the docs for map-key for more info. As the other answers have alluded to, keyboard handling in Swing is even nastier than the rest of Swing so be ready for some pain :)

like image 149
Dave Ray Avatar answered Dec 29 '22 04:12

Dave Ray