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:


The keycode for key 0


The keycode for key 1


The keycode for key 2


The keycode for key 3


The keycode for key 4


The keycode for key 5


The keycode for key 6


The keycode for key 7


The keycode for key 8


The keycode for key 9


The keycode for the star key “*”


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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.