Key Events

Team-Fly

Canvas handles events at a lower level than the other Displayable subclasses. Although you can add Commands and respond to them, Canvas also includes a set of methods that handle interaction with the individual keys of a device.

The following methods are called whenever the user presses and releases a key.

 protected void keyPressed(int keyCode) protected void keyReleased(int keyCode) 

The key code that is passed to these methods will most likely be one of the constants defined in Graphics, from KEY_NUM0 through KEY_NUM9 and including KEY_STAR and KEY_POUND. Devices may have more keys than this, which will be returned as device-specific key codes. Assuming there's an obvious mapping between the key and some Unicode character, the rule of thumb is that a key should have a code equal to its Unicode character value. Keys that don't have a Unicode mapping should use negative values. This means that, given a positive key code, you can find out the corresponding Unicode character by casting the int key code to char.

Note that key presses and key releases are separate events, which allows you considerable flexibility in how you design your user interface. The time between the press and the release could determine how high a game character jumps or how powerful a laser blast will be.

Depending on the device and the MIDP implementation, a key that is held down may spit out repeated key events. You can find out if repeated keys are supported by calling hasRepeatEvents(). If repeated key events are supported, the keyRepeated() method will be called with these events.

Finally, you can find a text description of a given key code by calling getKeyName().


Team-Fly


Wireless Java. Developing with J2ME
ColdFusion MX Professional Projects
ISBN: 1590590775
EAN: 2147483647
Year: 2000
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net