Controlling the Map with the Key Pad

Some users might prefer to control the map through the keypad (generally one "click," versus two "clicks" to cause a Menu event). Enabling this behavior also demonstrates how to respond to KeyPad events in general, so we've added some code to zoom in, zoom out, and back out of the current Activity: /**

* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) {

case KeyEvent.KEYCODE_DPAD_UP: // zoom in zoomIn(); return true; case KeyEvent.KEYCODE_DPAD_DOWN: // zoom out zoomOut(); return true;

case KeyEvent.KEYCODE_BACK: // go back (meaning exit the app) finish(); return true; default:

return false;

To catch key down events, we simply override onKeyDown and provide a switch for the different keys that are of interest. In addition to the keycodes you would expect (KEYCODE_A, ...KEYCODE_Z and things like KEYCODE_SPACE, KEYCODE_SHIFT_LEFT, and KEYCODE_SHIFT_RIGHT), Android includes keycodes that may or may not appear on any particular device (e.g., KEYCODE_CAMERA and KEYCODE_VOLUME_UP). A complete set of keycodes can be found at http://code.google.com/android/reference/android/view/Key Event.html.

0 0

Post a comment