Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Java ME Guideline: Events - how to modify key assignment

 
 
[Design Guidelines - General Introduction] [Design Guidelines - Technical Overview]  

Contents
  1. Problem Description
  2. Solution Description
  3. List of Affected Devices
  4. Keywords
1. Problem Description

How to use key events, how to assign different game keys than the default supported by the device for a specific application need

Use Cases

Any application that uses canvas and handles Low-Level Events

2. Solution Description
Solution Requirements

All devices and platforms

Solution Approach

SUBJECTIVE - Use JAD file to define different game keys to key codes

Solutions Overview

Solution description:

1. Define keycodes in JAD file [example]
Defining specific key mapping for the MIDlet/Device, which is not dependent on the default key mapping of the device implementation

1.1 Read keycodes from JAD file [example]

1.2 Map keycodes to game keys [example]

1.3 handle key events in Canvas [example]

Alternative Solutions

None.

Examples, Illustrations

Example for defining keys in JAD file

...

  UP: 2

  DOWN: 8

  LEFT: 4

  RIGHT: 6

  FIRE: 5

  ...
 

Example for reading the key mapping form the JAD file

...

    /* This code can be called anywhere in the application */

    //Get User-Defined attribute from JAD

    int upKeyCode = 0;
    String codeStr = getAppProperty("UP");
    try {
      upKeyCode = Integer.parseInt(codeStr);
  } catch (NumberFormatException nfe) {

  }
  ...
 

Example for mapping keycodes to game keys

...

public int myGetGameAction(int keyCode) {
  if (keyCode == upKeyCode) {
    return Canvas.UP;
  }

  ...
}

  ...
 

Example for handling key events in Canvas

...

  public void keyPressed(int keyCode){

    if(myGetGameAction(keyCode)==LEFT){

      ...

    } else if(myGetGameAction(keyCode)==RIGHT){

      ...

    } else if(myGetGameAction(keyCode)==FIRE) {

      ...

    } else if(myGetGameAction(keyCode)==UP) {

      ...

    }

  ...
 
Additional Information
3. List of Affected Devices

All devices

4. Keywords

events, key code, game action

 
Copyright 2006 Sun Microsystems, Inc. and Orange SA All Rights Reserved.