Handling Low-Level Events

If you use the Canvas class to write applications to access low-level input events or to issue graphics calls for drawing to the display, you must handle low-level events. Game applications are likely to use the Canvas class because it provides methods to handle game actions and key events. The key events are reported with respect to keycodes that are directly bound to concrete keys on the device.

The Canvas class, which is a subclass of Displayable, allows the application to register a listener for commands, but it requires applications to subclass the listener first. Also, while screens allow the application to define a listener and register it with an instance of the Screen class, the Canvas class does not allow this, because several listener interfaces need to be created, one for each kind of event.

Key Events

Every key for which events are reported is assigned a keycode. The MIDP defines the following keycodes in the Canvas class:

KEY_NUM0

The keycode for key 0

KEY_NUM1

The keycode for key 1

KEY_NUM2

The keycode for key 2

KEY_NUM3

The keycode for key 3

KEY_NUM4

The keycode for key 4

KEY_NUM5

The keycode for key 5

KEY_NUM6

The keycode for key 6

KEY_NUM7

The keycode for key 7

KEY_NUM8

The keycode for key 8

KEY_NUM9

The keycode for key 9

KEY_STAR

The keycode for the star key “*”

KEY_POUND

The keycode for the pound key “#”

As you probably guessed, these are the keys 0..9, *, and #. Other keys might exist on some devices, but for portability, applications ...

Get Wireless Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.