在LWUIT中无法捕捉到物理按键事件



我有一部只有一个物理键的手机,它被点击作为BACK操作。但关键事件不能在LWUIT1.5中捕捉到。这是我的代码:


    import com.sun.lwuit.Command;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.events.ActionEvent;
    public class CustomForm extends Form {
        public CustomForm() {
            Command backCmd = new Command("BACK", 2) {
                public void actionPerformed(ActionEvent evt) {
                    System.out
                            .println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! back cmd evt: "
                                    + evt);
                }
            };
            this.addCommand(backCmd, 0);
        }
        public void keyPressed(int aKeyCode) {
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@ key code = " + aKeyCode);
            super.keyPressed(aKeyCode);
        }
        public void keyReleased(int keyCode) {
            System.out
                    .println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ key released code: "
                            + keyCode);
            super.keyReleased(keyCode);
        }
    }

然而,关键事件可以用LCDUI-Form捕获


    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.StringItem;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    public class Test extends MIDlet implements CommandListener {
        protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
            // TODO Auto-generated method stub
        }
        protected void pauseApp() {
            // TODO Auto-generated method stub
        }
        protected void startApp() throws MIDletStateChangeException {
            Form form = new Form("Hello World");
            StringItem str = new StringItem("HI", "BYE");
            form.append(str);
            Command bc = new Command("Back", Command.BACK, 1);
            form.addCommand(bc);
            form.setCommandListener(this);
            Display.getDisplay(this).setCurrent(form);
        }
        public void commandAction(Command c, Displayable d) {
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ Got command: "
                    + c.getCommandType() + " " + c.getLabel());
            if (c.getCommandType() == Command.BACK) {
                // Application logic for Back button press
            }
        }
    }

也许某个关键事件被LWUIT忽略了?有人能帮我吗?

Back被发送到Back命令,并且不会继续传播。

相关内容

  • 没有找到相关文章

最新更新