2012-01-11 27 views
4

私はLWUITパッケージで簡単なJ2MEプログラムを書いています。私はMIDLETクラスファイルにFormを追加しました。ユーザーがキーを押してからFormを表示したいとしますが、LWUIT Formでキーイベントを取得できませんでした。LWUIT形式のキー押下イベントを検出する方法は?

これは、あなたがForm.addGameKeyListener(here the key, here actionListener)

キーは、例えばCanvas.FIREようCanvasを使用してマッピングされて使用する必要がLWUIT Formでイベントキーをキャプチャする

import javax.microedition.midlet.*; 
import com.sun.lwuit.*; 
import com.sun.lwuit.events.*; 


public class MultipleForm extends MIDlet implements ActionListener{ 

    private Form mFirstForm, mSecondForm; 

    public void startApp() 
{ 
     if (mFirstForm == null) 
    { 
     Display.init(this); 

     mFirstForm = new Form("First Form"); 
     Button button = new Button("Switch"); 
     button.addActionListener(this);   
     mFirstForm.addComponent(button); 

     mSecondForm = new Form("Second Form"); 
     Button button2 = new Button("Switch"); 
     button2.addActionListener(this); 
     mSecondForm.addComponent(button2); 

     mFirstForm.show(); 

     } 
    } 

    protected void keyPressed(int key) 
    { 
     System.out.println("Key Pressed"); 

     if(key==52) 
     { 
      Form current = Display.getInstance().getCurrent(); 
      if (current == mFirstForm) 
      { 
      mSecondForm.show(); 
      } 
      else if(current==mSecondForm) 
      { 
      mFirstForm.show(); 
      } 
     } 
    } 

    public void pauseApp() {} 

    public void destroyApp(boolean unconditional) {} 
} 

答えて

5

snippt私のコードです。

これを行うようにしてください。

+0

私たちが右クリックしたすべてのキーにゲームのキーリスナーを追加する必要があります。LCDUIでは、keyPressed(int key)をオーバーライドして、そのキーのコードをチェックして、それで、LCDUIのようにLWUITに汎用的な仕組みがありますか? – Saravanan

+3

フォームのkeyPressed/releaseをオーバーライドして、同じ効果を得ることができます。 keyReleasedを使用することをお勧めします。 –

+0

この提案の理由は何ですか? –

関連する問題