如何以LWUIT形式检测按键事件? [英] How to detect key pressed event in LWUIT form?

查看:98
本文介绍了如何以LWUIT形式检测按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用LWUIT包编写了简单的j2me程序,并在我的MIDLET类文件中添加了一个Form.假设用户按下一个键,然后我想显示另一个Form.但是我无法在我的LWUIT Form中捕获键事件.

I have written simple j2me program with LWUIT package.I have added one Form in my MIDLET class file. Suppose,user press a key then I want to show another Form.But I couldn't be able to capture key event in my 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) {}
}

推荐答案

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

To capture the event key in a LWUIT Form you need to use Form.addGameKeyListener(here the key, here actionListener)

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

尝试这样做.

这篇关于如何以LWUIT形式检测按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆