1

Why this Java code by clicking on button NUM0-9 doesn't print out the character?

Code:
switch(this.getGameAction(keyCode)){
            case Canvas.KEY_NUM2:
                System.out.println('A');
                break;

            case Canvas.KEY_NUM0:
                System.out.println('B');

        }

Even no Error appears. Why?

Jarrod
  • 9,349
  • 5
  • 58
  • 73
  • Is the keyboard NUMLOCK on? – Jim Garrison Jan 05 '13 at 09:01
  • see the doc: http://www.j2megame.org/j2meapi/JSR_118_Mobile_Information_Device_Profile_2_0/javax/microedition/lcdui/Canvas.html#gameactions – imxylz Jan 05 '13 at 09:01
  • these answers don't enough explain the reason why this code doesn't work. I need more explanation and solvation. More to say: the code was produced in LOW API Canvas in the abstract method keyPressed. – user1939029 Jan 05 '13 at 09:45
  • As I wrote in my reply below, you don't use getGameAction() on the KEY_NUM0-9 values, nor the KEY_STAR and KEY_POUND. Remove the getGameAction() method in the switch method, and just do switch(keyCode), then it will work. – mr_lou Jan 13 '13 at 20:22

1 Answers1

1

You don't use getGameAction() on the KEY_NUM0-9 values, nor the KEY_STAR and KEY_POUND. You do it like this:

public void keyPressed(int kc) {
 if (kc == KEY_NUM1 ) {} // key number 1 pressed
 if (getGameAction(kc) == LEFT) {} // left pressed
}
mr_lou
  • 1,910
  • 2
  • 14
  • 26