物理键按下事件不能在LWUIT中捕获

时间:2012-07-18 09:01:56

标签: key lwuit lcdui pressed

我的手机只有一个物理键,单击为BACK操作。但关键事件不能在LWUIT 1.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表格捕获。这是LCDUI的代码:



    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可能会忽略某些关键事件?有谁可以帮助我?

1 个答案:

答案 0 :(得分:0)

后退发送到后退命令,不会向前传播。