|
||||||||||||||||||||||||||
|
Low-Level Event Handling
The low-level UI provides a lot of event-handling options. Commands can be added to a Canvas in order to map soft-keys (physical keys on the keypad that map to labels on the display) just like high-level UI components such as List and Form. However, Canvas also allows for capturing raw key-press events for most of the keys available on the device. The Canvas APIs for monitoring key press activity are as follows:
In order to monitor key events, a subclass of Canvas must override one of these methods. An int code corresponding to the key to which the event pertains is passed as a parameter. It is important to understand that key codes are likely to change between platforms, making key event handling another sensitive area of low-level UI portability. For example, on device A, pressing "2" on the keypad may come through as key code 25 but on device B the same key might come through as key code 12. Fortunately, there are tools provided by MIDP to assist in normalizing key press events.
Key Handling Across Devices
The catch here is that if a device supports more than a 10-digit keypad, such as an alphanumeric keypad, MIDP does not provide constants to map all the keys. If an application needs to monitor an extended key code range, device-specific key handling may be needed.
In addition to the data-related keys, MIDP provides mapping support for some of the more standard control keys, such as up, down, left, right, and so forth. The method Canvas.getGameAction() can be used within one of the key-handling events to translate a raw key code to one of the following constant values defined in the Canvas class: UP, DOWN, LEFT, RIGHT, FIRE, GAME_A, GAME_B, GAME_C, GAME_D. In some cases, the mapping will work rather well. Many devices have some type of scrolling behavior that maps to the up, down, left and right keys. However, mappings for the FIRE key or the generic GAME_B key may translate in a way that is less than optimal for a particular application. The following code shows a keyPressed() event translating the FIRE and GAME_B keys, which, for the J2ME Wireless Toolkit, are mapped to the "select" key in the middle of the jog dial and the "3" key, respectively. The following table shows how the game key constant, the actual key (in the wireless toolkit emulator), and the raw key code relate to one another for these two key mappings
Below is an example showing how the getGameAction() method can be used to make decisions in key-handling events.
While the low-level APIs can be challenging to implement, they provide a way for developers to address portability issues that would difficult or impossible otherwise. With some thought and attention to detail, the low-level APIs can be used to create a custom user interface with a unique look and feel catering to a specific application's need. This flexibility does not need to come at the expense of portability either. By making use of font metrics, Canvas screen size methods and key mappings, MIDP applications can take advantage of the low-level APIs while remaining very portable across devices.
|
||||||||||||||||||||||||||
David Hemphill is the Lead Architect at Gearworks, a Minn.-based company that specializes in wireless workforce automation. He is also the co-author of 'Java 2 Micro Edition' (Manning). David is also a member of the Sun-Certified J2ME Exam expert group.
| ||||||||||||||||||||||||||
|