2012-01-11 38 views
4

我用LWUIT包编写了简单的j2me程序。我在MIDLET类文件中添加了一个Form。假设,用户按一个键,然后我想显示另一个Form。但我无法捕捉到我的LWUIT Form中的关键事件。如何检测LWUIT表单中的按键事件?

这是我的代码snippt

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

捕获事件关键在LWUIT你需要使用Form.addGameKeyListener(here the key, here actionListener)

的关键是使用CanvasCanvas.FIRE例如映射Form

试着做到这一点。

+0

我们需要为我们按下的每个键添加游戏键监听器......在LCDUI中,我们只是重写keyPressed(int键)并在该方法内,我们检查键代码以知道哪个键被按下那么,LWUIT中是否有像LCDUI一样的通用机制? – Saravanan 2012-01-12 04:04:10

+3

您可以覆盖表单中的keyPressed/release并获得相同的效果。我们建议您始终使用keyReleased来执行操作,而不是keyPressed。 – 2012-01-12 06:46:47

+0

此建议的任何特定原因? – 2013-10-29 11:47:01