J2ME button control problems

Hello everybody,
I have a problem about the button (e.g KEY_NUM1, KEY_NUM2 … etc), Why the buttons are disable (i.e. no action after pressing it) except KEY_STAR and KEY_POUND?
All are disable such as KEY_NUM1, KEY_NUM2 … KEY_NUM9.
My program like below:

switch(keycode){
case KEY_STAR:
System.out.println(“CDE”);
break;
}
switch(keycode)
{
case KEY_POUND:
System.out.println(“ABC”);
break;
}

Thank you very much~

If you’re using a MIDP 2.0 GameCanvas and you’ve passed ‘true’ as the argument for the constructor parameter ‘suppressKeyEvents’, then you won’t get keyPressed etc. callbacks for keys which correspond to game keys UP, DOWN, LEFT, RIGHT, FIRE, GAME_A…GAME_D. It might be that the phone you’re using has a mapping like:
KEY_NUM1 -> GAME_A
KEY_NUM2 -> UP
KEY_NUM3 -> GAME_B
KEY_NUM4 -> LEFT
KEY_NUM5 -> FIRE
KEY_NUM6 -> RIGHT
KEY_NUM7 -> GAME_C
KEY_NUM8 -> DOWN
KEY_NUM9 -> GAME_D
So then you’d only get keyPressed callbacks for KEY_STAR, KEY_NUM0 & KEY_POUND.

If this isn’t your answer, then please supply more information: what kind of Canvas / GameCanvas are you using, and what phone model?

Thanks a lot, but it also doesn’t work.
I am using MIDP2.0 GameCanvas.
I use Sun.com’s KToolbar for testing.
I apply this method:
public void keyReleased(int keycode)
{
// switch cases
}

So just to check: your GameCanvas subclass’s constructor should start with:


    super(false);

That way you’re not suppressing these events you want. I just hacked up one of my old games to do this, and the keyReleased method was called just as I expected.

It works now, thank you very much~ :smiley:
You are very helpful~ :smiley:
However, can you tell me what is the function of
super(true) and super(false)?
Why super(false) can work but not super(true)?
Thanks a lot~ :smiley:

You use super(true) usually when you want to do all the input handling asynchroneously using the getKeyStates() method. It means that keyPressed() won’t be called if a key that corresponds to one of the game actions (see David’s first post) is pressed. This supposedly will make the game perform a little bit better (anyone seen proof of this by the way?) because there will be less calls to keyPressed().

shmoove

I see
Thanks a lot~

I think there is little difference in performance. I always try and use the gameStates method though. At the moment, I have devised a gameCanvas that uses both methods, but only because I can’t work out how to check if the fire button was pressed and the number of the button corresponds to KEY_NUM5. Any ideas guys/girls?

Ribot: you question can be parsed in more than one way, so I’m not sure if I understand it correctly. If I understand the question correctly, your problem is that you’re worried that the fire button might have been pressed and released between calls to getKeyStates.

But the JavaDocs for that method state that the button’s bit will be set if the key is currently down or has been pressed at least once since the last time this method was called. So you won’t miss any short clicks.

Unless you’re targeting a particular phone for which it’s true, you shouldn’t assume that the fire button is (only) button 5. E.g. in Nokia Series 60 phones both button 5 and clicking the joystick work as fire buttons. Except (as usual) the 3650, where 5 probably isn’t a fire button - the 3650 isn’t a MIDP 2.0 phone, but still it’s a good reminder that sensible assumptions aren’t always true ;). I guess also in the MIDP 2.0 Nokia 6822, 5 isn’t a fire button when the folding alphanumeric keyboard is open (haven’t checked). These complications are why MIDP has abstract game actions as well as key codes.

davidaprice: thank you for your response! Yes, I realise that any length of press will result in the state of the button being set to pressed until polled. Sorry for my unobvious question, i will try to rephrase it - I’m currently working on a project which targets a specific device (Nokia 7610). My required functionality is that when the user presses on the joystick ‘fire’ button, they perform an ‘action’. Want i do not want to respond to is if the fire button pressed was number 5 on the keypad (or anything else for that matter). Is there a way of checking that the ‘5’ or a key other than the joystick fire button was pressed so I can determine not to respond?

I hope I have made things clearer. :wink:

Much clearer, thanks :). I’d say that the idea of game actions is handling abstract actions rather than specific keys, so if you need to distinguish between two specific keys that have the same game action, you’re going to have to use key codes instead. And since GameCanvas’s getKeyStates method is specified only in terms of game actions, it’s no use to you in this case. So you’re already doing the right thing, not suppressing the key events and using keyPressed to detect the joystick click.