1

I added a gameKeyListener() to my Form for when the up and down Button is presed, but the keyPressed() method is not called. My code:

    Form f = new Form();
         f.addGameKeyListener(Canvas.UP, this);
   f.addGameKeyListener(Canvas.DOWN, this);
          f.show();
}
    public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void actionPerformed(ActionEvent evt) {
       }

   public void keyPressed(int key)
{
   System.out.println ("Pressed");
    switch (key){
        case Canvas.DOWN:
            //code
            System.out.println ("Pressed");
            break;
        case Canvas.UP:
            //code
            System.out.println ("Pressed");
            break;
    }
}

}

Thanks in advance.

Mun0n
  • 4,438
  • 4
  • 28
  • 46
Glenncito
  • 902
  • 1
  • 10
  • 23

2 Answers2

2

When you use addGameKeyListener this fire action event. Try this:

public void actionPerformed(ActionEvent evt) {
switch (evt.getKeyEvent()){
        case Canvas.DOWN:
            //code
            System.out.println ("Pressed");
            break;
        case Canvas.UP:
            //code
            System.out.println ("Pressed");
            break;
    }

}

frayab
  • 2,512
  • 20
  • 25
1

You can see this post: How to detect key pressed event in LWUIT form?

I resolved it in this question

Community
  • 1
  • 1
Mun0n
  • 4,438
  • 4
  • 28
  • 46
  • I ended up with the above posted code after reading the post you mention and I was still stuck with the problem. – Glenncito Jan 26 '12 at 18:49