How to detect keystroke in LWUIT form?

I wrote a simple j2me program with the LWUIT package. I added one Form to my MIDLET class file. Suppose a user presses a key, then I want to show another Form . But I could not capture the key event in the LWUIT Form .

This is my snippt code

 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) {} } 
+4
source share
1 answer

To write an event key to an LWUIT Form , you need to use Form.addGameKeyListener(here the key, here actionListener)

Keys are displayed using Canvas , for example Canvas.FIRE .

Try to do it.

+5
source

Source: https://habr.com/ru/post/1390410/


All Articles