2012-01-11 36 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