i'm having a lot of trouble dealing with events.

28 views
Skip to first unread message

notallama

unread,
Nov 19, 2008, 9:14:14 PM11/19/08
to Clojure
so, for a key listener, the keypressed method is called when you press
a key, right?
or am i misunderstanding how events work?

this is what i have:

(import
'(javax.swing JFrame)
'(java.awt Canvas)
'(java.awt.event KeyListener KeyEvent))

(def app (JFrame.))
(def canvas
(proxy [Canvas KeyListener] []
(KeyPressed [e] (print "hay! it's working!"))))

(doto canvas
(setFocusable true)
(addKeyListener canvas))

(doto app
(add canvas)
(setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(setSize 640 480)
(setVisible true))


what i want it to do is print a message whenever a key is pressed, but
it doesn't.
(well, assuming that's how events are supposed to work.
the eventual plan is to have it so that when a key is pressed, it
calls a multimethod to deal with some refs. but it seems that i have
no idea what i'm doing here)

the window shows up, but key presses do nothing. (or nothing visible
in the frame or in the repl (slime), anyway)

notallama

unread,
Nov 19, 2008, 9:32:38 PM11/19/08
to Clojure
so i made a typo.
KeyPressed should be keyPressed

still nothing, though. tried making the listener seperate, adding it
to the frame, making the frame implement KeyListener and adding
itself. nothing.

notallama

unread,
Nov 19, 2008, 11:21:20 PM11/19/08
to Clojure
figured it out.

my prints were going to the inferior lisp buffer.

i feel kinda silly now.

Timothy Pratley

unread,
Nov 20, 2008, 12:41:35 AM11/20/08
to Clojure
Would you mind posting your working version?
I tried your code with the keyPressed changed, but I get an exception
whenever I press a key
Exception in thread "AWT-EventQueue-0" java.io.IOException: Stream
closed
so something else must have changed also?

notallama

unread,
Nov 20, 2008, 1:00:13 AM11/20/08
to Clojure
well, it has changed quite a lot now: (i'm not sure if this all
actually works yet. they key map does, though)

;;gui imports. hurray!
(import
'(javax.swing JFrame)
'(java.awt Canvas)
'(java.awt.event KeyListener KeyEvent
MouseListener MouseEcent
FocusListener FocusEvent))

(defstruct window :win :canvas :buffer :keys-down :clicks)

(defn make-win
"makes a window, complete with canvas, backbuffer, key map, and click
map.
maps are cleared when the window loses appropriate focus."
[x y]
(let [win (JFrame.)
canvas (Canvas.)
keys-down (agent {})
clicks (agent {})]
(doto canvas
(setSize x y)
(addKeyListener (proxy [KeyListener] []
(keyTyped [#^KeyEvent e] nil)
(keyPressed [#^KeyEvent e] (send keys-down assoc (. e getKeyCode)
e))
(keyReleased [#^KeyEvent e] (send keys-down dissoc (. e
getKeyCode)))))
(addMouseListener (proxy [MouseListener] []
(mousePressed [#^MouseEvent e] (send clicks assoc (. e getButton)
e))
(mousereleased [#^MouseEvent e] (send clicks dissoc (. e
getButton)))
(mouseExited [#^MouseEvent e] (send clicks (fn [a] {})))
(mouseEntered [#^MouseEvent e])
(mouseClicked [#^MouseEvent e])))
(addFocusListener (proxy [FocusListener] []
(FocusGained [#^FocusEvent e])
(FocusLost [#^FocusEvent e] (send keys-down (fn [a] {}))))))
(doto win
(setIgnoreRepaint true)
(setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(add canvas)
(pack)
(setVisible true))
(doto canvas
(createBufferStrategy 2))
(struct window win canvas (. canvas getBufferStrategy) keys-down
clicks)))



i think this is what i had when i first got it working error-free,
though: (this works, i just don't know for sure if it's what i had)

(import
'(javax.swing JFrame)
'(java.awt Canvas)
'(java.awt.event KeyListener KeyEvent))

(def app (JFrame.))
(def canvas
(proxy [Canvas KeyListener] []
(keyPressed [e] (println "hay! it's working!"))
(keyReleased [e] (println "released"))
(keyTyped [e] (println "typed"))))

(doto canvas
(setFocusable true)
(addKeyListener canvas))

(doto app
(add canvas)
(setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(setSize 640 480)
(setVisible true))


without the keyReleased and keyTyped, it throws a bunch of errors.
in between the errors, it was printing stuff, though.

Timothy Pratley

unread,
Nov 20, 2008, 3:19:50 AM11/20/08
to Clojure
Thanks for that.

It turns out that if I run your code from a REPL everything works
great.
I was trying to run it from a file... which for some reason throws an
exception.
clj event.clj
Exception in thread "AWT-EventQueue-0" java.io.IOException: Stream
closed
<other spam>

It would seem that stdout is not available from within the proxy when
running from the command line, but it *is* when executing from the
REPL. I'm sure there is a good reason for that, but I find it very
confusing! Anyone care to enlighten me?

Regards,
Tim.

Timothy Pratley

unread,
Nov 20, 2008, 5:27:38 AM11/20/08
to Clojure
> It would seem that stdout is not available from within the proxy when
> running from the command line, but it *is* when executing from the
> REPL. I'm sure there is a good reason for that, but I find it very
> confusing! Anyone care to enlighten me?

Actually if I add (read ) to the end of my file, then stdout is kept
open... and the program behaves the same as from the REPL. So it seems
that stdout is closed once the reader reaches EOF, however the JFrame
is still active. This is unexpected. Would it make sense to change the
reader to 'packup' only after spawned instances are finished with?

Regards,
Tim.

Mark Volkmann

unread,
Nov 20, 2008, 10:21:20 AM11/20/08
to clo...@googlegroups.com
On Thu, Nov 20, 2008 at 12:00 AM, notallama <nota...@gmail.com> wrote:
>
> (keyTyped [#^KeyEvent e] nil)

I'm not familiar with the syntax above. What does the "#^" part do?

--
R. Mark Volkmann
Object Computing, Inc.

Cosmin Stejerean

unread,
Nov 20, 2008, 10:28:42 AM11/20/08
to clo...@googlegroups.com
On Thu, Nov 20, 2008 at 9:21 AM, Mark Volkmann <r.mark....@gmail.com> wrote:

On Thu, Nov 20, 2008 at 12:00 AM, notallama <nota...@gmail.com> wrote:
>
>                        (keyTyped [#^KeyEvent e] nil)

I'm not familiar with the syntax above. What does the "#^" part do?


--
Cosmin Stejerean
http://www.offbytwo.com

wlr

unread,
Nov 20, 2008, 10:29:32 AM11/20/08
to Clojure
On Nov 20, 10:21 am, "Mark Volkmann" <r.mark.volkm...@gmail.com>
wrote:
> On Thu, Nov 20, 2008 at 12:00 AM, notallama <notall...@gmail.com> wrote:
>
> >                        (keyTyped [#^KeyEvent e] nil)
>
> I'm not familiar with the syntax above. What does the "#^" part do?
>
> --
> R. Mark Volkmann
> Object Computing, Inc.

http://clojure.org/getting_started

Scroll down to "Type Hints"

Cosmin Stejerean

unread,
Nov 20, 2008, 10:30:25 AM11/20/08
to clo...@googlegroups.com
On Thu, Nov 20, 2008 at 9:21 AM, Mark Volkmann <r.mark....@gmail.com> wrote:

On Thu, Nov 20, 2008 at 12:00 AM, notallama <nota...@gmail.com> wrote:
>
>                        (keyTyped [#^KeyEvent e] nil)

I'm not familiar with the syntax above. What does the "#^" part do?

wlr

unread,
Nov 20, 2008, 10:32:31 AM11/20/08
to Clojure
err, bad paste. :-( Go with Cosmin Stejerean's reply.
Reply all
Reply to author
Forward
0 new messages