Generic soft key detection in J2ME


As many of you are aware, the soft key buttons on a mobile phone are general purpose buttons that are reused over and over again. On most handsets there are two of these directly beneath the screen (one on the left, and one on the right).

In a recent project I needed to be able to detect when the left and right soft keys were being pressed, but unfortunately, J2ME does not define a standard key code. Instead it is up to the developer to specify different codes for all of the different phones in their target audience.

Two solutions came to mind:

  1. Compile the application for each target handset (perhaps overkill given that this be the only difference).
  2. Get the user to specify which button is which (even if they do not realize this!).


I decided to go with option #2 so that my application would work for the majority of handsets. Instead of asking the user to configure the keys directly, I created a welcome screen.

The welcome screen contains a nice greeting, but more importantly the text “Continue” in the bottom left corner. The user will instinctively press the soft key button that is nearest that text, and so the key code is known for the rest of the application. I decided to filter out all known keys so that the user does not press the “FIRE” button, etc.

Screenshot of Welcome Screen
Welcome Screen for J2ME

Once the key code of the left soft key is known, we can assume that any other non-standard button that is pressed is the second soft-key.

Please note that this does mean that other unknown buttons will also behave as the second soft key.

Here is a simplified version of the welcome screen that I used:

public class WelcomeCanvas extends Canvas {
   public WelcomeCanvas() {
      setFullScreenMode(true);
   }

   protected void paint(Graphics g) {
      g.setColor(0x336699);
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(0xFFFFFF);
      g.setFont(Font.getFont(
         Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
      g.drawString("Welcome!", getWidth() / 2, getHeight() / 2,
         Graphics.HCENTER|Graphics.TOP);

      // The all important continue text!
      g.setFont(Font.getDefaultFont());
      g.drawString("Continue", 2, getHeight() - 2,
         Graphics.BOTTOM|Graphics.LEFT);
   }
   protected void keyPressed(int keyCode) {
      if (!SoftCommandCanvas.isKnownKey(this, keyCode)) {
         SoftCommandCanvas.KEY_SOFT_LEFT = keyCode;
         continuePressed();
      }
   }

   protected void continuePressed() {
      // Do something!
   }
}

Here is an example of how you can detect which soft key is being pressed in another canvas:

public class SoftCommandCanvas extends Canvas {
   // The default value here works for Nokia handsets.
   public static int KEY_SOFT_LEFT = -6;

   public SoftCommandCanvas() {
      setFullScreenMode(true);
   }

   public static boolean isKnownKey(Canvas canvas, int keyCode) {
      return (keyCode >= KEY_NUM0 && keyCode <= KEY_NUM9) ||
         keyCode == KEY_POUND || keyCode == KEY_STAR ||
         canvas.getGameAction(keyCode) != 0;
   }

   protected void keyPressed(int keyCode) {
      if (!isKnownKey(this, keyCode)) {
         if (keyCode == KEY_SOFT_LEFT)
            fireLeftSoftCommand();
         else
            fireRightSoftCommand();
      }
      else
         super.keyPressed(keyCode);
   }

   protected void fireLeftSoftCommand() {
      // Do something!
   }

   protected void fireRightSoftCommand() {
      // Do something!
   }
}
Advertisements

2 thoughts on “Generic soft key detection in J2ME

  1. smart approach… I like the thought behind it.

    Have you given any thought to identifying the second soft key?
    I’m thinking that the difference for both the soft keys numbers is very likely small…

    1. Identifying the second soft key was part of my reasoning behind introducing the welcome screen. End-users will intuitively press the associated soft key to get past the welcome screen. So, to identify the second soft key in non-critical apps you can assume that any other unknown button (not number, or fire, etc.) is the second soft key.

      Platform detection makes it far simpler to detect the second soft key, but this is not generic to all phones. But you can combine this logic with platform detection for a better result. For platforms that you can detect, utilise the platform-specific code. For platforms where the second soft key could not be detected, assume that the unknown key is the second soft key. Be aware though, because there could be multiple second soft keys with this reasoning (second soft key, camera button, function button, etc.)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s