Hi all, I am trying to write a map application based on the MapView, and I need to handle the UP/DOWN/LEFT/RIGHT keys.
At first, I tried to implement with setOnKeyListener, please take a look at he following code. mapView = (MapView) findViewById(R.id.mapview); // set on key listener mapView.setOnKeyListener(onKeyListener); // Inner class for OnKeyListener final private OnKeyListener onKeyListener = new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { boolean ret = false; Log.i("SuperMap", "event.getAction()=" + event.getAction()); Log.i("SuperMap", "keyCode=" + keyCode); if (event.getAction() == KeyEvent.ACTION_DOWN) // key pressed down switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: Toast.makeText(SuperMap.this, "UP", Toast.LENGTH_SHORT) .show(); ret = true; break; case KeyEvent.KEYCODE_DPAD_DOWN: Toast.makeText(SuperMap.this, "DOWN", Toast.LENGTH_SHORT) .show(); ret = true; break; case KeyEvent.KEYCODE_DPAD_LEFT: Toast.makeText(SuperMap.this, "LEFT", Toast.LENGTH_SHORT) .show(); ret = true; break; case KeyEvent.KEYCODE_DPAD_RIGHT: Toast.makeText(SuperMap.this, "RIGHT", Toast.LENGTH_SHORT) .show(); ret = true; break; default: // do nothing here ret = false; break; } else { // do nothing when key released or multiple keys pressed at the // same time ret = false; } return ret; } }; It compiles OK, but does not works (there isn't any log in Logcat, seems it does not receive any key press event). Later, I tried to use the onKeyDown() function, so no setOnKeyListener () is needed, and all I need is just the following function which override the one in the super class. @Override public boolean onKeyDown(int keyCode, KeyEvent event) { boolean ret = false; Log.i("SuperMap", "event.getAction()=" + event.getAction()); Log.i("SuperMap", "keyCode=" + keyCode); if (event.getAction() == KeyEvent.ACTION_DOWN) // key pressed down switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: Toast.makeText(SuperMap.this, "UP", Toast.LENGTH_SHORT).show(); ret = true; break; case KeyEvent.KEYCODE_DPAD_DOWN: Toast.makeText(SuperMap.this, "DOWN", Toast.LENGTH_SHORT) .show(); ret = true; break; case KeyEvent.KEYCODE_DPAD_LEFT: Toast.makeText(SuperMap.this, "LEFT", Toast.LENGTH_SHORT) .show(); ret = true; break; case KeyEvent.KEYCODE_DPAD_RIGHT: Toast.makeText(SuperMap.this, "RIGHT", Toast.LENGTH_SHORT) .show(); ret = true; break; default: // do nothing here ret = false; break; } else { // do nothing when key released or multiple keys pressed at the // same time ret = false; } return ret; } This time, it works, but I am confused. According to my understanding, they are almost the same. Anybody knows why? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---