First: don't attach any Commands or a CommandListener to the Canvas. Doing this might disable keyPressed() events for the softkeys.
Softkeys have key codes just like any other key. The one slight problem is: they don't have standard codes like 0-9, * and # do. Same goes for dpad (direction pad, the up-down-left-right control).
You may be using getGameAction() to access the dpad control. I recommend you don't. Use the key codes instead. Yes, these will vary from device to device, but so will the softkey codes, so this is a problem you will have to deal with anyway. The problem with game-actions is that they are unpredictable. You never know which keys will map to which actions. You will often find, for example, that 2, 4, 6 and 8 also map to up, left, right and down, as will as the dpad. 5 often maps to fire, but then again it might be 0. Or the left softkey. Or send (green key). Or all of them. This particularly becomes a problem if you mix game-actions and raw key-codes in the same keyPressed() event. It can make the help text hard to get right (since you don't know which keys the use must use).
OK... having abandoned game-actions, which codes do you use?
You might want to create a class like this:
Code:
public class Keys {
public static final int LEFT_SOFT = com.nokia.mid.ui.FullCanvas.KEY_SOFTKEY1;
public static final int RIGHT_SOFT = com.nokia.mid.ui.FullCanvas.KEY_SOFTKEY2;
public static final int SELECT = com.nokia.mid.ui.FullCanvas.KEY_SOFTKEY3;
public static final int UP = com.nokia.mid.ui.FullCanvas.KEY_UP_ARROW;
public static final int LEFT = com.nokia.mid.ui.FullCanvas.KEY_LEFT_ARROW;
public static final int RIGHT = com.nokia.mid.ui.FullCanvas.KEY_RIGHT_ARROW;
public static final int DOWN = com.nokia.mid.ui.FullCanvas.KEY_DOWN_ARROW;
}
I've referenced these from the Nokia API. If you like, you can hard-code them as literals:
Code:
public class Keys {
public static final int LEFT_SOFT = -6;
public static final int RIGHT_SOFT = -7;
public static final int SELECT = -5;
public static final int UP = -1;
public static final int LEFT = -3;
public static final int RIGHT = -4;
public static final int DOWN = -2;
}
This will enable you to build without having the Nokia API in your classpath.
These values are correct for the current Sun emulator, for all Nokia devices (one or two Nokias don't have a SELECT (dpad-centre) key), and all MIDP-2 Sony Ericsson devices. They also work on some Samsungs, and one or two other devices.
For other devices, you can have different versions of this class, and produce device-specific builds.
Another alternative is to put the codes in a resource file in the JAR, and read them from there. I don't favour this approach. If you're going to generate different JARs, you might as well re-compile.
Thirdly, you could consider putting them in the JAD, so you don't need to have different JARs, just different JADs. Use MIDlet.getAppProperty() to read information from the JAD.
Having them in code (as shown above) generates the smallest, fastest code, if that's important for you.
Graham.