2011-12-07 2 views
1

私はJFrameにキーバインドのマップを追加するためのコードを用意しました。残念ながら、プログラムをコンパイルしても、プログラムを実行するときにエラーがなくなりますが、バインディングは機能しません。フレームのキーバインディングの開始

私には何が欠けていますか?溶液は、JComponent/WHEN_ANCESTOR_OF_FOCUSED_COMPONENTの代わりにJComponent/WHEN_IN_FOCUSED_WINDOWを使用することであった

(doto frame 
    (.setFocusable true) 
    (init-jframe-key-bindings!                                                    
     {"RIGHT" [:next-view to-next-view]                                                  
     "LEFT" [:prev-view to-previous-view]                                                 
     "T" [:thresh-test conduct-thresh-test]                                                 
     "A" [:add-marks #(dosync (ref-set ref-mark-mode :a))]                                             
     "D" [:del-marks #(dosync (ref-set ref-mark-mode :d))]})) 

EDIT:

(defn create-action                                                       
    "Returns an Action that, when called, executes the given fn."                                            
    [f]                                                           
    (proxy [AbstractAction] []                                                     
    (actionPerformed [e] (f))))                                                    

(defn init-jframe-key-bindings!                                                    
    "Adds the keybindings to the frame.                                                   

    keymap take the form of:                                                     
    {\"KEYSTROKE\" [:key-name fn]                                                    
    ...}"                                                          
    [frame keymap]                                                        
    (let [actionmap (.getActionMap (.getRootPane frame))                                               
     inputmap (.getInputMap (.getRootPane frame) JComponent/WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)]                                   
    (doseq [[keystroke [keyword action]] keymap]                                                 
     (.put actionmap (name keyword) (create-action action))                                            
     (.put inputmap (KeyStroke/getKeyStroke keystroke) (name keyword))))) 

バインディングはそうように添加されます。私は焦点がフォーカスされたコンポーネントの祖先であるという要件を満たさなければならないので(なぜなら、私の無数のコンポーネントで何か)、答えを聞くのが大好きだが、なぜこれが当てはまるのか分からないが、後世の解決策。

答えて

2

forは遅延です。 init-jframe-key-bindings!関数が呼び出されており、実際にはキーバインドが追加される遅延シーケンスが返されます。しかし、あなたはそれを実現させることはありません。あなたはその結果を捨てる。 forの代わりに、副作用が必要な場合はdoseqを使用してください。

+0

それはひどい間違いですが、私はそれを捕まえて(そして私の質問を更新するのを全然忘れていました。)私の問題を解決しませんでした。 – Isaac

関連する問題