1

I have a phone with only one physical key, which is clicked as BACK operation. But the key event can NOT be caught in LWUIT 1.5. Here is my codes:



    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);
        }
    }

However, the key event can be caught with LCDUI Form. Here is the code with 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
            }
        }

    }

Maybe some key event is ignored by LWUIT? Could anyone help me?

obunny
  • 26
  • 2

1 Answers1

0

Back is sent to the back command and isn't propagated onwards.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Make sure to add it to every form that is relevant via setBackCommand(yourCommand) – Shai Almog Sep 24 '12 at 09:53
  • What I have did that I add it in the MyForm class and make all other forms inherit from MyForm , then I make a back action method abstract and implement it in each child form and then using reflection I apply the action of the current form from the parent form , It's works now but I don't know if it will make any problem next – Amira Elsayed Ismail Sep 24 '12 at 10:01