【问题标题】:How to detect key pressed event in LWUIT form?如何检测 LWUIT 表单中的按键事件?
【发布时间】:2012-01-11 11:05:30
【问题描述】:

我用 LWUIT 包编写了简单的 j2me 程序。我在我的 MIDLET 类文件中添加了一个Form。假设,用户按下一个键,然后我想显示另一个 Form。但是我无法在我的 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) {}
}

【问题讨论】:

    标签: java-me key lwuit lwuit-form


    【解决方案1】:

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

    使用Canvas 映射键,例如Canvas.FIRE

    尝试这样做。

    【讨论】:

    • 我们需要为我们按下的每个按键添加游戏按键监听器...在 LCDUI 中,我们只是覆盖 keyPressed(int key) 并且在该方法中,我们正在检查按键代码以知道按下了哪个键。那么,LWUIT 中有没有像 LCDUI 一样的通用机制?
    • 您可以在表单中覆盖keyPressed/release 并获得相同的效果。我们建议始终使用 keyReleased 而不是 keyPressed。
    • 这个建议有什么特别的原因吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2013-02-04
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多