2

I'm trying to listen to the :key-pressed and :key-released events on my Seesaw frame, but the events aren't firing. I've narrowed the problem down to a listbox -- when the listbox is present, the frame no longer captures the key events. Here's a simplified version of my code that shows the behavior:

(ns ainur.example
  (:use seesaw.core))

(let [lst (listbox :model ["Chiptune" "Sinewave"])
      f (frame :title "Ainur"
             :on-close :exit
             :size [1024 :by 768]
             :content (border-panel :hgap 10 :vgap 10
                                    :center (label "Center")
                                    :north (label "North")
                                    :south (label "South")
                                    :west lst))]
(listen lst :selection (fn [e]
                         (let [active-inst (selection e)]
                           (println active-inst))))
(listen f
        :key-pressed (fn [e]
                       (println "Key pressed"))
        :key-released (fn [e]
                        (println "Key released")))
(invoke-later
 (native!)
 (show! f)))

Can anyone help me figure out why the key events aren't triggered? Any help would be really appreciated. Thanks in advance!

Austin Pocus
  • 993
  • 11
  • 18

1 Answers1

0

I posted this question in seesaw's Google Group, and received an excellent answer from Seesaw's creator, Dave Ray, himself. I'm posting it here in case anyone else runs into this issue:

"Hi. Once there's another widget in the hierarchy like a listbox, it grabs keyboard focus so the events never get to the frame. I think the best bet would be to put the key listener on a nested widget like a panel and then give it keyboard focus. A kind of similar example can be seen here:

https://github.com/daveray/regenemies/blob/master/src/regenemies/ui.clj#L163

The :key-typed event is bound to the canvas and then .requestFocusInWindow is used to give it keyboard focus."

Many thanks, Dave!

Austin Pocus
  • 993
  • 11
  • 18